Support » Plugin: WooCommerce » You cannot add another “” to your cart – Redirect? 2

  • Anonymous User 16692328

    (@anonymized-16692328)


    Hey guys, having the same questions as this one more or less:
    https://wordpress.org/support/topic/you-cannot-add-another-to-your-cart-redirect/

    Having the shortcode [shop_page] on the front page. When adding a product to cart which is already in the card (at quantity 1), the page reloads, scrolls to top and displays the error message where the shortcode is displayed (NOT on top of the page unfortunately)
    Thats why I want to redirect to the shopping cart instead of showing the error message where noone can see it after addind already added products to the cart.

    The option from the answer above is just for normal redirects to the cart page if the product is available, not if the product quantity isnt available.

    Tried using the filter ‘woocommerce_cart_redirect_after_error’ without any effect.

    • This topic was modified 5 years, 3 months ago by Anonymous User 16692328.
Viewing 11 replies - 1 through 11 (of 11 total)
  • 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)

    Doesn’t seem work unfortunately. The regular redirect after adding to cart worked already before just setting the option.
    Using the woocommerce_add_to_cart hook does execute the redirect up to prio 20 even before the product was added to the cart resulting a redirect to an empty cart, setting higher priority numbers doesn’t do anything if the product quantity isnt available.

    • 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.
    • This reply was modified 5 years, 3 months ago by Anonymous User 16692328.
    Thread Starter Anonymous User 16692328

    (@anonymized-16692328)

    Any ideas?

    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.
    mappel

    (@mappel)

    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)

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘You cannot add another “” to your cart – Redirect? 2’ is closed to new replies.