Advertisement
vapvarun

Prohibit a specific product to specific states

Oct 5th, 2019
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.99 KB | None | 0 0
  1. /**
  2.  * Prohibit a specific product to specific states
  3.  */
  4. function custom_add_validation_rules_products() {
  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_products = array( 188, 189 ); // Add prohibited product IDs here
  15.     $prohibited_states   = array( 'HI', 'AK' ); // Add prohibited states here
  16.  
  17.     // Invalidate when trying to ship a specific products to prohibited states
  18.     if ( 'US' == $shipping_country && in_array( $shipping_state, $prohibited_states ) && array_intersect( $prohibited_products, $cart_product_ids ) ) {
  19.         wc_add_notice( __( 'There are some products in your cart that cannot be shipped to your location.' ), 'error' );
  20.     }
  21.  
  22. }
  23. add_action( 'woocommerce_checkout_process', 'custom_add_validation_rules_products' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement