When shopping on a website, driven by WordPress and WooCommerce, sometimes you want your users to just return directly to the shop. Rather than having the user search for the shop navigation button, you want to give them a button, as soon as they add a product to the cart.
The trick is that what you may not want to a direct button on the page, regardless of any action. Now, I am not sure about you, but for me, I want to make sure that I am not redirecting off the product page itself, unless the action of ‘add to cart’ has been taken by the user.
When adding a product to the cart from the single product page, you are often greeted with a message that asks you if you want to now go directly to the cart and thus checkout. What if you wanted to also offer a way to greet the use with a ‘Continue Shopping’ and return them back to the full shop page? Sound like a good idea? To me it did, so I sat down and wrote this little piece of code.
We will use the wc_add_to_cart_message
– WooCommerce hook to add a shiny new button to the added to cart message.
I placed your code at the end of my theme’s function.php on my WC site at starkravingcat.com but it didn’t work. I also tried several other code fragments from others – they worked but they put the Continue Shopping button under the Add to Cart button. If you clicked Add to Cart, the Continue Shopping button disappeared completely. Which is why I hoped your solution would work since you showed an image of the Continue Shopping button on the View Cart message.
Don’t know if this will show up correctly, but here’s one of the code fragments that partially worked on my site:
add_action( ‘woocommerce_single_product_summary’, ‘my_continue_shopping_button’, 31 );
function my_continue_shopping_button() {
if ( wp_get_referer() ) echo ‘Continue Shopping‘;
}
Any ideas why your code didn’t work?
I just tried your modified code but no luck.
Is there a store page or test page where you have this working that I can see it in action?
That may give me a clue to why its not working on my store.
Thanks for trying!
removing the ‘if’ statement, updated gist above. see: This link
OK, so you really are a genius. ; )
Latest iteration appears to work on all my tests so far.
Thanks!
ahh- your right… I updated the code. Instead of calling is_product, let’s use is_single. this works now. thanks for your feedback π
Hi, I have used your code and it works fantastically but i would like to click the continue shopping button and go to different page instead of the shop page, any ideas how this can be done?
Thanks
Kevin
Glad itβs working for you!
You could use this filter: woocommerce_continue_shopping_redirect And return the url that you want it to go to π
Hope that helps!
Thanks for replying, I am a newbie to hooks and know nothing about php. Can you tell me the syntax of how to write the filter and where is goes in the code below.
add_filter( ‘wc_add_to_cart_message_html’, ‘add_continue_shopping_button’, 10, 2);
function add_continue_shopping_button( $message, $products ){
$message .= sprintf( ‘%s‘, esc_url( wc_get_page_permalink( ‘shop’ ) ), esc_html__( ‘Continue Shopping’, ‘woocommerce’ ) );
return $message;
}
Thanks
Hi Kevin –
I will attempt to get some code for you to add to this plugin… BUT, for now, in the code you have here, you can just replace ‘shop’ with whatever page slug you desire.
Hope this helps!
Hi Ray,
I have tried changing the slug to another pages slug and even to ‘basket’ but it just takes me back to the home page. The link only seems to work with the slug ‘shop’. I have permalinks set to custom /%postname%/. Any ideas!
Thanks
did you ever get an answer to this? i’m running into the same issue.
thx!
Hey guys: so sorry I have not address this… try this:
wc_get_page_permalink( ‘some-page’, ‘/path-to/some-page/’ )
adding the ‘path’ as a second parameter will allow it to go to ‘some-page’ which you are adding in the first parameter.
for example: wc_get_page_permalink( ‘basket’ ) would become:
wc_get_page_permalink( ‘basket’, ‘/basket/’ )
just exchange that function within the code and you will be set π
Let me know if you find success here!