WooCommerce is one of the best e-commerce plugin for WordPress. I have used it on couple of website that I built for my clients and setting up e-commerce platform with WooCommerce is super-duper easy. Although the plugin works flawlessly out of the box but there is always room for improvement. Right! So today I am going to share a code snippet, that I used on a project, to redirect WooCommerce user to checkout page after adding a product to cart. This is especially useful snippet for small e-commerce stores having couple of items to sell or if you’re building a landing page with an Add to Cart feature.
WooCommerce provides many useful filter and action hooks that we can use to ‘hook into’ the code for changing the default behaviors of system. Here I used woocommerce_add_to_cart_redirect
hook that fires just after adding a product in cart. I used this hook to force WordPress to redirect WooCommerce user to Checkout Page after add to cart. So here’s a very simple piece of code that you can add to the file functions.php.
// redirect woocommerce user to checkout page after add to cart
function wpcs_add_to_cart_redirect( $url ) {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'wpcs_add_to_cart_redirect' );
Super simple, but it could boost your conversion rate.