Do you want to get the cart after clicking the Add to Cart Button? In today’s post, we will share how you can redirect users directly to the cart after adding a new product to their cart.
Read on, as we will also share custom code snippets for redirecting a user to a custom page and checkout.
In addition, we will illustrate how you can conditionally redirect users after add to cart for certain products, categories, and shipping classes.
Before you proceed, it is important to create a child theme. This will ensure that your changes are not lost during an update.
Redirect Users To Cart After Add To Cart
It is not a complicated process to redirect users to the cart page after Add to Cart. This is because WooCommerce comes with a built-in option that allows you to redirect users directly to the cart when they’ve added a new product to their cart.
You can find the option in the WooCommerce > Settings > Products > Display area.
The next step is to check the “Redirect to the cart page after successful addition” option. This action will redirect all users to the cart after adding a product to the cart.
Redirect Users To A Custom Page
If you want to redirect users to a custom landing page, we will use a custom snippet to redirect the users to a specific page ID. You can also set a fully custom URL.
All you need to do is log in to your WordPress dashboard as an admin.
From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function to redirect users to a custom landing page
Add the following code to the functions.php file:
[php] /*** Redirect users after add to cart.
*/
function my_custom_add_to_cart_redirect( $url ) {
$url = get_permalink( 1 ); // URL to redirect to (1 is the page ID here)
return $url;
}
add_filter( ‘woocommerce_add_to_cart_redirect’, ‘my_custom_add_to_cart_redirect’ );
[/php]
Remember to add the correct URL for the code to work.
Redirect To Checkout After Add To Cart
Alternatively, you may want to redirect your users to the checkout page. If this is the case, add the following code to the function.php file of your child theme:
[php] /*** Redirect users after add to cart.
*/
function my_custom_add_to_cart_redirect( $url ) {
$url = WC()->cart->get_checkout_url();
// $url = wc_get_checkout_url(); // since WC 2.5.0
return $url;
}
add_filter( ‘woocommerce_add_to_cart_redirect’, ‘my_custom_add_to_cart_redirect’ );
[/php]
Conditionally Redirect Users After Add To Cart
If you want to redirect users to a different page only when they add a specific product to the cart or a product from a specific category/shipping class, we will share some of the custom code snippets you can use to achieve this.
However, it is important to disable the AJAX add to cart buttons if you want the conditional redirects to work correctly. This can be achieved by navigating to WooCommerce > Settings > Products > Display area.
The next step is to uncheck the “Enable AJAX add to cart buttons on archives” option.
1. Redirect For Certain Products
If you want to redirect for a specific product ID, add the following code in the functions.php file:
[php] /*** Redirect users after add to cart.
*/
function my_custom_add_to_cart_redirect( $url ) {
if ( ! isset( $_REQUEST[‘add-to-cart’] ) || ! is_numeric( $_REQUEST[‘add-to-cart’] ) ) {
return $url;
}
$product_id = apply_filters( ‘woocommerce_add_to_cart_product_id’, absint( $_REQUEST[‘add-to-cart’] ) );
// Only redirect the product IDs in the array to the checkout
if ( in_array( $product_id, array( 1, 16, 24) ) ) {
$url = WC()->cart->get_checkout_url();
}
return $url;
}
add_filter( ‘woocommerce_add_to_cart_redirect’, ‘my_custom_add_to_cart_redirect’ );
[/php]
Remember to add the correct product ID for the code snippet to work.
2. Redirect For Certain Categories
If you want to redirect for certain categories, add the following code in the functions.php file:
[php] /*** Redirect users after add to cart.
*/
function my_custom_add_to_cart_redirect( $url ) {
if ( ! isset( $_REQUEST[‘add-to-cart’] ) || ! is_numeric( $_REQUEST[‘add-to-cart’] ) ) {
return $url;
}
$product_id = apply_filters( ‘woocommerce_add_to_cart_product_id’, absint( $_REQUEST[‘add-to-cart’] ) );
// Only redirect products that have the ‘t-shirts’ category
if ( has_term( ‘t-shirts’, ‘product_cat’, $product_id ) ) {
$url = WC()->cart->get_checkout_url();
}
return $url;
}
add_filter( ‘woocommerce_add_to_cart_redirect’, ‘my_custom_add_to_cart_redirect’ );
[/php]
Remember to input the correct categories in the code snippet.
3. Redirect For Certain Shipping Classes
If you want to redirect for certain shipping classes, add the following code in the functions.php file:
[php] /*** Redirect users after add to cart.
*/
function my_custom_add_to_cart_redirect( $url ) {
if ( ! isset( $_REQUEST[‘add-to-cart’] ) || ! is_numeric( $_REQUEST[‘add-to-cart’] ) ) {
return $url;
}
$product_id = apply_filters( ‘woocommerce_add_to_cart_product_id’, absint( $_REQUEST[‘add-to-cart’] ) );
// Only redirect products with the ‘small-item’ shipping class
if ( has_term( ‘small-item’, ‘product_shipping_class’, $product_id ) ) {
$url = WC()->cart->get_checkout_url();
}
return $url;
}
add_filter( ‘woocommerce_add_to_cart_redirect’, ‘my_custom_add_to_cart_redirect’ );
[/php]
Remember to add the correct shipping class in the code snippet.
Conclusion
By now, we are sure that you can redirect users to different pages in your WooCommerce store. It is important to add the code snippets in the functions.php file of your child theme. This ensures that your changes are not lost during an update.
If you need any further customization, feel free to reach out to us for a custom quote. We hope that this post helped you to learn more about redirecting users after add to cart.
Similar Articles
- How to Redirect User If Not Logged in WordPress » Page Redirect
- How to Set Up WooCommerce You Must be Logged in to Checkout
- How to Redirect to Cart after Login in WooCommerce
- How to Add WooCommerce Login With Phone Number
- How to Show Tax on Checkout In WooCommerce
- WooCommerce Different Shipping For Different Countries
- How to Change SKU Text WooCommerce
- How to Style WooCommerce Checkout Page
- How to Fix Checkout Is Not Available Whilst Your Cart Is Empty WooCommerce
- How to Get Last Order by User Id WooCommerce
- WooCommerce Visual Hook Guide Checkout
- How to Change Out Of Stock Text in WooCommerce
- How to Install WooCommerce Storefront Step By Step [Complete Guide]
- How to Get WooCommerce Page URLs » Cart, Shop,Checkout & Account
- How to Add Text after Price in WooCommerce » Add Price Suffix
- How to Hide Downloads From My Account WooCommerce
- WooCommerce Archive Hooks [Complete Guide]
- How to Set Up WooCommerce Per Product Shipping
- Hide or Remove the Quantity Field from the WooCommerce Product Page