Woocommerce add to cart button redirect to checkou

2019-01-14 15:16发布

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.

8条回答
乱世女痞
2楼-- · 2019-01-14 15:56

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楼-- · 2019-01-14 16:01

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 
  }
}
查看更多
乱世女痞
4楼-- · 2019-01-14 16:02

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.

查看更多
迷人小祖宗
5楼-- · 2019-01-14 16:04

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

查看更多
可以哭但决不认输i
6楼-- · 2019-01-14 16:05

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();
} );
查看更多
手持菜刀,她持情操
7楼-- · 2019-01-14 16:07

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

查看更多
登录 后发表回答