How to prevent same product id can't add in a cart while another product id add in cart list Woocommerce before hook .
Asked
Active
Viewed 319 times
0
-
Please show us what you have tried till now. – Alice Mar 07 '18 at 12:53
-
Please [edit] your question to show [the code you have so far](http://whathaveyoutried.com). You should include at least an outline (but preferably a [mcve]) of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Mar 07 '18 at 12:56
-
Possible duplicate of [WooCommerce - Check if item's are already in cart](https://stackoverflow.com/questions/41262426/woocommerce-check-if-items-are-already-in-cart) – Andrew Schultz Mar 07 '18 at 13:07
-
This question is just not understandable… Please try to better explain it better and clarify it. – LoicTheAztec Mar 07 '18 at 14:05
1 Answers
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;
}

Abdhesh Kumar
- 1
- 3