Howdy 🙂
So am I understanding right that if someone has 1
of some item in their cart, and that item is ‘sold individually’ then you want to redirect them to the cart rather than display the notice?
If I have that right, then the way I would approach this would be via an unconventional use of a filter.
That filter can be seen here: https://github.com/woocommerce/woocommerce/blob/3.5.2/includes/class-wc-cart.php#L1033 So what I would do is just hook into that and in my callback function add some conditional code to redirect to the cart if the $found_in_cart variable is true. Something like this: https://gist.github.com/WillBrubaker/4b2798706d56f75d82997ddf29ab210f
Kind regards,
Thread Starter
Anonymous User 16692328
(@anonymized-16692328)
Hey, thanks a bunch for answering!
The item is not sold individually unfortunately, should’ve made that clearer, it’s some kind of charity item, which has a number of variants, each variant has the available quantity of 1.
Ended up putting the redirect directly into class-wc-cart for now:
if ( $product_data->managing_stock() ) {
$products_qty_in_cart = $this->get_cart_item_quantities();
if ( isset( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ] ) && ! $product_data->has_enough_stock( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ] + $quantity ) ) {
/*CUSTOM*/
exit(wp_redirect( wc_get_page_permalink('cart')));
Really ugly unfortunately, so do you have another brilliant solution like the one above for the variant-product?
-
This reply was modified 5 years, 3 months ago by Anonymous User 16692328.
-
This reply was modified 5 years, 3 months ago by Anonymous User 16692328.
-
This reply was modified 5 years, 3 months ago by Anonymous User 16692328.
Thread Starter
Anonymous User 16692328
(@anonymized-16692328)
Alternatively a solution could be to programatically scroll to where the product-shortcode is displayed after the error-message is shown there if thats achieveable?
That page reload before the message seems unnecessary.
-
This reply was modified 5 years, 3 months ago by Anonymous User 16692328.
-
This reply was modified 5 years, 3 months ago by Anonymous User 16692328.
ended up putting the redirect directly into class-wc-cart for now:
Please don’t do that. That’s just a horrible idea in so many ways.
You can hook into the woocommerce_add_to_cart
action and put your redirect there.
Thread Starter
Anonymous User 16692328
(@anonymized-16692328)
Thanks, @wbrubaker wbrubaker I was struggling with this one your second replay fixed my day 😀
Thread Starter
Anonymous User 16692328
(@anonymized-16692328)
@sadesades Thanks that really helps!
@wbrubaker that was it?
Eventually anyone else can solve this once and for all please ?
-
This reply was modified 5 years, 1 month ago by Anonymous User 16692328.
-
This reply was modified 5 years, 1 month ago by Anonymous User 16692328.
-
This reply was modified 5 years, 1 month ago by Anonymous User 16692328.
Thank you @wbrubaker !
Awesome, your code (added below) works like a charm on any product page
Unfortunately it doesn’t work with the shortcode
[add_to_cart id=’111′ class=” style=”]
on my landing page, for example.
It doesn’t redirect. Do I have to add the parameter “sold individually” in the shortcode as well?
Of course, the box “sold individually” is already ticked in the product settings.
https://gist.github.com/WillBrubaker/4b2798706d56f75d82997ddf29ab210f
add_filter( ‘woocommerce_add_to_cart_sold_individually_found_in_cart’, ‘wbrubaker_redirect_to_cart’ );
function wbrubaker_redirect_to_cart( $found_in_cart ) {
if ( $found_in_cart ) {
wp_safe_redirect( wc_get_page_permalink( ‘cart’ ) );
exit;
}
return $found_in_cart;
}
I am having the same issue with some clients who cannot read the screen and keep clicking on the same “add to cart” button. Is there a way to show the “View Cart” button when this error message pops-up ?
Thanks!
Thread Starter
Anonymous User 16692328
(@anonymized-16692328)