I have 3 categories "other", "category D" and "category T" :
• If the user has in his basket products belonging to the 3 categories, the payment is accepted.
• If the user has in his basket products belonging to “category D” and to “other”, payment is accepted.
• If the user only has products from one category, payment is accepted
• If the user has in his basket products belonging to “category T” and “category D”, the payment is refused.
• If the basket is empty, no error message should appear.
To summarize, payment should only be refused if the user has "category D" and "category T" products in his basket.
Here is my code :
function verifierproduitpanier(){
$cat_is_catD = false;
foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item){
$product = $cart_item['data'];
if (has_term('categorieD', 'product_cat',$product->id)) {
$cat_is_catD = true;
}
$cat_is_catT = false;
if (has_term('categorieT', 'product_cat',$product->id)) {
$cat_is_catT = true;
}
$cat_is_other = false;
if (has_term('autre', 'product_cat',$product->id)) {
$cat_is_other = true;
}
}
if ($cat_is_catD && $cat_is_catT && !$cart_is_other){
wc_add_notice(sprintf('<p>Les produits sélectionnés ne sont pas disponibles</p>'), 'error');
}
}
add_action( 'woocommerce_check_cart_items', 'verifierproduitpanier' );
When I have items belonging to "Category D" and "Category T" I get an error message that pops up but the problem is when I remove a product from "Category D" and I click on the "cancel" button the error message no longer appears and the payment is accepted.
Any help?