• Resolved smriazati

    (@smriazati)


    Hi Bhavik,

    I’m really happy to find this plugin! However I think I found one bug, due to my particular shop set up.

    In my online shop, we skip the cart page entirely, so when users click the “add to cart” button on a single product page, they are redirected immediately to the Checkout page.

    I am doing this redirect with the following code in my functions.php file:

    function skip_cart_page_redirection_to_checkout() {
    // If is cart page, redirect checkout.
    if( is_cart() )
    wp_redirect( WC()->cart->get_checkout_url() );
    }

    add_action(‘template_redirect’, ‘skip_cart_page_redirection_to_checkout’);

    The problem with this plugin comes when a user chooses to click the “Remove” button. When a user clicks the button, the plugin removes the item from cart, and tries to send the user back to the cart page. But in my particular situation, this creates a redirect loop (Plugin sends to cart > skip_cart_page_redirection_to_checkout tries to send to checkout, but Cart is empty so Checkout page can’t be accessed).

    Here’s what I would like to happen: When a user clicks the “Remove” button, they should return back to that original product page.

    Do you see a way to this solution?

    thanks,
    Sarah

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author bhavik.kiri

    (@bhavikkiri)

    Hi Sarah,

    Thank you for appreciating the plugin. And your detailed reply.

    >When a user clicks the button, the plugin removes the item from cart, and tries to send the user back to the cart page.

    Yes, when your cart is empty then it will send back to the cart page. And according to your code, it sends back to the checkout page and it will go into the loop.

    What we can do here, that we can make 1 check point. That will check if cart content is not empty then only send customers to the checkout page. If cart content is empty then send back to the shop page.

    Below is the code patch which you need to put in the function which you had shared above.

    function skip_cart_page_redirection_to_checkout() {
        // If is cart page, redirect checkout.
        if( is_cart() && WC()->cart->cart_contents_count != 0 ) { 
            wp_redirect( WC()->cart->get_checkout_url() );
        }else if ( is_cart() && WC()->cart->cart_contents_count == 0 ) {
            wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'shop' ) ) );
        }
    }

    Here, in above case if the cart is empty then it will redirect to the shop page.

    Note: I have not tested this solution at my end. If you have any issues please let me know.

    Please let me know the result.

    Regards,
    Bhavik Kiri

    Thread Starter smriazati

    (@smriazati)

    Hi Bhavik,
    Thanks for the quick reply!

    This solution could work for me – but we were not planning to allow users to visit the Shop page either.

    I tried to update your code snippet to redirect to a specific product ID, like this, but this was unsuccessful and ended up redirecting to the cart page:

    function skip_cart_page_redirection_to_checkout() {
        // If is cart page, redirect checkout.
        if( is_cart() && WC()->cart->cart_contents_count != 0 ) { 
            wp_redirect( WC()->cart->get_checkout_url() );
        }else if ( is_cart() && WC()->cart->cart_contents_count == 0 ) {
            wp_safe_redirect( get_permalink( get_page_id( '1606' ) ) );
        }
    }

    Any suggestion why the above doesn’t work?

    Ideally, instead of specifying only one product, we could redirect a user to the product page for the product she just removed.

    Is that possible here?

    Since we need a quick solution, for now we decided to allow users to visit the Shop page in this instance, and redesigned it to better match our store.

    Thanks!
    sarah

    Plugin Author bhavik.kiri

    (@bhavikkiri)

    Hi Sarah,

    Can you please let me know you are able to solve the issue?

    Regards,
    Bhavik

    Thread Starter smriazati

    (@smriazati)

    Hi Bhavik –
    Thanks for following up. We resolved this issue by creating a custom design for the shop page. So now, when a user removes the final item from her cart, she is redirected to the new shop page like in the original function.

    Our ideal scenario would have been: user removes last item from cart > user is redirected to the single product page of the product that was just removed from the cart, but we didn’t have time to implement…

    thanks!
    Sarah

    Plugin Author bhavik.kiri

    (@bhavikkiri)

    Hi Sarah,

    Glad to know that you are able to apply the alternative fix for your issue.

    I hope you will implement your permanent solution in near future. ??

    Would you like to write the review for the plugin? It would help me immensely.

    Regards,
    Bhavik

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Remove button creates redirect loop when Cart page is disabled’ is closed to new replies.