WooCommerce redirect to checkout after adding product to cart

WordPress AI Code Robot

Create WordPress code snippets for free without a developer!

Neil requested the  following code from my WordPress AI Code Generator.

WooCommerce redirect to checkout after adding product to cart

The robot generated the following code:

Here's a WordPress PHP code snippet illustrating how you could accomplish this:

“`php
function nm_WooCommerce_add_to_cart_redirect() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'nm_WooCommerce_add_to_cart_redirect' );
“`

What this code snippet does is to filter the URL to which WooCommerce redirects after a product is added to cart. The `nm_WooCommerce_add_to_cart_redirect()` function returns the checkout URL, so users will automatically get redirected to checkout after they add a product to their cart.

More
CODE SNIPPETS