0

add_filter( 'woocommerce_package_rates', 'bbloomer_hide_free_shipping_for_shipping_class', 9999, 2 );
function bbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) {
   $shipping_class_target = 546; // shipping class ID (to find it, see screenshot below)
   $in_cart = false;
   $in_cart_non_free = false;
   foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
      if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
         $in_cart = true;
         break;
      } 
   }
   foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
      if ( $values[ 'data' ]->get_shipping_class_id() != $shipping_class_target ) {
         $in_cart_non_free = true;
         break;
      } 
   }
   if ( $in_cart && $in_cart_non_free || $in_cart_non_free ) {
      unset( $rates['flat_rate:19'] ); 
      unset( $rates['free_shipping:22'] ); // shipping method with ID (to find it, see screenshot below)
   }
   return $rates;
}

Jagdish Sarma Asked question September 12, 2023
Add a Comment