If you have the “Redirect to the cart page after successful addition” setting active when you add a product to the cart you should be taken to the cart page.
When you're on that page you'll probably notice a little button that say's “Continue Shopping”. Now by default this button takes you back to the product you were on. You can, of course, change this to any destination you want with a filter.
In this case I want to change the location from the original product to the main shop page. You can find that page with the really useful wc_get_page_id
function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return the permalink of the shop page for the continue shopping redirect filter | |
* | |
* @param string $return_to | |
* @return string | |
*/ | |
function my_woocommerce_continue_shopping_redirect( $return_to ) { | |
return get_permalink( wc_get_page_id( 'shop' ) ); | |
} | |
add_filter( 'woocommerce_continue_shopping_redirect', 'my_woocommerce_continue_shopping_redirect', 20 ); |
Sorry for my ignorance but where would one paste this code? I am wanting to do exactly that, redirect to the shop page instead of the product.
Thanks!
Hey Pauline. Just add this to the bottom of your theme’s
functions.php
file. You can omit the opening php tag:.
I am trying to change the redirect to shop that occurs when you empty your cart and the ‘return to shop’ button appears.
That’s an entirely different snippet. I don’t have an answer to that right now but I did find something here: http://www.kriesi.at/support/topic/woocommerce-how-to-modify-the-return-to-shop-link-when-you-empty-your-cart/
if you’re more comfortable with templates than your
functions.php
file, the template for this page is in the woocommerce plugins hierarchy atplugins->woocommerce->templates->cart->cart-empty.php
.Duplicate this file and then move it to a folder in your theme called woocommerce and this will over ride the original file (making sure to rename it to the original file name). you can then go into the duplicate and change the links or button text by removing the respective php code that retrieves the text for the button and link.
Thank you so much Patrick for the solution.
Hi, what if I want to change the name of the button also?
Have you seen my post about the gettext filter?
Perfect, thanks a bunch:)
Yes! It’s finally working! Thank you so much! 😉
Thanks for the help!
This is not working for me I am on 2.3.5.
I checked in
wc-cart-functions.php
and the value ofreturn_to
is null, so it always redirects to cart page or previous page which is product details page$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf('%s %s', $return_to, __( 'Continue Shopping', 'woocommerce' ), $added_text );
What am I doing wrong?
Are you sure you have a shop page set? You can check under WooCommerce -> Settings -> Products -> Display.
wicked thanks so much, this worked for me without adding in the code, i just made sure it was set to shop. Thank you kind Sir!
how to redirect to category instead of single product page
You can change the
my_woocommerce_continue_shopping_redirect
function to return any url you want and the browser will be redirected to that page. I’d read through this helpful article about linking to posts / pages within WordPress.Great! Just a heads up in your snippet you are using the correct new wc_get_page_id function but you mention the deprecated ‘woocommerce_get_page_id’ function in the actual post content. It may confuse some people.
Wow – thanks so much. Really appreciate you finding the issue and pointing it out to me!
All updated now. 🙂
Hi, how could i remove the button?
I too am looking for a way to remove the “continue shopping” button all together, any suggestions would be appreciated.
Hello! It’s very interesting, but I’d like to know if it’s possible to add another location. In my case, I have a product with several accessories below, if the client click on an accessory and add to cart, when he click “continue shopping” is redirected to the accessory (last product he has visited), not to the main product with all the accessories. I’d like he is redirected to the main product with all the accessories, so he can go on looking and buying them. How can I change it? Is it possible??
Thank you very much!
HI
Sorry, but excatly what in the snippet should I change to “my own data” so to speak… ?
thanks for your sharing,… !!
any idea how to redirect with ” window.history.go(-2); ”
?
Thank you
I like doing it this way.
add_action( ‘woocommerce_after_cart_totals’, ‘add_continue_shopping_button_to_cart’ );
if ( ! function_exists(‘add_continue_shopping_button_to_cart’) ) :
function add_continue_shopping_button_to_cart() {
$shop_page_url = get_permalink( wc_get_page_id( ‘shop’ ) );
if (!empty($shop_page_url)):
echo ”;
echo ‘ ‘.__(‘Continue shopping’, ‘woocommerce’).’‘;
echo ”;
endif;
}
endif;
Thanks Patrick. I’ve watched your videos on Lynda.com and they are excellent. I appreciate this post and it worked like a charm. Thank you!
How to make “Continue Shopping” button return to previous page and not the shop pages?