Redirect to Checkout After Adding Product in WooCommerce

WooCommerce can redirect to the cart after add to cart out of the box, but it has no setting to redirect to checkout after add to cart. For that you need the woocommerce_add_to_cart_redirect filter.

Skipping the cart makes sense for stores that sell one product at a time, such as courses, subscriptions or event tickets. Below are the code, the related setting, and the AJAX buttons that often make the redirect look broken.

Redirect to Checkout After Add to Cart

Add this to your child theme functions.php file or to a custom plugin:

/**
 * Send customers straight to checkout after adding a product to the cart.
 */
function savvy_add_to_cart_redirect_checkout( $url ) {
    return wc_get_checkout_url();
}
add_filter( 'woocommerce_add_to_cart_redirect', 'savvy_add_to_cart_redirect_checkout' );

Then go to WooCommerce > Settings > Products and tick Redirect to the cart page after successful addition under “Add to cart behaviour”. The next section explains why the setting matters.

How It Works

When a product is added through a regular form submit or an ?add-to-cart= link, WooCommerce passes the redirect URL through woocommerce_add_to_cart_redirect. If the filter returns a URL, WooCommerce redirects there, whatever the setting says.

The filter also receives the product being added as a second argument. That lets you redirect only for some products, as shown below.

Older tutorials use WC()->cart->get_checkout_url(). That method has been deprecated since WooCommerce 2.5. It still works, but it triggers a deprecation notice and simply calls wc_get_checkout_url(), so use the function directly.

AJAX Add to Cart and the Redirect Setting

Shop and category pages usually add products with AJAX, controlled by Enable AJAX add to cart buttons on archives.

An AJAX request never reaches the redirect code in PHP, which is why the filter alone often seems to do nothing on archives.

WooCommerce handles this with the redirect setting:

  • Setting off: archive buttons add the product in the background and stay on the page. Your filter only affects single product pages and add to cart links.
  • Setting on: after the AJAX request succeeds, the script redirects to the cart URL, and that URL also goes through woocommerce_add_to_cart_redirect. With the snippet active, the result is checkout.

In block themes, the Product Button and Add to Cart with Options blocks switch to a regular link or form submit when the setting is on, so they go through the filter too.

Ticking the setting is what makes the redirect consistent across every button.

Redirect Only for Specific Products

Use the product argument to limit the redirect. This version skips the cart only for products in a courses category:

function savvy_add_to_cart_redirect_courses( $url, $product ) {
    if ( $product instanceof WC_Product && has_term( 'courses', 'product_cat', $product->get_id() ) ) {
        return wc_get_checkout_url();
    }
    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'savvy_add_to_cart_redirect_courses', 10, 2 );

The instanceof check matters. When WooCommerce builds the cart URL for its AJAX script, it calls the filter with null instead of a product.

If a link adds a variation ID directly, $product is the variation, so check its get_parent_id() instead.

To show a message on the same page instead of redirecting, see Show a Message After Product is Added to Cart in WooCommerce. For more on the actions and filters involved, see Mastering WooCommerce Hooks: A Complete Guide.

FAQs

Common questions about redirecting to checkout after add to cart:

Is there a WooCommerce setting to redirect to checkout after add to cart?
No. The built-in setting under WooCommerce > Settings > Products only redirects to the cart. To land on checkout, combine that setting with the woocommerce_add_to_cart_redirect filter.
Why does the redirect work on product pages but not on the shop page?
Shop page buttons use AJAX, which does not run the PHP redirect. Enable "Redirect to the cart page after successful addition" and WooCommerce will send AJAX adds to the filtered URL as well.
Does the filter change the View cart link?
Yes, on classic archives. The "View cart" link that appears after an AJAX add uses the same filtered URL, so with the redirect setting off it points to checkout.
Will this work with Ajax add to cart on single product pages?
Not automatically. Custom AJAX on the single product form bypasses the PHP redirect, so the script must redirect itself after the request succeeds.
Discussion
[  0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!