如何防止同一产品ID无法添加到购物车中而另一个产品ID添加到购物车列表Woocommerce之前挂钩。
答案 0 :(得分:0)
Here is the code to solve same product id can't add twice in a cart while different product id added in cart may help you.....Not sure
add_action('woocommerce_before_cart', 'bbloomer_find_product_in_cart');
function bbloomer_find_product_in_cart($product_id) {
global $woocommerce;
foreach($woocommerce->cart->get_cart() as $key => $val ) {
$_product = $val['data'];
if($product_id == $_product->id ) {
$notice = 'Product ID ' . $product_id . 'Allready is in the Cart choose another product!';
wc_print_notice( $notice, 'notice' );
return false;
}
}
return true;
}