title: Unsetting WooCommerce shipping method based on cart items shipping classes
body: <p>I’m trying to unset Flat Rate shipping method only if cart has products both with and without shipping class. If all product in cart have shipping class then it should stay.</p>
  
  <p>Have this shipping method: Flat Rate - (flat_rate1) (instance_id=1) </p>
  
  <p>And these shipping classes: 50, 100 and so on, with same way named Slugs: 50, 100…</p>
  
  <p>Flat Rate shipping method has costs set up for these shipping classes, Main Cost and No shipping class cost for this method are not set, so it only appears for products in cart that have shipping classes set.</p>
  
  <p><strong>Got it working</strong></p>
  
  <pre><code>add_filter( 'woocommerce_package_rates', 'unset_shipping_for_unmatched_items', 100, 2 );
  
  function unset_shipping_for_unmatched_items( $rates, $package ) {
      // Initialisation
      $shipping_classes = array( 50, 100, 150, 200, 250, 300 );
      $cart_items = WC()-&gt;cart-&gt;get_cart();
      $cart_items_count = WC()-&gt;cart-&gt;get_cart_contents_count();
  
      $items_match = false;
  
      $inArray    = 0;
      $notInArray = 0;
  
      foreach( $cart_items as $cart_item ){
          if( in_array( $cart_item[ 'data' ]-&gt;get_shipping_class(), $shipping_classes ) &amp;&amp; $cart_items_count &gt; 1 ) {
              $inArray++;
          } else {
              $notInArray++;
          }
      }
      if( ( $cart_items_count == $notInArray ) || ( $cart_items_count == $inArray ) ){
          $items_match = false;
      } else {
          $items_match = true;
      }
  
      if( $items_match )
          unset( $rates['flat_rate:6'] );
  
      return $rates;
  }
  </code></pre>