In this little snippet I’m going to show you a very simple way to redirect your customers to the checkout page on add to cart. There are a few possible scenarios that you might need to use this snippet. For instance, if you are selling avirtual product such as a course, or a downloadable product like an audio file or plugin. There really is no need for buyers to access the cart page, and every time they have to press a button there’s a risk of losing them as a paying customer. So let’s take a look at how simple this really is.
Disable ajax and redirection to cart page
The first step that you need to take is to head to Woocommerce > settings > product and under the general tab make sure both redirect to cart page after successful addition and redirect to cart page are unchecked.
Once both boxes are unchecked as in the image above, click save at the bottom of the page.
Adding the snippet to redirect to checkout page on add to cart
[cc lang=”php”]
/**
* snippet Redirect to checkout page on add to cart for WooCommerce
* sourcecode https://wcsuccessacademy.com/?p=146&
* author John Cook
* tested Woocommerce 3.3.3
*/
add_filter( ‘woocommerce_add_to_cart_redirect’, ‘wcsuccessacademy_redirect_to_checkout_on_add_cart’ );
function wcsuccessacademy_redirect_to_checkout_on_add_cart( $url ) {
$url = get_permalink( get_option( ‘woocommerce_checkout_page_id’ ) );
return $url;
}
[/cc]
Where do you place the code?
Place the code snippet into your theme functions file, child theme functions file or functions plugin. Make sure it is placed before the closing ?> if you have it in your file. If you are uncertain how to place this, please use one of the plugins found in this list.
Did this plugin work for you?
Please leave a comment letting me know if the code snippet showing how to redirect customers to the checkout page on add to cart has worked for you.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Google Scholar ID: https://scholar.google.com.au/citations?user=0KvpwNEAAAAJ&hl=en
ORCID: https://orcid.org/0000-0001-8113-1296
Stay In Touch