php Woocommerce 添加到购物车按钮重定向到结帐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15592633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:31:45  来源:igfitidea点击:

Woocommerce add to cart button redirect to checkout

phpwordpresswordpress-pluginwoocommerce

提问by Michael Villeneuve

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.

我使用插件 woocommerce 创建了一个电子商务。我只出售订阅,所以“/cart/”页面没用。我试图摆脱它,以便当我的客户单击“添加到购物车”按钮时,他最终会出现在结帐页面上。

回答by Ewout

you can use a filter in functions.php:

你可以在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

它似乎不适用于 ajax,但它适用于单个产品页面,我认为这就是您使用的

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

在最新版本的 WooCommerce (>= 2.1) 上,该功能可以简化为:

function redirect_to_checkout() {
    return WC()->cart->get_checkout_url();
}

回答by Devin Walker

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

WooCommerce 设置中有一个选项可让您启用此功能:

Option to redirect visitor to cart page

将访问者重定向到购物车页面的选项

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

只需登录您的 WP 管理面板 > WooCommerce > 目录并选择该选项。我希望这有帮助!

回答by Shir Gans

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).
  1. 正如@Ewout 所提到的,选中“成功添加后重定向到购物车页面”的框。
  2. Woocommerce > 设置 > 结帐(选项卡) - 您应该在其中选择购物车和结帐页面,选择结帐页面作为购物车页面(附图片)。

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

就是这样。为我工作。 在此处输入图片说明

回答by ATutorMe

@RemiCorson posted this brief but beneficial tutorial:

@RemiCorson 发布了这个简短但有益的教程:

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

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

He mentions the same filter as @Ewout above,

他提到了与上面@Ewout 相同的过滤器,

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;

}

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:

但是一行代码很突出,对我当前的 woocommerce 项目来说非常有价值:

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

用户可以使用直接链接自动绕过产品页面。 http://your-site.com/?add-to-cart=37

'37' will be replaced by your product ID.

“37”将替换为您的产品 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.

这对我来说很有用,可以消除不必要的步骤,让用户直接从主页和其他非 woocommerce 页面/帖子结账。

回答by Misha Rudrastyh

Update for WooCommerce 3.5.1

WooCommerce 3.5.1 更新

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

第 1 步。首先转到 WooCommerce 产品设置并停用 AJAX 添加到购物车。

Step 2. Use woocommerce_add_to_cart_redirecthook to make a redirect to checkout.

步骤 2. 使用woocommerce_add_to_cart_redirect钩子重定向到结帐。

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

当然还有一些小事情要做,比如更改添加到购物车按钮的文本和删除一些与 WooCommerce 购物车相关的通知。我建议查看本教程了解更多https://rudrastyh.com/woocommerce/redirect-to-checkout-skip-cart.html

回答by Christian Lescuyer

Filter add_to_cart_redirectis deprecated in WooCommerce 2.6. Use woocommerce_add_to_cart_redirectinstead.

WooCommerce 2.6 中不推荐使用过滤器add_to_cart_redirect。请改用woocommerce_add_to_cart_redirect

Add this to your functions.php :

将此添加到您的 functions.php :

add_filter ('woocommerce_add_to_cart_redirect', function() {
  return WC()->cart->get_checkout_url();
} );

回答by jedi

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:

在商店页面上,如果您想使用 ajax 和重定向在一起。第二种方法仅在某些条件下,您可以使用此过滤器并启用 Woocommerce 设置 ajax:

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

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

删除类属性ajax_add_to_cart并将 href 值更改为结帐 url 页面;

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="([^"]*)"(.*)>/','<ahref="'.$woocommerce->cart->get_checkout_url().'">', $text);
        }
    }

    return $text;
}

I hope that this help.

我希望这有帮助。

回答by nikksan

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_to_cart_redirect 在每个页面上触发,不仅仅是在购物车上。我对建议的答案做了一些修改。

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 
  }
}