Redirect WooCommerce single product page

Redirect to add-to-cart url

Instead of showing a single product page, we can add the product to shopping cart, when someone clicks on the single product link or tries to browse the product page with a direct link. This only works for simple product type and simple subscription product type, if you have WooCommerce Subscription plugin active.

Default WooCommerce add-to-cart url is https://yoursite.com/cart/?add-to-cart=123, where 123 is the product id. We will utilize this url to achieve our goal with the help of template_redirect hook.

function careless_redirect_product_pages() {
    //only for product single pages
    if ( is_product() ) {
        //get the WC_Product object for the current product
        $product = wc_get_product( get_the_ID() );

        //check for product type
        if ( $product->get_type() === 'simple' || $product->get_type() === 'subscription' ) {
            //add-to-cart url with current product id
            $redirect_to = wc_get_page_permalink( 'shop' ) . '?add-to-cart=' . get_the_ID();

            wp_redirect( $redirect_to );
            exit();
        }
    }
}
add_action( 'template_redirect', 'careless_redirect_product_pages' );

Redirect to home page

Following code will redirect the product single page to home page.

function careless_redirect_product_pages() {
    //only for product single pages
    if ( is_product() ) {
        wp_redirect( home_url() );
        exit();
    }
}
add_action( 'template_redirect', 'careless_redirect_product_pages' );

Redirect to shop page

We can also redirect the product single page to WooCommerce shop page (and pretty much to any page) easily.

function careless_redirect_product_pages() {
    //only for product single pages
    if ( is_product() ) {
        wp_redirect( wc_get_page_permalink( 'shop' ) );
        exit();
    }
}
add_action( 'template_redirect', 'careless_redirect_product_pages' );

Code goes in the functions.php file of your active theme.