Reputation: 121
I found the below code to add a 'Return to Shop' button, it works perfectly
add_action('woocommerce_cart_actions', function() {
?>
<a class="button wc-backward" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>"> <?php _e( 'Return to shop', 'woocommerce' ) ?> </a>
<?php
});
I wonder if there is a way to change it to return to the previous page - i.e. like hitting back on the browser to the previous viewed product?
And one step further, could the text be dynamic, saying "Return to [name of previous page]"
This might be challenging!
Upvotes: 1
Views: 2305
Reputation: 29624
You can use something like this
More about parse_url
It depends on how your links are constructed
function action_woocommerce_cart_actions( ) {
// Pass the URL it came from. Expl: https://www.example.com/shop/product-1/
$previous = $_SERVER['HTTP_REFERER'];
// Get last part from url. Expl: product-1
$last_part = basename( parse_url( $previous, PHP_URL_PATH ) );
?>
<a class="button wc-backward" href="<?php echo $previous ?>">
<?php _e( 'Return to ' . $last_part, 'woocommerce' ) ?>
</a>
<?php
}
add_action( 'woocommerce_cart_actions', 'action_woocommerce_cart_actions', 10, 0 );
Upvotes: 1