Advertisement
vapvarun

Prevent Categories From Being Shipped

Oct 5th, 2019
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.95 KB | None | 0 0
  1. /**
  2.  * Prohibit a product category
  3.  */
  4. function custom_add_validation_rules_category() {
  5.  
  6.     if ( ! isset( WC()->cart ) ) {
  7.         return;
  8.     }
  9.  
  10.     $shipping_country      = WC()->customer->get_shipping_country();
  11.     $shipping_state        = WC()->customer->get_shipping_state();
  12.     $cart_contents         = WC()->cart->cart_contents;
  13.     $cart_product_ids      = wp_list_pluck( $cart_contents, 'product_id' );
  14.     $prohibited_categories = array( 34 ); // Add prohibited category IDs
  15.  
  16.     // Loop through each product to check if anyone has any prohitibited category
  17.     foreach ( $cart_product_ids as $product_id ) {
  18.         if ( array_intersect( wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ), $prohibited_categories ) ) {
  19.             wc_add_notice( __( 'There are some product (categories) in your cart that cannot be shipped to your location.' ), 'error' );
  20.             break;
  21.         }
  22.     }
  23.  
  24. }
  25. add_action( 'woocommerce_checkout_process', 'custom_add_validation_rules_category' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement