只允许在 Woocommerce 3 中购买一件商品
Only allow to purchase one Item In Woocommerce 3
有没有办法阻止在 WooCommerce 中购买一件以上的商品,或者阻止将一件以上的商品添加到购物车?
我有不同的产品,但我想每次结账只允许一件商品。
我试图搜索解决方案,但那些现有的解决方案无法正常工作,假设当用户未登录并将商品添加到购物车,然后去结账并在那里登录之前添加的商品时客户登录也加起来就是刚刚添加的一个客户,所以现在购物车里有 2 个产品,这里的问题是我使用的代码无法正常工作。
function woo_custom_add_to_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );
已更新 (在您的评论中提出了第二个备选方案)。
下面的代码会将添加到购物车的商品限制为在添加多个商品时显示错误消息的唯一商品。第二个函数将检查购物车中的商品,避免结账,并在有多个商品时添加错误消息:
// Allowing adding only one unique item to cart and displaying an error message
add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation', 10, 1 );
function add_to_cart_validation( $passed ) {
if( ! WC()->cart->is_empty() ){
wc_add_notice( __("You can add only one item to cart", "woocommerce" ), 'error' );
$passed = false;
}
return $passed;
}
// Avoiding checkout when there is more than one item and displaying an error message
add_action( 'woocommerce_check_cart_items', 'check_cart_items' ); // Cart and Checkout
function check_cart_items() {
if( sizeof( WC()->cart->get_cart() ) > 1 ){
// Display an error message
wc_add_notice( __("More than one items in cart is not allowed to checkout", "woocommece"), 'error' );
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
1) 当尝试将第二件商品添加到购物车时:
2) 如果购物车中有不止一件商品:
3) 在结帐时 你会得到一个空白页面,上面有同样的错误提示:
只允许一个购物车项目删除在任何情况下都有效的任何其他项目:
// Removing on add to cart if an item is already in cart
add_filter( 'woocommerce_add_cart_item_data', 'remove_before_add_to_cart' );
function remove_before_add_to_cart( $cart_item_data ) {
WC()->cart->empty_cart();
return $cart_item_data;
}
// Removing one item on cart item check if there is more than 1 item in cart
add_action( 'template_redirect', 'checking_cart_items' ); // Cart and Checkout
function checking_cart_items() {
if( sizeof( WC()->cart->get_cart() ) > 1 ){
$cart_items_keys = array_keys(WC()->cart->get_cart());
WC()->cart->remove_cart_item($cart_items_keys[0]);
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
有没有办法阻止在 WooCommerce 中购买一件以上的商品,或者阻止将一件以上的商品添加到购物车?
我有不同的产品,但我想每次结账只允许一件商品。
我试图搜索解决方案,但那些现有的解决方案无法正常工作,假设当用户未登录并将商品添加到购物车,然后去结账并在那里登录之前添加的商品时客户登录也加起来就是刚刚添加的一个客户,所以现在购物车里有 2 个产品,这里的问题是我使用的代码无法正常工作。
function woo_custom_add_to_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );
已更新 (在您的评论中提出了第二个备选方案)。
下面的代码会将添加到购物车的商品限制为在添加多个商品时显示错误消息的唯一商品。第二个函数将检查购物车中的商品,避免结账,并在有多个商品时添加错误消息:
// Allowing adding only one unique item to cart and displaying an error message
add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation', 10, 1 );
function add_to_cart_validation( $passed ) {
if( ! WC()->cart->is_empty() ){
wc_add_notice( __("You can add only one item to cart", "woocommerce" ), 'error' );
$passed = false;
}
return $passed;
}
// Avoiding checkout when there is more than one item and displaying an error message
add_action( 'woocommerce_check_cart_items', 'check_cart_items' ); // Cart and Checkout
function check_cart_items() {
if( sizeof( WC()->cart->get_cart() ) > 1 ){
// Display an error message
wc_add_notice( __("More than one items in cart is not allowed to checkout", "woocommece"), 'error' );
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
1) 当尝试将第二件商品添加到购物车时:
2) 如果购物车中有不止一件商品:
3) 在结帐时 你会得到一个空白页面,上面有同样的错误提示:
只允许一个购物车项目删除在任何情况下都有效的任何其他项目:
// Removing on add to cart if an item is already in cart
add_filter( 'woocommerce_add_cart_item_data', 'remove_before_add_to_cart' );
function remove_before_add_to_cart( $cart_item_data ) {
WC()->cart->empty_cart();
return $cart_item_data;
}
// Removing one item on cart item check if there is more than 1 item in cart
add_action( 'template_redirect', 'checking_cart_items' ); // Cart and Checkout
function checking_cart_items() {
if( sizeof( WC()->cart->get_cart() ) > 1 ){
$cart_items_keys = array_keys(WC()->cart->get_cart());
WC()->cart->remove_cart_item($cart_items_keys[0]);
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。