Barbareshet

Hide shipping rates when free shipping is available

Jan 14th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. /**
  2.  * Hide shipping rates when free shipping is available.
  3.  * Updated to support WooCommerce 2.6 Shipping Zones.
  4.  *
  5.  * @param array $rates Array of rates found for the package.
  6.  * @return array
  7.  */
  8. function my_hide_shipping_when_free_is_available( $rates ) {
  9.     $free = array();
  10.     $free_available = false;
  11.     foreach ( $rates as $rate_id => $rate ) {
  12.         if ( 'free_shipping' === $rate->method_id ) {
  13.             $free_available = true;
  14. //            $free[ $rate_id ] = $rate;
  15.             break;
  16.         }
  17.     }
  18.     foreach ( $rates as $rate_key => $rate ){
  19.         if ( $free_available && 6 === $rate->instance_id ){
  20.             unset( $rates[$rate_key] );
  21.         }
  22.     }
  23.     // this will return only free shipping method
  24.     return $rates;
  25. }
  26. add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );
Add Comment
Please, Sign In to add comment