Support » Plugin: WooCommerce » How to make “return to shop” button return to previous page and not the cart?

  • Resolved jenclickspring


    So I’ve been fiddling with this for hours and can’t seem to figure this out.

    I just want the “return to shop” button (visible when cart is empty) to return to the previous page.

    I have found ways to make “return to shop” return to the previous page, but the code I’ve found all lead to an infinite loop problem in which the cart thinks the “previous page” is also the cart page when it is emptied because that was the “previous page” before it was emptied. Hence, clicking “return to shop” will link right back to the cart.

    For example, the code below should have worked if not for the infinite loop problem:

    function wc_empty_cart_redirect_url() {
        return $_SERVER['HTTP_REFERER'];
    add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );

    Is there a way to accomplish this?

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support RK


    Hi there,

    That cart should work if you disable the setting found under **WooCommerce > Settings > Products > General > Redirect to the cart page after successful addition**. Otherwise you might first have to add another conditional to check what page the user is currently on before returning a URL.



    Hi RK,

    Before I saw this message, I went into cart-empty.php and adjusted the code to the following:

    if ( wc_get_page_id( 'shop' ) > 0 ) : ?>
    	<p class="return-to-shop">
    		<a href="history.go(-1);">
    			<?php _e( 'Back to Dashboard', 'woocommerce' ) ?>
    <?php endif; ?>

    I basically just added the href as “javascript:history.go(-1)”. I tested it and it does work to go back to the previous page (not stuck in the cart), but do you think it would cause problems for other users? Do you think there are any other possible issues other than it not working if the user has javascript disabled (which is probably rare right?) Thanks!

    Plugin Support ryanr14


    Hi @jenclickspring,

    Did you by chance copy that template file over into your theme before modifying? If you haven’t, that’s what I’d recommend doing first (if you didn’t already). That way the customization will be safe from WooCommerce updates.

    I’d say if that works for you, keep using it. Just know if the template gets updated in WooCommerce you’ll want to compare the changes and keep your copied template file up to date.

    Plugin Support Kenin


    Automattic Happiness Engineer

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to make “return to shop” button return to previous page and not the cart?’ is closed to new replies.