Home » WooCommerce: Redirect Users After Add To Cart

WooCommerce: Redirect Users After Add To Cart

Last updated on June 16, 2022 by

Here, We will show you the best different 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 is 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 is 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

In this, we will see Woocommerce add to cart custom redirect. For that, we have used a woocommerce_add_to_cart_redirect WooCommerce hook which will be used after add to cart redirect. You just need to return the URL which you want to redirect after the product is added to the cart. That’s it.

Add the below code to your child themes’ function.php file to redirect the user to the checkout page after add to cart in WooCommerce:

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 in WooCommerce.

Custom page redirection is 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;
}

In the above example, We have used a get_permalink($page_id) function that 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 could 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 comment in the above example to better understand and try to add some alternative functions 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 guide:

  1. How to Add Products Per Page Dropdown in WooCommerce
  2. “Sorry, your session has expired. Return to homepage” – WordPress WooCommerce Error
  3. How to Create a Plugin in WordPress from Scratch
  4. ERROR: Cookies are blocked or not supported by your browser
  5. How To Send Custom Emails in WordPress
  6. How to Allow Preview of Draft Post Without Login in WordPress
  7. Import Users From CSV In WordPress Programmatically
  8. WordPress Keeps Redirecting To wp-admin/install.php
  9. How to Remove the Category From URL in WordPress
  10. How to Add Custom Column To Any Post Types in WordPress
  11. Dynamically Populate A Select Field’s Choices In ACF
  12. How To Create Database Table In WordPress
  13. Difference Of require, include, require_once And include_once
  14. PHP: Get Day Name From Date
  15. PHP: Get Year From Date
  16. How To Update Pivot Table In Laravel
  17. Laravel 9 Resource Controller And Route With Example
  18. Laravel 9 File Upload Tutorial With Example
  19. How To Schedule Tasks In Laravel With Example
  20. Laravel Collection Push() And Put() With Example

That’s it from us. We hope this article helped you to learn how to redirect users after add to Cart in WooCommerce in 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 in advance 🙂. Keep Smiling! Happy Coding!

 
 

Leave a Comment