Last active
September 29, 2017 07:20
-
-
Save bluvertigo/f59c0ea69fcc5857aeff to your computer and use it in GitHub Desktop.
Woocommerce - Nascondere dal carrello la selezione delle spese di spedizione
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Nasconde le spese di spedizione, lasciando il ritiro in sede e le spese gratuite | |
| * | |
| * @param array $available_methods | |
| */ | |
| function hide_all_shipping_when_free_is_available( $rates, $package ) { | |
| $excluded_states = array( 'AK','HI','GU','PR' ); | |
| if( isset( $rates['local_pickup'] ) ) { | |
| $local_pickup = array(); | |
| $local_pickup = $rates['local_pickup']; | |
| } | |
| 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; | |
| if ( isset( $local_pickup ) ) { | |
| $rates[] = $local_pickup; | |
| } | |
| 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment