In my shop I have my products with a checkbox before the "add to cart" button. If the customer check this checkbox and add to cart, the value is send to the cart session.
The code of my checkbox :
<input type="checkbox" id="check" name="check" value="colissimo">
To output my checkbox value I use this in cart.php
:
$cart_item['check'];
I want create a function to checkin mini-cart if there is a product with that checkbox.
Ex situation 1 :
The customer add to cart the product with the checkbox checked. The customer go to another product. In the product they have a checkbox but the customer not checked and add to cart.
The cart return false because it's not checked.
Ex situation 2 : The customer add to cart the product with the checkbox NOT checked. The customer go to another product. In the product they have a checkbox but the customer check the chebox and add to cart.
The cart return false because it's checked.
I am trying to achieve this with the code below but it doesn't work:
function is_product_the_same_cat($valid, $product_id, $quantity) {
global $woocommerce;
if($woocommerce->cart->cart_contents_count == 0){
return true;
}
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$items = $woocommerce->cart->get_cart();
$target_terms = $woocommerce->cart->get_cart();
foreach ($items as $item) {
$cat_ids[] = $cart_item['check'];
}
foreach ($target_terms as $term) {
$target_cat_ids[] = $cart_item['check'];
}
}
$same_cat = array_intersect($cat_ids, $target_cat_ids);
print_r ($same_cat);
if(count($same_cat) > 0) return $valid;
else {
wc_add_notice( 'This product have not the same parameter!', 'error' );
return false;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);
Thank's for your help.