title: Show or hide shipping methods based on number of cart items for specific shipping class
body: <p>I’m trying to unset two shipping methods only if cart has 4 or less products from a specific shipping class.</p>
  <p>Shipping Methods: flat_rate:20 and flat_rate:21</p>
  <p>Shipping Class: 182</p>
  <p>This is what I have:</p>
  <pre><code>add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
  function hide_shipping_method_based_on_shipping_class( $rates, $package )
  {
      if ( is_admin() &amp;&amp; ! defined( 'DOING_AJAX' ) )
          return;
  
      // Shipping Class To Find
      $class = 182;
  
      // Number Of Shipping Class Items In Cart
      $amount = 4;
  
      // Shipping Methods To Hide
      $method_key_ids = array('flat_rate:20', 'flat_rate:21');
  
      // Checking In Cart Items
      foreach( $package['contents'] as $item ) {
          // If We Find The Shipping Class and Number of Items
          if( $item['data']-&gt;get_shipping_class_id() == $class &amp;&amp; count($package['contents']) &lt;= $amount ){
              foreach( $method_key_ids as $method_key_id ){
                  unset($rates[$method_key_id]); // Remove Targeted Methods
              }
              break; // Stop The Loop
          }
      }
      return $rates;
  }
  </code></pre>
  <p>I would like to combine the above logic with the below logic:</p>
  <pre><code>add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
  function hide_shipping_method_based_on_shipping_class( $rates, $package ) {
      $targeted_class_ids = array(182); // Shipping Class To Find
      $allowed_max_qty    = 4; // Max allowed quantity for the shipping class
      $shipping_rates_ids = array( // Shipping Method rates Ids To Hide
          'wf_shipping_ups:07',
          'wf_shipping_ups:08',
          'wf_shipping_ups:11',
          'wf_shipping_ups:54',
          'wf_shipping_ups:65',
          'wf_shipping_ups:70',
          'wf_shipping_ups:74',
          'free_shipping:2',
          'request_shipping_quote'
      );
      
      $related_total_qty  = 0;
  
      // Checking cart items for current package
      foreach( $package['contents'] as $key =&gt; $cart_item ) {
          if( in_array( $cart_item['data']-&gt;get_shipping_class_id(), $targeted_class_ids ) ){
              $related_total_qty += $cart_item['quantity'];
          }
      }
      
      // When total allowed quantity is more than allowed (for items from defined shipping classes)
      if ( $related_total_qty &gt; $allowed_max_qty ) {
          // Hide related defined shipping methods
          foreach( $shipping_rates_ids as $shipping_rate_id ) {
              if( isset($rates[$shipping_rate_id]) ) {
                  unset($rates[$shipping_rate_id]); // Remove Targeted Methods
              }
          }
      }
      return $rates;
  }
  </code></pre>
  <p>To create the following logic:</p>
  <p><strong>1. If cart has 4 or less products in shipping class 181 unset the following shipping methods:</strong></p>
  <ul>
  <li>'flat_rate:20'</li>
  <li>'flat_rate:21'</li>
  </ul>
  <p><strong>2. If cart has 5 or more products in shipping class 181 unset the following shipping method:</strong></p>
  <ul>
  <li>'wf_shipping_ups:07'</li>
  <li>'wf_shipping_ups:08'</li>
  <li>'wf_shipping_ups:11'</li>
  <li>'wf_shipping_ups:54'</li>
  <li>'wf_shipping_ups:65'</li>
  <li>'wf_shipping_ups:70'</li>
  <li>'wf_shipping_ups:74'</li>
  <li>'free_shipping:2'</li>
  <li>'request_shipping_quote'</li>
  </ul>
  <p>Both codes work if I use them individually. But I get an error when I try to use both concurrently.</p>
  <p><strong>I get the following error:</strong> <em>Cannot redeclare hide_shipping_method_based_on_shipping_class() (previously declared in /functions.php:272)</em></p>