Hi there!
I would like to add a back button in single product pages to return to shop page.
You can use the following code to add the “Go back” button on the single product page:
add_action( 'woocommerce_after_add_to_cart_button', 'my_function_sample', 10 );
function my_function_sample() {
global $product;
echo ' <button type="button" onclick="history.back();"> Go back </button> ';
}
If you’d like to show the button somewhere else on the single product page, then replace woocommerce_after_add_to_cart_button
with any other hook from here: https://businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/
Also, this custom code should be added to your child theme’s functions.php
file or via a plugin that allows custom functions to be added, such as Code Snippets. Please don’t add custom code directly to your parent theme’s functions.php
file as this will be wiped entirely when you update.
Also, I would like to add another one in purchase process to go back through cart page
The code above can be tweaked to work for your cart/checkout pages — you’ll just need to change woocommerce_after_add_to_cart_button
Cart page hooks here: https://businessbloomer.com/woocommerce-visual-hook-guide-cart-page/
Checkout page hooks here: https://businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/
You can also then replace echo ' <button type="button" onclick="history.back();"> Go back </button> ';
with:
echo '<a href="URL">Back to cart</a>';
The URL
need to be changed to the URL of your shop page.
Cheers!