32

I created an ecommerce using the plugin woocommerce. I am selling only a subscription so the "/cart/" page is useless. I'm trying to get rid of it so that when my customer click on "Add to cart" button, he ends up on the checkout page.

9 Answers 9

76

In WooCommerce 3.6 or later you can use woocommerce_add_to_cart_redirect (props @roman)

add_filter ('woocommerce_add_to_cart_redirect', function( $url, $adding_to_cart ) {
    return wc_get_checkout_url();
}, 10, 2 ); 

Original answer:

you can use a filter in functions.php:

add_filter ('add_to_cart_redirect', 'redirect_to_checkout');

function redirect_to_checkout() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    return $checkout_url;
}

it doesn't seem to work with ajax, but it works from the single product pages, which I think is what you use

On WooCommerce (>= 2.1) the function can be simplified as:

function redirect_to_checkout() {
    return WC()->cart->get_checkout_url();
}
9
  • 1
    You are my official new best friend. TYVM Mar 26, 2013 at 21:20
  • 1
    Hi Guys, How can i add that functionality for a specific products category ? Nov 25, 2014 at 11:45
  • @RoeeYossef one way can be to alter the add-to-cart link adding something like : add_query_arg( array( 'direct-checkout-redirect' => 1 ), $_product->add_to_cart_url() ); and then into function redirect_to_checkout(), checking for "direct-checkout-redirect" param. Mar 26, 2015 at 13:54
  • 1
    Caveat - if you turn off Ajax in Settings->Product->Display, the second example still stops in the checkout line. The first example, however, behaves even from a direct link.= ( ?add-to-cart=[PRODUCT-ID]&variation_id=[VARIATION-ID]&attribute_[ATTRIBUTE-NAME]=[ATTRIBUTE-SLUG]&attribute_[ATTRIBUTE-NAME]=[ATTRIBUTE-SLUG] )
    – Imperative
    Mar 27, 2015 at 21:39
  • 1
    The "add_to_cart_redirect" hook uses out of date data structures and is <strong>deprecated</strong> since WooCommerce version 3.1.1! Use woocommerce_add_to_cart_redirect instead.
    – Roman
    Aug 15, 2017 at 6:26
14

There is an option within WooCommerce settings that allows you to enable this functionality:

Option to redirect visitor to cart page

Simply login to your WP admin panel > WooCommerce > Catalog and select the option. I hope this helps!

3
  • 2
    On seconds read it looks like you want to skip the cart page and redirect to the checkout... Mar 24, 2013 at 0:41
  • Exactly, I'm using at the moment this option. But like you just said I'd like to get straight to the checkout. Mar 24, 2013 at 0:52
  • @DevinWalker what if you need to redirect only with few condition and continue to use ajax in the other case?
    – jedi
    Oct 12, 2016 at 14:57
12

I've found a simple solution that work like magic.

  1. As mentioned by @Ewout, check the box that says "Redirecto to cart page after succesful addtion".
  2. Woocommerce > Settings > Checkout (Tab) - where you should select pages for cart and checkout, select the checkout page as the cart page (image attached).

That's it. works for me. enter image description here

4
  • Thanks, I think this is new a new feature of woocomerce because that option wasn't there when I asked the question. I haven't tested it but +1 for the update. Jul 30, 2014 at 18:07
  • 11
    Don't do this - when Woocommerce detects someone visiting the checkout page with an empty cart, it will try and redirect them to the cart page. Because these are one and the same, it will end up in an endless redirect loop.
    – Lewis
    Aug 4, 2014 at 12:22
  • I have the latest version and they have moved all these functions. Can't find redirect anywhere in ui :( Mar 27, 2015 at 1:50
  • I don't understand what Lewis meant. I have followed the instructions above. Then, I went to mydomain.com/checkout/ and I had an expected empty checkout page. Aug 1, 2018 at 15:11
6

Update for WooCommerce 3.5.1

Step 1. First of all go to WooCommerce Products settings and deactivate AJAX add to cart.

Step 2. Use woocommerce_add_to_cart_redirect hook to make a redirect to checkout.

add_filter( 'woocommerce_add_to_cart_redirect', function( $url ) {
    return wc_get_checkout_url();
});

Of course there some small things are left to do, like changing add to cart buttons text and removing some WooCommerce cart-related notices. I recommend to check this tutorial for more https://rudrastyh.com/woocommerce/redirect-to-checkout-skip-cart.html

3

@RemiCorson posted this brief but beneficial tutorial:

http://www.remicorson.com/woocommerce-skip-product-cart-pages/

He mentions the same filter as @Ewout above,

add_filter ('add_to_cart_redirect', 'redirect_to_checkout');

function redirect_to_checkout() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    return $checkout_url;

}

but one line of code stands out and is of super value for me for my current woocommerce project:

There is a direct link that a user can use to automatically bypass the product page. http://your-site.com/?add-to-cart=37

'37' will be replaced by your product ID.

This was useful for me to eliminate unnecessary steps and take users directly to checkout from the home page and other non-woocommerce pages/posts.

2
  • 8
    add_to_cart_redirect is deprecated now, use woocommerce_add_to_cart_redirect instead. Jan 13, 2015 at 16:42
  • 1
    get_checkout_url() is deprecated as of 2.5.0 in favor to wc_get_checkout_url()
    – nu everest
    Jan 10, 2017 at 18:42
1

Filter add_to_cart_redirect is deprecated in WooCommerce 2.6. Use woocommerce_add_to_cart_redirect instead.

Add this to your functions.php :

add_filter ('woocommerce_add_to_cart_redirect', function() {
  return WC()->cart->get_checkout_url();
} );
3
  • 1
    HI @Christian Lescuyer this function works fine, however I would like to know how do I add an additional "Checkout" button next to "Add to cart" and assign it on that button but not globally on ALL "add to cart" buttons throughout the site..any idea?
    – gil hamer
    Jun 28, 2017 at 12:29
  • @gil-hamer You will have to modify the theme. Locate the “Add to cart” button on the page you want, and add your own button. This is not so simple, because you must not modify the main theme (your modifications will be overwritten at the next upgrade). The usual way is to add a child theme. If you already have one, no problem! Jun 28, 2017 at 16:19
  • Thanks @Christian Lescuyer
    – gil hamer
    Jun 28, 2017 at 16:43
1

Try the below code in the themes function.php file

add_filter( 'woocommerce_add_to_cart_redirect', 'woo_skip_cart_redirect_checkout' );
 
function woo_skip_cart_redirect_checkout( $url ) {
    return wc_get_checkout_url();
}
0

On shop page, if you want use ajax and redirect toghether. The second method only when there are some condition, you can use this filter and leave on Woocommerce setting ajax enabled:

add_filter('woocommerce_loop_add_to_cart_link', array( $this, 'add_quantity_input' ), 4, 2); 

to remove on a class attribute ajax_add_to_cart and change the href value to checkout url page;

On my template case:

public function add_quantity_input($text = null, $product = null) {
    global $product, $woocommerce;

    if ( $text != null and $product != null  ) {
        if(ismycondition($product->id)) {
            $s = explode('class="', $text);
            $s[2]=str_replace('ajax_add_to_cart', '', $s[2]);
            $text = implode('class="', $s);

            $text = preg_replace('/<a(.*)href="([^"]*)"(.*)>/','<a$1href="'.$woocommerce->cart->get_checkout_url().'"$3>', $text);
        }
    }

    return $text;
}

I hope that this help.

0

None of the solutions actually worked out for me, the filter add_to_cart_redirect was triggering on every page,not only on the cart.I did some modification on the suggested answer.

add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
  function redirect_to_checkout() {
  if(is_cart()){
    $checkout_url = WC()->cart->get_checkout_url();
  ?>
  <script>
  location = '<?=$checkout_url?>';
  </script>
  <?php 
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.