Cart / Checkout: Autoship Free Shipping on Orders over $50
In this example the merchant wants to only offer the Autoship Free Shipping option at checkout, when there are Autoship Items in the Cart and the Cart total is $50 or more.
Example:
/** * Calculate the total amount in cart for Autoship Items. * @param array $cart Optional. The cart to process. * @return float The total $ amount */ function xx_calculate_cart_autoship_total( $cart = NULL ){ $autoship_total = 0; $cart = isset( $cart ) ? $cart : WC()->cart->get_cart(); // Get the cart content total for Autoship products foreach( $cart as $cart_item ){ // Sum total autoship amount if ( isset( $cart_item['autoship_frequency_type'] ) && isset( $cart_item['autoship_frequency'] ) ) { $autoship_total += $cart_item['line_total']; } } return $autoship_total; } /** * Adjust the available Shipping Rates by removing Autoship Free Shipping * when the total autoship amount is less then $50 * * @param array An array of Available WC_Shipping_Rate Objects. * @param array $package Package of cart items. * @return array The filtered array of WC_Shipping_Rate Objects */ function xx_disable_free_shipping_on_autoship_amount( $rates, $package ){ // Get the Autoship Total $autoship_total = xx_calculate_cart_autoship_total(); // Disabling Autoship Free Shipping Method based on Autoship Amount if( $autoship_total < 50 ){ foreach ( $rates as $rate_key => $rate ){ if( 'autoship_free_shipping' == $rate->method_id ) unset( $rates[ $rate_key ] ); } } return $rates; } add_filter('woocommerce_package_rates', 'xx_disable_free_shipping_on_autoship_amount', 10, 2 );
Now, the free shipping option only displays when the $50 minimum subtotal is reached.