Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Forked from woogist/gist:6065863
Last active May 16, 2019 06:42
Show Gist options
  • Save mikejolley/11171530 to your computer and use it in GitHub Desktop.
Save mikejolley/11171530 to your computer and use it in GitHub Desktop.

Revisions

  1. mikejolley revised this gist Apr 22, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /**
    * woocommerce_package_rates is a 2.1+ hook
    */
    add_filter( 'woocommerce_package_rates', 'hide_all_shipping_when_free_is_available', 10, 2 );
    add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );

    /**
    * Hide shipping rates when free shipping is available
    @@ -10,7 +10,7 @@
    * @param array $package The package array/object being shipped
    * @return array of modified rates
    */
    function hide_all_shipping_when_free_is_available( $rates, $package ) {
    function hide_shipping_when_free_is_available( $rates, $package ) {

    // Only modify rates if free_shipping is present
    if ( isset( $rates['free_shipping'] ) ) {
  2. mikejolley revised this gist Apr 22, 2014. 1 changed file with 26 additions and 24 deletions.
    50 changes: 26 additions & 24 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,28 @@
    // Hide ALL shipping options when free shipping is available
    add_filter( 'woocommerce_available_shipping_methods', 'hide_all_shipping_when_free_is_available' , 10, 1 );

    /**
    * Hide ALL Shipping option when free shipping is available
    *
    * @param array $available_methods
    */
    function hide_all_shipping_when_free_is_available( $available_methods ) {

    if( isset( $available_methods['free_shipping'] ) ) :

    // Get Free Shipping array into a new array
    $freeshipping = array();
    $freeshipping = $available_methods['free_shipping'];

    // Empty the $available_methods array
    unset( $available_methods );

    // Add Free Shipping back into $avaialble_methods
    $available_methods = array();
    $available_methods['free_shipping'] = $freeshipping;
    endif;
    * woocommerce_package_rates is a 2.1+ hook
    */
    add_filter( 'woocommerce_package_rates', 'hide_all_shipping_when_free_is_available', 10, 2 );

    return $available_methods;
    }
    /**
    * 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_all_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;
    }
  3. @woogist woogist revised this gist Dec 17, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -19,8 +19,7 @@ function hide_all_shipping_when_free_is_available( $available_methods ) {

    // Add Free Shipping back into $avaialble_methods
    $available_methods = array();
    $available_methods[] = $freeshipping;

    $available_methods['free_shipping'] = $freeshipping;
    endif;

    return $available_methods;
  4. @ChromeOrange ChromeOrange revised this gist Jul 23, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // Hide ALL shipping options when free shipping is available
    add_filter( 'woocommerce_available_shipping_methods', 'hide_standard_shipping_when_free_is_available' , 10, 1 );
    add_filter( 'woocommerce_available_shipping_methods', 'hide_all_shipping_when_free_is_available' , 10, 1 );

    /**
    * Hide ALL Shipping option when free shipping is available
    @@ -8,7 +8,7 @@
    */
    function hide_all_shipping_when_free_is_available( $available_methods ) {

    if( isset( $available_methods['free_shipping'] ) ) :
    if( isset( $available_methods['free_shipping'] ) ) :

    // Get Free Shipping array into a new array
    $freeshipping = array();
  5. @ChromeOrange ChromeOrange created this gist Jul 23, 2013.
    27 changes: 27 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    // Hide ALL shipping options when free shipping is available
    add_filter( 'woocommerce_available_shipping_methods', 'hide_standard_shipping_when_free_is_available' , 10, 1 );

    /**
    * Hide ALL Shipping option when free shipping is available
    *
    * @param array $available_methods
    */
    function hide_all_shipping_when_free_is_available( $available_methods ) {

    if( isset( $available_methods['free_shipping'] ) ) :

    // Get Free Shipping array into a new array
    $freeshipping = array();
    $freeshipping = $available_methods['free_shipping'];

    // Empty the $available_methods array
    unset( $available_methods );

    // Add Free Shipping back into $avaialble_methods
    $available_methods = array();
    $available_methods[] = $freeshipping;

    endif;

    return $available_methods;
    }