Forked from JeroenSormani/hide-shipping-if-free-shipping-is-available.php
Created
April 20, 2022 09:54
-
-
Save jswebschmiede/ef902e99c0675e63a52047412bb67a74 to your computer and use it in GitHub Desktop.
New - Hide paid shipping rates when free is available. DOES show Local Pickup. Considered ANY shipping rate that is $0 as free (so the 'Free shipping' shipping method is not a requirement)
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
<?php | |
/** | |
* Copy from here to your (child) themes functions.php | |
* Recommended to do so via FTP. | |
*/ | |
/** | |
* Hide all but the free shipping options when free is available. | |
* | |
* Shows ALL free options when available. | |
* Does NOT take in account local pickup as free shipping - It does show local pickup rates in the list of free shipping rates | |
* DOES consider non-'free_shipping' rates that have 0 cost as free shipping. | |
*/ | |
function custom_hide_all_shipping_when_free_is_available( $shipping_rates) { | |
$free_rates = array(); | |
$free_shipping_available = false; | |
foreach ( $shipping_rates as $key => $rate ) { | |
// Check for free rates / don't take in account local pickup | |
if ( 0 == $rate->cost && $rate->method_id != 'local_pickup' ) { | |
$free_shipping_available = true; | |
} | |
if ( 0 == $rate->cost ) { | |
$free_rates[ $key ] = $rate; | |
} | |
} | |
// Show all free rates | |
if ( $free_shipping_available ) { | |
return $free_rates; | |
} | |
return $shipping_rates; | |
} | |
add_filter( 'woocommerce_package_rates', 'custom_hide_all_shipping_when_free_is_available' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment