How to Redirect specific category product pages to cart page in Woocommerce

Rate this post

i want to redirect users when they click single page product directly to cart without going to product description page for only one category products.

i used this code:

add_action( 'wp', 'ts_redirect_product_pages', 99 );

function ts_redirect_product_pages() {
    if ( is_product() ) {
        wp_safe_redirect( home_url('/cart/')); 

it’s redirecting to the cart page but all the other product categories also redirecting i only want one product category to be redirected, please guide me how to do this



Better to use template_redirect hook. To target specific product category(ies), you can use WordPress has_term() conditional function. For cart url, is better to use WooCommerce function wc_get_cart_url()

So in the code below define your product category(ies) term(s) name(s), slug(s) or Id(s):

add_action( 'template_redirect', 'ts_redirect_product_pages' );

function ts_redirect_product_pages() {
    $categories = array('my-category-1', 'my-category-2');

    if ( is_product() && has_term( $categories, 'product_cat' ) ) {
        wp_safe_redirect( wc_get_cart_url() );

Code goes in functions.php file of the active child theme (or active theme). It should work.

Leave a Reply

Your email address will not be published.

Back to top button