如果您想限制客户在Woocommerce购物车中添加单个商品,那么您可以使用过滤器" woocommerce_add_to_cart_validation"。
答案 0 :(得分:3)
过滤器在以下位置定义。
/includes/class-wc-ajax.php
允许在WooCommerce购物车中仅添加单个商品
add_filter( 'woocommerce_add_to_cart_validation', 'restrict_only_one_item_in_cart' );
function restrict_only_one_item_in_cart($cart_item_data) {
global $woocommerce;
$item_count = $woocommerce->cart->cart_contents_count;
if($item_count > 0){
wc_add_notice( 'Sorry. Only one item is allowed.If you would like to add another, please remove the item in the cart.', 'error' );
return false;
}
return $cart_item_data;
}