woocommerce_continue_shopping_redirect
Filters the URL where the "Continue Shopping" button redirects users after adding a product to the cart.
add_filter( 'woocommerce_continue_shopping_redirect', $callback, 10, 1 );
Description
Filters the URL where customers are redirected after adding an item to their cart in WooCommerce. Developers can alter this redirect destination, typically defaulting to the shop page or the previous referer, to customize the shopping experience.
Usage
add_filter( 'woocommerce_continue_shopping_redirect', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
// Redirect users to a specific product category page after adding a product to the cart,
// but only if the product being added is in the 'books' category.
add_filter( 'woocommerce_continue_shopping_redirect', 'my_custom_continue_shopping_redirect', 10, 1 );
function my_custom_continue_shopping_redirect( $redirect_url ) {
// Check if we are on the shop page or a single product page.
// This prevents unnecessary checks on other pages.
if ( is_shop() || is_product() ) {
// Get the product ID from the cart items.
// This assumes only one product is added at a time for simplicity in this example.
// A more robust solution might loop through all cart items.
$product_id = isset( WC()->cart->get_cart_contents()[key( WC()->cart->get_cart_contents() )]['product_id'] ) ? WC()->cart->get_cart_contents()[key( WC()->cart->get_cart_contents() )]['product_id'] : null;
if ( $product_id ) {
$product = wc_get_product( $product_id );
// Check if the product exists and has categories.
if ( $product && $product->get_category_ids() ) {
$category_ids = $product->get_category_ids();
$book_category_slug = 'books'; // Replace with your actual book category slug
// Check if any of the product's categories match the 'books' category slug.
foreach ( $category_ids as $cat_id ) {
$term = get_term_by( 'id', $cat_id, 'product_cat' );
if ( $term && $term->slug === $book_category_slug ) {
// If it's a book, redirect to the 'books' category archive.
$books_category_url = get_term_link( $book_category_slug, 'product_cat' );
if ( ! is_wp_error( $books_category_url ) ) {
return $books_category_url;
}
}
}
}
}
}
// If no specific conditions are met, return the original redirect URL.
return $redirect_url;
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/woocommerce/woocommerce-license.php:466
public function custom_add_to_cart_message_html( $message, $products, $show_qty ) {
$override = get_option( 'ulgm_add_to_cart_message', '' );
if ( ! empty( $override ) && ulgm_filter_has_var( 'add-seats' ) ) {
$titles = array();
$count = 0;
if ( ! is_array( $products ) ) {
$products = array( $products => 1 );
$show_qty = false;
}
if ( ! $show_qty ) {
$products = array_fill_keys( array_keys( $products ), 1 );
}
$found_license = false;
foreach ( $products as $product_id => $qty ) {
/* translators: %s: product name */
$product = wc_get_product( $product_id );
if ( $product instanceof WC_Product && $product->is_type( 'license' ) ) {
$found_license = true;
break;
}
}
if ( ! $found_license ) {
return $message;
}
foreach ( $products as $product_id => $qty ) {
/* translators: %s: product name */
$titles[] = ( $qty > 1 ? absint( $qty ) . ' × ' : '' ) . apply_filters( 'woocommerce_add_to_cart_item_name_in_quotes', sprintf( _x( '“%s”', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) ), $product_id );
$count += $qty;
}
$titles = array_filter( $titles );
// The custom message is just below
$added_text = sprintf( str_replace( '{{product}}', '%s', $override ), wc_format_list_of_items( $titles ) );
// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
$message = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), esc_html( $added_text ) );
} else {
$message = sprintf( '%s', esc_html( $added_text ) );
}
}
return $message;
}