/**
* Prohibit a product category
*/
function custom_add_validation_rules_category() {
if ( ! isset( WC()->cart ) ) {
return;
}
$shipping_country = WC()->customer->get_shipping_country();
$shipping_state = WC()->customer->get_shipping_state();
$cart_contents = WC()->cart->cart_contents;
$cart_product_ids = wp_list_pluck( $cart_contents, 'product_id' );
$prohibited_categories = array( 34 ); // Add prohibited category IDs
// Loop through each product to check if anyone has any prohitibited category
foreach ( $cart_product_ids as $product_id ) {
if ( array_intersect( wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ), $prohibited_categories ) ) {
wc_add_notice( __( 'There are some product (categories) in your cart that cannot be shipped to your location.' ), 'error' );
break;
}
}
}
add_action( 'woocommerce_checkout_process', 'custom_add_validation_rules_category' );
Comments