如何删除 woocommerce 添加的购物车项目并重定向到结帐?
How to remove woocommerce added cart items and redirect to checkout?
我有一个 Woocommerce 表格 'Add Funds'。它有一个金额输入字段($20、$30 ...等)和一个提交按钮,该按钮重定向到购物车页面,输入金额为总金额。
重定向到结账功能正常,但如果用户放弃购物车并再次尝试订购,购物车商品不会被移除。
我尝试了多种重定向到结帐的解决方案,但只有一个有效。
重定向到结帐的工作解决方案:
无法重定向到结帐的解决方案:
Woocommerce add to cart button redirect to checkout
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.
如果在添加新产品之前清空购物车,none 的解决方案有效:
https://gist.github.com/viniciusrtf/b49403b5f87dcd7699c1
https://hungred.com/how-to/empty-woocommerce-cart-adding-item/
使用 Woocommerce 3.2.6 和 WordPress 4.9.2
First you will need in WooCommerce Settings > Products > Display in "Add to cart behaviour" to enabled the checkbox : Redirect to the cart page after successful addition
那么你将需要以下3个钩子函数:
1) 添加到购物车前清空购物车(如果购物车不为空)
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) 添加到购物车重定向到结帐:
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) 跳过购物车页面重定向到结帐:
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 文件。
已测试并有效 (在 Storefront 主题上使用 Woocommerce 3.2.6 和 WordPress 4.9.2)。
我有一个 Woocommerce 表格 'Add Funds'。它有一个金额输入字段($20、$30 ...等)和一个提交按钮,该按钮重定向到购物车页面,输入金额为总金额。
重定向到结账功能正常,但如果用户放弃购物车并再次尝试订购,购物车商品不会被移除。
我尝试了多种重定向到结帐的解决方案,但只有一个有效。
重定向到结帐的工作解决方案:
无法重定向到结帐的解决方案:
Woocommerce add to cart button redirect to checkout
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.
如果在添加新产品之前清空购物车,none 的解决方案有效:
https://gist.github.com/viniciusrtf/b49403b5f87dcd7699c1
https://hungred.com/how-to/empty-woocommerce-cart-adding-item/
使用 Woocommerce 3.2.6 和 WordPress 4.9.2
First you will need in WooCommerce Settings > Products > Display in "Add to cart behaviour" to enabled the checkbox : Redirect to the cart page after successful addition
那么你将需要以下3个钩子函数:
1) 添加到购物车前清空购物车(如果购物车不为空)
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) 添加到购物车重定向到结帐:
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) 跳过购物车页面重定向到结帐:
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 文件。
已测试并有效 (在 Storefront 主题上使用 Woocommerce 3.2.6 和 WordPress 4.9.2)。