Posted on

WooCommerce: Redirect to Checkout on Add to Cart

A Business Bloomer fan asked me a specific question via the blog comments, so I had to make a new snippet of course ๐Ÿ™‚

Here’s how you force redirection to the checkout every time a product is added to cart, no matter if you do so from the loop/shop pages or the single product page.

Force Redirection to WooCommerce Checkout on Add to Cart

1. WooCommerce Settings to allow for Redirect to Checkout on Add to Cart

Go to WooCommerce > Settings > Products > Display and disable both options: “Redirect to the cart page after successful addition” & “Enable AJAX add to cart buttons on archives”:

WooCommerce Settings to Allow for Checkout Redirect

2. PHP Snippet: Redirect to Checkout on Add to Cart – WooCommerce


/**
 * @snippet       Redirect to Checkout Upon Add to Cart - WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=21607
 * @author        Rodolfo Melogli
 * @compatible    WC 3.3.3
 */

function bbloomer_redirect_checkout_add_cart( $url ) {
	$url = get_permalink( get_option( 'woocommerce_checkout_page_id' ) ); 
	return $url;
}

add_filter( 'woocommerce_add_to_cart_redirect', 'bbloomer_redirect_checkout_add_cart' );

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

36 thoughts on “WooCommerce: Redirect to Checkout on Add to Cart

  1. Awesome! Really Helpful, greeetings from Argentina…

    1. Genial!

  2. Hello Rodolfo,
    thanks for your blog. You saved me a lot of time when I started to customize my first WordPress/WooCommerce site. I hopefully can give back a bit.

    Another way to get the checkout url is the function wc_get_checkout_url(). Then the snippet looks like this:

     
    function my_add_to_cart_redirect() {
        $checkout_url = wc_get_checkout_url();
        return $checkout_url;
    }
    add_filter('woocommerce_add_to_cart_redirect', 'my_add_to_cart_redirect', 10, 1 );
    

    This version should be version independent.

    1. Olaf – thanks so much for your comment! Excellent feedback ๐Ÿ™‚

  3. Hi Rodolfo,

    Thanks a lot for it!.

    It worked fine on desktop adding a product to cart from the product page, but failed to redirect to the checkout page on mobile environment (no errors, just stays on the product page)

    What else do i have to look?

    I’m using atelier theme

    Thanks in advance

    Agustin

    1. Hey Agustin, thanks for your comment! Try clearing your phone’s browser cache/cookies ๐Ÿ™‚

  4. Hi,

    How can I redirect specific product to checkout page on click on add to cart button.

    1. Hi Naveed, thanks for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know ๐Ÿ™‚

  5. Hi,

    This is exactly what I need but is there a way to use this snippit on a custom Elementor sales page?
    Does it just copy directly into the custom CSS?

    1. Hey Canneton, this should apply to every add to cart on your site. Just place this in your functions.php

  6. woow awesome

  7. worked 3/16/2018

  8. working fine only thing that Facebook pixel doesnt track “add to cart” only skip and track checkout.

    1. Hey Mike, maybe you can delay the redirect so that Facebook pixel fires ๐Ÿ™‚

  9. No longer working after latest update. Please suggest workaround?

    1. Leland, it works perfect on Woo 3.3.3. Try to see if there’s something else that is causing that not to work ๐Ÿ™‚

  10. Like a charm.
    WC 3.3.1
    WP 4.9.4
    php 7.0.27

  11. not work for me
    woocommerce 3.3.1
    PHP version: 7.0.27

    1. Hi there, thanks for your comment! I just tested this again with 3.3.1 and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

      Also, take a look at this tutorial to see how to troubleshoot: https://businessbloomer.com/woocommerce-troubleshooting-mistakes-to-avoid/

      Finally, can you try switching temporarily to “Twentyseventeen” or “Storefront” theme and let me know if it works?

      Hope this helps!

      R

  12. Hello,
    I want to re-direct users to checkout when user click on ‘Add to cart’. i added your code in functions.php and also un-checked ajax checkbox of cart. But it’s not redirect the user to checkout page

    1. agan, thanks so much for your comment! I just retested this on the latest version of WooCommerce and it still works. Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R

  13. Hello, what if i just want to re-direct users to the checkout for a single product? but keep all the other products as add to cart? Do you have code for this? Perhaps adding the product ID somewhere in the code in Functions.php?

  14. This was AWESOME! It worked PERFECTLY and no plugins required! Thank you

    1. Excellent, thanks Dustin ๐Ÿ™‚

  15. You rock – thanks!

    1. Thank you Ben ๐Ÿ™‚

  16. Hello Rodolfo,

    You do an awesome job with all your snippets. I bet you help a lot of people and safe time for them. (even if they don’t respond).
    This snippet works perfectly, except in one situation. When I enable the “Sold Individually” checkbox to limit the product to one per order. People don’t get redirect anymore when they try to add the product for the second time.
    Is there a solution for this?

    Kind Regards,
    Joke

    1. Hey Joke, thanks for your message! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here on the blog. Thanks a lot for your understanding! ~R

  17. Hi Rodolfo,
    This is a great little snippet.
    Can it work with just one product?

    1. Hey Steve thanks for your comment! Yes, of course, that can be done with conditional logic. I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know ๐Ÿ™‚

  18. Hi Rodolfo,
    I’ve added your Return to Checkout function and Rename Tab function to my child theme functions.php and nothing is showing up. How do I troubleshoot this? It is on a development site, not live site.

    1. Hello Liz, thanks for your comment! Are your problems related to this snippet?

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.