WooCommerce PHP Beginner Requires WooCommerce

Send Shoppers Straight to Checkout After Adding to Cart

Skip the cart page for stores with one product or simple orders: clicking Add to cart goes straight to checkout.

Tested on WordPress 7.1 PHP 8.3 Updated
functions.php 10 lines
// Go to checkout after a successful add to cart.
add_filter( 'woocommerce_add_to_cart_redirect', function (): string {
	return wc_get_checkout_url();
} );

// Shop page buttons add to cart in the background by default. Turn that off so they redirect too.
add_filter( 'pre_option_woocommerce_enable_ajax_add_to_cart', fn () => 'no' );

// Remove the "View cart" notice that would show on the checkout page.
add_filter( 'wc_add_to_cart_message_html', '__return_empty_string' );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

If most customers buy a single item, the cart page is an extra step with nothing to do. This snippet sends them straight to checkout after they click Add to cart.

How it works

The woocommerce_add_to_cart_redirect filter decides where to go after a product is added. Returning the checkout URL skips the cart.

Buttons on the shop page normally add products in the background without leaving the page, which skips the redirect. The second line overrides the “Enable AJAX add to cart buttons on archives” setting so those buttons redirect too. The last line removes the “added to your cart” message, which would otherwise appear at the top of checkout.

Things to watch

  • Product blocks and the block-based product collection add to cart through the Store API, which doesn’t follow this redirect.
  • Customers can still reach the cart from your header. Make sure the checkout page shows the order summary clearly, since it’s now the only place they review their order.

How to test it

Click Add to cart on a product page and on the shop page. Both should take you to checkout with the product in the order summary.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only