In the past, we discussed how to change the Return to Shop button URL in the Empty Cart page. Today, we will learn how to change the Continue Shopping button URL on the cart page to redirect wherever you want.
This button appears when the option Redirect to the cart page after successful addition is selected in WooCommerce > Settings > Products > General > Shop pages > Add to cart behaviour.
Before you make any code changes, have a backup of your site. This will help you restore things in case of unforeseen errors. I recommend using Jetpack Backup for real-time backups and one-click restores.
The necessary snippet is quite short, requiring only one line of code:
add_filter( 'woocommerce_continue_shopping_redirect', function() { return get_home_url(); } );
You can add a layer of complexity to the Continue Shopping button and return a different link, maybe conditionally by using a full function instead of an anonymous one, like so:
add_filter( 'woocommerce_continue_shopping_redirect', 'custom_continue_shopping_redirect' );
function custom_continue_shopping_redirect( $default ) {
//return home_url(); // Redirects to the Home page
//return wc_get_page_permalink( 'shop' ); // Redirects to the Shop page
//return get_permalink( get_option( 'page_for_posts' ) ); // Redirects to the Blog page
//return get_permalink( PAGE_ID ); // Redirects to the specific page by ID (Replace PAGE_ID with your ID
return $default; // Return to the default WooCommerce page
}
Choose the page you want to redirect to and remove the comment (the // at the beginning) on the respective line. If you want to, you can redirect conditionally, for example, based on products in the cart, or the role of the customer, as such:
add_filter( 'woocommerce_continue_shopping_redirect', 'custom_continue_shopping_redirect' );
function custom_continue_shopping_redirect( $default ) {
// Example condition 1: Redirect to Home
if ( /* your condition for Home */ ) {
return home_url(); // Redirects to the Home page
}
// Example condition 2: Redirect to Shop
if ( /* your condition for Shop */ ) {
return wc_get_page_permalink( 'shop' ); // Redirects to the Shop page
}
// Example condition 3: Redirect to Blog
if ( /* your condition for Blog */ ) {
return get_permalink( get_option( 'page_for_posts' ) ); // Redirects to the Blog page
}
// Example condition 4: Redirect to another page by ID
$another_page_id = 123; // Replace 123 with your specific page ID
if ( /* your condition for specific page */ ) {
return get_permalink( $another_page_id ); // Redirects to the specific page by ID
}
// Default redirect
return $default;
}


Leave a Comment