简体   繁体   English

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

[英]Woocommerce add to cart button redirect to checkout

I created an ecommerce using the plugin woocommerce. 我使用插件woocommerce创建了一个电子商务。 I am selling only a subscription so the "/cart/" page is useless. 我只销售订阅,所以“/ cart /”页面没用。 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. 我正试图摆脱它,以便当我的客户点击“添加到购物车”按钮时,他最终在结帐页面上。

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();
}

There is an option within WooCommerce settings that allows you to enable this functionality: WooCommerce设置中有一个选项,允许您启用此功能:

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

Simply login to your WP admin panel > WooCommerce > Catalog and select the option. 只需登录您的WP管理面板> WooCommerce>目录,然后选择该选项。 I hope this helps! 我希望这有帮助!

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". 如@Ewout所述,选中“成功添加后重定向到购物车页面”框。
  2. Woocommerce > Settings > Checkout (Tab) - where you should select pages for cart and checkout, select the checkout page as the cart page (image attached). Woocommerce>设置>结帐(标签) - 您应该选择购物车和结帐页面,选择结帐页面作为购物车页面(附图像)。

That's it. 而已。 works for me. 适合我。 在此输入图像描述

@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; 

} }

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页面/帖子结账。

Filter add_to_cart_redirect is deprecated in WooCommerce 2.6. 在WooCommerce 2.6中不推荐使用过滤器add_to_cart_redirect Use woocommerce_add_to_cart_redirect instead. 请改用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();
} );

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 Products设置并停用AJAX添加到购物车。

Step 2. Use woocommerce_add_to_cart_redirect hook 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. 当然还有一些小事要做,比如更改添加到购物车按钮文本和删除一些与WooCommerce购物车相关的通知。 I recommend to check this tutorial for more https://rudrastyh.com/woocommerce/redirect-to-checkout-skip-cart.html 我建议查看本教程以获取更多信息https://rudrastyh.com/woocommerce/redirect-to-checkout-skip-cart.html

On shop page, if you want use ajax and redirect toghether. 在商店页面上,如果你想使用ajax并重定向到其他。 The second method only when there are some condition, you can use this filter and leave on Woocommerce setting ajax enabled: 第二种方法只有在有某些条件时,你可以使用这个过滤器并在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_cart and change the href value to checkout url page; 删除类属性ajax_add_to_cart并将href值更改为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. 我希望这有帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在结帐时添加一个按钮以清空购物车并重定向到Woocommerce中的商店页面 - Add a button in checkout to empty cart and redirect to shop page in Woocommerce 其他Woocommerce产品“添加到购物车”按钮,可重定向到结帐 - Additional Woocommerce product Add To Cart button that redirect to checkout 将添加到购物车按钮重定向到 WooCommerce 中 1 个特定登录页面的结帐页面 - Redirect add to cart button to checkout page for 1 specific landing page in WooCommerce 添加到购物车并重定向到 WooCommerce 中可变产品的结帐 - Add to cart and redirect to checkout for variable products in WooCommerce WooCommerce 添加到购物车后重定向到结帐 - WooCommerce after add to cart redirect to checkout 从woocommerce删除添加到购物车按钮,查看购物车,结帐页面 - Removing Add to cart button, View cart, Checkout pages from woocommerce WooCommerce 添加到购物车重定向基于点击的按钮 - WooCommerce add to cart redirect based on button clicked 添加到购物车并重定向到 WooCommerce 中默认可变产品的结帐 - Add to cart and redirect to checkout for default variable products in WooCommerce 重定向到Checkout滚动到WooCommerce中添加到购物车的账单详细信息 - Redirect to Checkout scrolling to billing details on add to cart in WooCommerce Woocommerce添加到购物车重定向到Checkout不适用于具有变体的产品 - Woocommerce Add To Cart redirect to Checkout not working for product with variants
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM