Advertisement
Barbareshet

remove shipping methods for specific product category

Apr 10th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.49 KB | None | 0 0
  1. // a function to check if the cart has product from CATEGORY and it's subcategory id
  2. function cart_has_product_from_CATEGORY() {
  3.     //Check to see if user has product in cart
  4.     global $woocommerce;
  5.     //assigns a default negative value
  6.     $product_in_cart = false;
  7.  
  8.     // start of the loop that fetches the cart items
  9.     foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
  10.  
  11.         $_product = $values['data'];
  12.         $terms = get_the_terms( $_product->id, 'product_cat' );
  13.         // second level loop search, in case some items have several categories
  14.         if($terms){
  15.             foreach ($terms as $term) {
  16.                 $_categoryid = $term->term_id;
  17.                    
  18.                 if (( $_categoryid === PRODUCT_CAT_ID) ) {
  19.                     //category is in cart!
  20.                     $product_in_cart = true;
  21.  
  22.                 }
  23.             }
  24.  
  25.         }
  26.     }
  27.     return $product_in_cart;
  28. }
  29.  
  30. // add filter and function to hide method
  31. add_filter( 'woocommerce_available_shipping_methods', 'custom_shipping_methods' , 10, 1 );
  32. add_filter( 'woocommerce_package_rates', 'custom_shipping_methods', 100, 2 );
  33.  
  34.  
  35. function custom_shipping_methods( $rates, $package ){
  36.  
  37.     // Define/replace here your correct category slug (!)
  38.     $cat_slug = PRODUCT_CAT_SLUG;
  39.     $prod_cat = false;
  40.  
  41.     // Going through each item in cart to see if there is anyone of your category
  42.     foreach ( WC()->cart->get_cart() as $values ) {
  43.         $product = $values['data'];
  44.  
  45.         // compatibility with WC +3
  46.         $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
  47.  
  48.         if ( has_term( $cat_slug, 'product_cat', $product_id ) )
  49.             $prod_cat = true;
  50.     }
  51.  
  52.     $rates_arr = array();
  53.  
  54.     if ( $prod_cat ) {
  55.         foreach( $rates as $rate_id => $rate ) {
  56.             //change to live store IDs
  57.             if ( FLAT_RATE_METHOD_ID === $rate->instance_id || FLAT_RATE_METHOD_ID  === $rate->instance_id ) {
  58.                 $rates_arr[ $rate_id ] = $rate;
  59.  
  60.             }
  61.         }
  62.     }
  63.     return !empty( $rates_arr ) ? $rates_arr : $rates;
  64.  
  65. }
  66. //Notify the client item can't be sent any other way
  67. add_action( 'woocommerce_before_checkout_form' , 'ib_product_notices' );
  68. add_action('woocommerce_before_cart_table','ib_product_notices');
  69.  
  70.  
  71. function ib_product_notices() {
  72.     global $woocommerce;
  73.     if( cart_has_product_from_CATEGORY()){
  74.  
  75.         wc_print_notice( "NOTICE HERE", $notice_type = 'notice' );
  76.  
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement