Sunday 12 August 2018

Redirect to Checkout / custom page on Add to Cart button click – WooCommerce

There may be time when client would ask you to redirect user to checkout or cart page directly when users click on add to cart button.

For the similar situations WooCommerce provide predefined settings and filter hook which you can use to achieve the results.

To redirect the users, you can follow below steps:
1) I believe you have WordPress and WooCommerce already setup, now you can go to WooCommerce > Settings > Products tab.
2) Now look for "Add to cart behaviour" and there you will find the two settings: first is "Redirect to the cart page after successful addition" and second would be "Enable AJAX add to cart buttons on archives".
3) Now suppose if you want no redirection and want user to stay on the page without page reload, you can select the second checkbox "Enable AJAX add to cart buttons on archives" and you are done.
4) If you want users to redirect to cart page after user click on add to cart you can select the first option "Redirect to the cart page after successful addition" and you are done.
5) And if you have requirement to sent the user to checkout/custom page after user click on add to cart button, please uncheck both the options there follow the next step.
6) Now open your theme's functions.php file and copy/paste the below code.
function my_custom_add_to_cart_redirect( $url ) {  
 // for checkout page 
 $url = wc_get_checkout_url(); 
 // for custom/home page in our case.
 // $url = home_url(); 
 return $url;
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

Below is the video demonstration for above mentioned points.

Version at the time of demonstration:
1) WordPress: v4.9.8
2) Localserver: WampServer Version 2.5
3) WooCommerce: v3.4.4
4) Twenty Sixteen Theme: v1.5

Load disqus comments