Created
February 17, 2024 00:22
-
-
Save wpeasy/7ac5807ddfb6b71341c5396ceeedb552 to your computer and use it in GitHub Desktop.
Woocommerce Hide Paid Shipping id qualify for free.
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 | |
/** | |
* Hide paid shipping methods if free shipping is available. | |
*/ | |
/* | |
$name should be the part before the : separaor | |
*/ | |
function wpe_find_shipping_method($name, $rates){ | |
/* The rate could have any suffic, find by part before the : */ | |
foreach($rates as $fullName=>$value){ | |
$parts = explode(':', $fullName); | |
if($parts[0] === $name){ | |
return [ | |
'name' => $fullName, | |
'value' => $value, | |
]; | |
} | |
} | |
return false; | |
} | |
function wpe_hide_paid_shipping_when_free_shipping_available( $rates, $package) { | |
$free = wpe_find_shipping_method('free_shipping', $rates); | |
if($free){ | |
$newRates = array( | |
$free['name'] => $free['value'] | |
); | |
$local = wpe_find_shipping_method('local_pickup', $rates); | |
if($local){ | |
$newRates += array($local['name'] => $local['value']); | |
} | |
return $newRates; | |
}else{ | |
return $rates; | |
} | |
} | |
add_filter( 'woocommerce_package_rates', 'wpe_hide_paid_shipping_when_free_shipping_available', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment