Cart / Checkout: Free Shipping Based on Frequency and Type

Conditionally remove Free Shipping as an option in the cart/checkout based on the values for Frequency and Frequency Type.

Important: Always test customizations to your site on a staging environment before making changes to your live / production site.  If you are not experienced with testing changes on a staging site, here is a good article to review.

* Removes Free Shipping Based On Frequency & Type requirement
 * For example this function requires the cart to have at least one.
 * item with a Frequency of 2 and Type of Months ( i.e. Every 2 Months )
 *
 * @param array $rates Array of shipping rates found for the package.
 * @return array Filtered Shipping rates
 */
function xx_remove_free_shipping_conditional( $rates ){

    $cart = WC()->cart;
  
    // Don't bother if empty
      if ( empty( $cart ) )
      return $rates;
  
    // Loop through the cart checking for Free Shipping Autoship frequency & Type.
      $free_shipping = false;
      foreach ( $cart->get_cart() as $item ) {
  
      $free_shipping = 
        ( isset( $item['autoship_frequency'] ) && ( $item['autoship_frequency'] == 2 ) &&
          isset( $item['autoship_frequency_type'] ) && ( 'Months' == $item['autoship_frequency_type'] )
        );
  
    }
  
    // If the cart meets the requirements then they get free shipping.
    if ( $free_shipping )
    return $rates;
  
    // They don't qualify so remove it.
    foreach ( $rates as $rate_id => $rate ) {
      if ( 'autoship_free_shipping' === $rate->method_id ) {
        unset( $rates[ $rate_id ] );
        break;
      }
    }
  
    return $rates;
  
  }
  add_filter( 'woocommerce_package_rates', 'xx_remove_free_shipping_conditional', 101 , 1 );