Ocultar el resto de gastos de envió, cuando el envió sea gratuito en woocomerce

Parece ser que existe un problema muy común que surge habilitas varios tipos de gastos de envío y habilitas la opción de gasto de envío gratuito cuando cumpla X condiciones.

Pues resulta que cunado se cumple los criterios para que el envio sea gratuito, woocomerce te da muestra una lista con todos los tipos de envíos disponibles, ¡incluido el envío gratuito!

Lo cual parece curioso, si ya es gratis el envio, para que mostrar la lista de los de pago, obviamente ningún cliente quiere pagar gastos de envío.

Y para mas inri, resulta que woocomerce, ofrece una solucion un poco rara, ya que viene ocurriendo desde los inicio de woocomere y es incluir una función dentro del tema hijo en funtions.php

O en su defecto utilizar un plugin de terceros WC Hide Shipping Methods

 

Versión WC 3.0+

/**
 * Hide shipping rates when free shipping is available.
 * Updated to support WooCommerce 2.6 Shipping Zones.
 *
 * @param array $rates Array of rates found for the package.
 * @return array
 */
function my_hide_shipping_when_free_is_available( $rates ) {
 $free = array();
 foreach ( $rates as $rate_id => $rate ) {
 if ( 'free_shipping' === $rate->method_id ) {
 $free[ $rate_id ] = $rate;
 break;
 }
 }
 return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );

Versión WC 2.5

/**
 * woocommerce_package_rates is a 2.1+ hook
 */
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
 
/**
 * Hide shipping rates when free shipping is available
 *
 * @param array $rates Array of rates found for the package
 * @param array $package The package array/object being shipped
 * @return array of modified rates
 */
function hide_shipping_when_free_is_available( $rates, $package ) {
 
 // Only modify rates if free_shipping is present
 if ( isset( $rates['free_shipping'] ) ) {
 
 // To unset a single rate/method, do the following. This example unsets flat_rate shipping
 unset( $rates['flat_rate'] );
 
 // To unset all methods except for free_shipping, do the following
 $free_shipping = $rates['free_shipping'];
 $rates = array();
 $rates['free_shipping'] = $free_shipping;
 }
 
 return $rates;
}

Versión < WC 2.5

/**
 * Hide ALL shipping options when free shipping is available and customer is NOT in certain states
 * Hide Free Shipping if customer IS in those states
 *
 * UPDATED FOR WOOCOMMERCE 2.1
 *
 * Change $excluded_states = array( 'AK','HI','GU','PR' ); to include all the states that DO NOT have free shipping
 */
add_filter( 'woocommerce_package_rates', 'hide_all_shipping_when_free_is_available' , 10, 2 );

/**
 * Hide ALL Shipping option when free shipping is available
 *
 * @param array $available_methods
 */
function hide_all_shipping_when_free_is_available( $rates, $package ) {
 
 $excluded_states = array( 'AK','HI','GU','PR' );
 if( isset( $rates['free_shipping'] ) AND !in_array( WC()->customer->shipping_state, $excluded_states ) ) :
 // Get Free Shipping array into a new array
 $freeshipping = array();
 $freeshipping = $rates['free_shipping'];
 
 // Empty the $available_methods array
 unset( $rates );
 
 // Add Free Shipping back into $avaialble_methods
 $rates = array();
 $rates[] = $freeshipping;
 
 endif;
 
 if( isset( $rates['free_shipping'] ) AND in_array( WC()->customer->shipping_state, $excluded_states ) ) {
 
 // remove free shipping option
 unset( $rates['free_shipping'] );
 
 }

return $rates;
}