Website Building » WooCommerce » How Do I Redirect Checkout After Add to Cart in WooCommerce?

How Do I Redirect Checkout After Add to Cart in WooCommerce?

Last updated on January 18, 2023 @ 9:13 am

When you add a product to your cart in WooCommerce, the default behavior is to take you to the cart page. For some stores, it might make more sense to keep customers on the same page and redirect them to the checkout page after they add a product to their cart.

In this article, we’ll show you how to do just that.

First, you need to add this code to your functions.php file:

PRO TIP: If you are not familiar with code or are not comfortable working with code, do not attempt to redirect checkout after add to cart in WooCommerce. You could potentially break your site.
function wc_custom_add_to_cart_redirect() { 
    return wc_get_checkout_url(); 
} 
add_filter( 'woocommerce_add_to_cart_redirect', 'wc_custom_add_to_cart_redirect' );

This code tells WooCommerce to redirect customers to the checkout page after they add a product to their cart. You can also use this code snippet in a plugin or child theme.

If you want to keep customers on the same page and just display a message that their product has been added to the cart, you can use this code:

function wc_custom_add_to_cart_message() {
    $message = sprintf( '%s has been added to your cart.', get_the('title') );
    return $message;
}
add_filter( 'wc_add_to_cart_message', 'wc_custom_add_to_cart_message');

Redirecting customers to the checkout page after they add a product to their cart is a great way to keep them focused on completing their purchase. You can also use this technique to upsell products by redirecting customers to a related products page.

Dale Leydon

Dale Leydon

Sysadmin turned Javascript developer. Owner of 20+ apps graveyard, and a couple of successful ones.