我发现此代码有效,但仅用于产品页面,而不检查购物车页面中的数量。
add_filter( 'woocommerce_add_to_cart_validation', 'only_four_items_allowed_add_to_cart', 10, 3 );
function only_four_items_allowed_add_to_cart( $passed, $product_id, $quantity ) {
$cart_items_count = WC()->cart->get_cart_contents_count();
$total_count = $cart_items_count + $quantity;
if(has_term('quantity4','product_cat',$product_id )){
if($cart_items_count >= 4 || $total_count > 4){
// Set to false
$passed = false;
// Display a message
wc_add_notice( __( "You Chose a Box of 4 Items, Can't Add More", "woocommerce" ), "error" );
}
}
else if (has_term('quantity6','product_cat',$product_id )){
if($cart_items_count >= 6 || $total_count > 6){
// Set to false
$passed = false;
// Display a message
wc_add_notice( __( "You Chose a Box of 6 Items, Can't Add More", "woocommerce" ), "error" );
}
}
return $passed;
}
有人可以帮我检查一下我需要在此代码中添加的内容吗,也可以在购物车页面中查看吗?任何帮助将不胜感激。