如何删除woocommerce添加的购物车物品并重定向到结帐? [英] How to remove woocommerce added cart items and redirect to checkout?

查看:107
本文介绍了如何删除woocommerce添加的购物车物品并重定向到结帐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个添加资金"的Woocommerce表格.它有一个金额输入字段($ 20,$ 30 ...等)和一个提交按钮,该按钮重定向到购物车页面,输入金额为总计.

I have a Woocommerce form to 'Add Funds'. It has an amount input field ($20, $30 ...etc.) and a submit button that redirects to cart page with the input amount as total.

重定向到结帐功能有效,但是如果用户放弃购物车并尝试再次订购,则购物车中的物品不会被移除.

Redirect to checkout is working, but the cart items are not getting removed if a user abandons the cart and tries to order again.

我尝试了多种解决方案来重定向到结帐,但只有一个可行.

I tried numerous solutions for the redirect to checkout, but only one worked.

用于重定向到结帐的有效解决方案:

WooCommerce-跳过购物车页面重定向到结账页面

解决方案不适用于重定向到结帐:

https://wordpress.stackexchange.com/questions/267071/在购物车中添加购物车后重定向到woocommerce-checkout-

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

我为重定向添加了工作和不工作的解决方案 进行结帐,因为它可以提供有关为什么空购物车的见解 解决方案不起作用.

N.B. I have added the working and not working solutions for redirect to checkout because it may provide an insight as to why the empty cart solutions are not working.

在添加新产品之前清空购物车的情况下,所有解决方案均无效:

Incase of emptying cart before adding a new product, none of the solutions are working:

https://gist.github.com/viniciusrtf/b49403b5f87dcd7699c1

https://hungred.com/how-to/empty-woocommerce-cart-adding-item/

https://wordpress.stackexchange.com/questions/267071/在购物车中添加购物车后重定向到woocommerce-checkout-

使用Woocommerce 3.2.6和WordPress 4.9.2

推荐答案

首先,您需要在WooCommerce设置>产品>在添加到购物车行为"中显示以启用复选框:添加成功后重定向到购物车页面

然后,您将需要以下3个挂钩函数:

Then you will need the 3 following hooked function:

1)添加到购物车之前,先清空购物车(如果购物车不为空)

1) Empty cart before add-to-cart (if cart is not empty)

add_filter( 'woocommerce_add_to_cart_validation', 'one_cart_item_at_the_time', 10, 3 );
function one_cart_item_at_the_time( $passed, $product_id, $quantity ) {
    if( ! WC()->cart->is_empty())
        WC()->cart->empty_cart();
    return $passed;
}

2)购物车重定向至结帐:

2) Add-to-cart redirection to checkout:

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

3)跳过购物车页面重定向到结帐页面:

3) Skip cart page redirecting to checkout:

add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
function skip_cart_page_redirection_to_checkout() {
    if( is_cart() )
        wp_redirect( wc_get_checkout_url() );
}

代码进入您的活动子主题(或活动主题)的function.php文件中.

经过测试并可以正常工作(在店面主题上使用Woocommerce 3.2.6和WordPress 4.9.2).

Tested and works (with Woocommerce 3.2.6 and WordPress 4.9.2 on Storefront theme).

这篇关于如何删除woocommerce添加的购物车物品并重定向到结帐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆