Home » How to Redirect Users after Add to Cart in WooCommerce

How to Redirect Users after Add to Cart in WooCommerce

Last updated on June 13, 2020 by

Here, We will show you the best ways to redirect users after add to cart in WooCommerce to the custom page, checkout page, or any condition based as soon as the product added to the cart. Are you ready? Let’s do it together.

Table of Contents
1. Redirect Users to a Checkout Page after Add to Cart
2. Redirect Users to a Custom Page after Add to Cart
3. Conditionally Redirect Users after Add to Cart
3.1 Redirect Users When Product’s Price Is Greater Than The Limit after Add to Cart
3.2 Redirect Users for Specific Product ID after Add to Cart
3.3 Redirect Users for Specific Category/Tags/Shipping Class after Add to Cart

Precondition

Here, We are forcing users to redirect somewhere after the product added to the cart. But there is an already default option in WooCommerce that redirect users directly to the cart. So first we need to disable it.

To do so, go to WooCommerce > Settings > Products > General and disable both options:
1) Redirect to the cart page after successful addition
2) Enable AJAX add to cart buttons on archives

WooCommerce Settings to Allow for Checkout Redirect

An Ideal Place to Add Code in WordPress

Most Important: Add the following code to your child theme’s functions.php file. If you add custom code directly to your parent theme’s functions.php file then it will be wiped entirely when you update the theme.

If you are using a custom theme and if it doesn’t require any update then you can directly place the code into wp-content/themes/your-theme/function.php file.

Please note that all the code has been tested with Storefront (Parent Theme) & Storefront-Child theme.

01 Redirect Users to a Checkout Page after Add to Cart

add_filter( 'woocommerce_add_to_cart_redirect', 'tf_redirect_to_checkout_add_cart' );

function tf_redirect_to_checkout_add_cart( $url ) {

    $url = WC()->cart->get_checkout_url();
    // $url = wc_get_checkout_url(); // since WC 2.5.0

    return $url;
}

02 Redirect Users to a Custom Page after Add to Cart

If you have noticed the above example, then you can realize that we are just passing the page URL for redirection. So here, we will redirect the user to any other custom page.

Custom page redirection useful to show something very important to the user in between the journey from cart to checkout. Let’s say you have a page “Precaution” and it has an ID 177“.

/*@ Add to cart redirection to custom page */
add_filter( 'woocommerce_add_to_cart_redirect', 'tf_redirect_to_checkout_add_cart' );

function tf_redirect_to_checkout_add_cart( $url ) {

    $url = get_permalink(177);

    /*@ For page redirection by slug
    
    $url = get_permalink( get_page_by_path( 'precaution' ) );
    
    */
   
    return $url;
}

On the above example, We have used get_permalink($page_id) function which is used to get the link of the provided ID of page/post.

You need to replace your desired page ID to make the above function works.

03 Conditionally Redirect Users after Add to Cart

Sometimes, You want to redirect users to the custom page when certain conditions are met. The condition can be anything. We can make any condition using the data.

Maybe you want to redirect users when the product of a certain category/tag or price of product greater/ less than some limit or product has a specific shipping class or product with specific IDs, etc added. Let’s see some examples with the snippet.

3.1 Redirect Users When Product’s Price Is Greater Than The Limit after Add to Cart

/**
 * If product price is greater than 40 then redirect users to page1
 */
add_filter( 'woocommerce_add_to_cart_redirect', 'tf_redirect_to_custom_page_add_cart' );

function tf_redirect_to_custom_page_add_cart( $url ) {
    
    /*@ Return default URL if add-to-cart not set or is not numeric 
     *
     *  Use : add-to-cart query string carry a product ID so we can use that
     *  to get the product data and we can set condition on it
     *  
     */
    if ( !isset($_GET['add-to-cart']) || !is_numeric($_REQUEST['add-to-cart']) ) :
        return $url;
    endif;
    
    /*@ Convert a value to non-negative integer */
    $product_id = absint($_GET['add-to-cart']);

    /*@ Get product data and get price for comparision*/
    $product = wc_get_product( $product_id );
    $price = $product->get_price();  

    /* To get regular price : $product->get_regular_price();
       To get sale price : $product->get_sale_price(); */
    
    /*@ Redirect users if product price > 40 to the custom page 
        else redirect to the default cart page */
    if ( $price > 40 ) :
        $url = get_permalink(61);
    endif;
    
    return $url; /*@ default cart link*/

}

We have given an explanation of the code in the above example to better understand and try to add some alternative function so that you can try more conditions.

3.2 Redirect Users for Specific Product ID after Add to Cart

add_filter( 'woocommerce_add_to_cart_redirect', 'tf_redirect_to_custom_page_add_cart' );
function tf_redirect_to_custom_page_add_cart( $url ) {
    
    /*@ Return default URL if add-to-cart not set or is not numeric 
     *
     *  Use : add-to-cart query string carry a product ID so we can use that 
     *        to get the product data and we can set condition on it
     */
    if ( !isset($_GET['add-to-cart']) || !is_numeric($_REQUEST['add-to-cart']) ) :
        return $url;
    endif;
    
    /*@ Convert a value to non-negative integer */
    $product_id = absint($_GET['add-to-cart']);
    
    /*@ Redirect users if add-to-cart product ID matched with given product IDs, 
        then redirect to the custom page else redirect to the default  */
    if ( in_array( $product_id, array( 23, 77, 17) ) ) :
         $url = get_permalink(61);
    endif;
    
    return $url; /*@ default cart link*/

}

3.3 Redirect Users for Specific Category/Tags/Shipping Class after Add to Cart

add_filter( 'woocommerce_add_to_cart_redirect', 'tf_redirect_to_custom_page_add_cart' );
function tf_redirect_to_custom_page_add_cart( $url ) {
    
    /*@ Return default URL if add-to-cart not set or is not numeric 
     *
     *  Use : add-to-cart query string carry a product ID so we can use that 
     *        to get the product data and we can set condition on it
     */
    if ( !isset($_GET['add-to-cart']) || !is_numeric($_REQUEST['add-to-cart']) ) :
        return $url;
    endif;
    
    /*@ Convert a value to non-negative integer */
    $product_id = absint($_GET['add-to-cart']);
    
    /*@ Redirect users if add-to-cart product category is "clothing" 
        then redirect to the custom page else redirect to the default  */
    if ( has_term( 'clothing', 'product_cat', $product_id ) ) :
         $url = get_permalink(61);
    endif;

    
    return $url; /*@ default cart link*/

}

Additionally read our article, Set Default Country and State on The Checkout Form in WooCommerce

That’s it from us. We hope this article helped you to learn How to Redirect Users after Add to Cart in WooCommerce with different ways.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post. Keep Smiling 🙂 & Keep Coding!

 
 

Leave a Comment