>> How can we also target a specific category to apply this code to all products in that specific category?
For this, you need to replace the first section of the above code with the below one:
function disable_add_to_cart_till_required_fields_filled(){
if(!is_product()){
return;
}
global $product;
$product_id = $product->get_id();
$allowed_category = array(19, 20, 21); // Update category ID
$product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));
if(empty(array_intersect($allowed_category, $product_categories))){
return;
}
?>
>> Is there a new category array that you can add with an && operator alongside the product array?
You need to replace the first section of the code with the below one:
function disable_add_to_cart_till_required_fields_filled(){
if(!is_product()){
return;
}
$allowed = array(150, 152); // Update product id
global $product;
$product_id = $product->get_id();
if(! in_array($product_id, $allowed)){
return;
}
$allowed_category = array(19, 20, 21); // Update category ID
$product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));
if(empty(array_intersect($allowed_category, $product_categories))){
return;
}
?>
We hope this will help.
Thank you!