Quando è disponibile la spedizione gratuita nel nostro sito ecommerce realizzato con il plugin Woocommerce, molto spesso possiamo riscontrare che nel carrello rimangono visibili e selezionabili anche gli altri metodi di spedizione, con conseguente rischio di confondere i nostri clienti.

Diventa quindi necessario impostare una modifica per nascondere altre spedizioni con SPEDIZIONE GRATUITA disponibile.

Il codice che segue andrebbe implementato nel file functions.php del nostro tema child:

Per WooCommerce 2.6+

add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available_all_zones', 10, 2 );

function hide_shipping_when_free_is_available_all_zones( $rates, $package ) {

    $all_free_rates = array();

        foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $all_free_rates[ $rate_id ] = $rate;
            break;
        }
    }

    if ( empty( $all_free_rates )) {
        return $rates;
        } else {
        return $all_free_rates;
        }
}

Per WooCommerce 2.1-2.5

/**
 * per woocommerce 2.1-2.5
 */
 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;
 }



Mostrare solo la SPEDIZIONE GRATUITA in tutti gli Stati tranne che in alcuni…

Qualora volessimo impostare la spedizione gratuita in tutti gli Stati, ma nasconderla solo in alcuni, dovremo implementare quanto segue:

 

/**
* 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;
}

In questo codice occorre modificare ‘AK’,’HI’,’GU’,’PR’ inserendo i codici dei paesi che non hanno la spedizione gratuita.