Created
August 22, 2021 14:44
-
-
Save bagerathan/b4f354c7e3bac2ec7bc3be75ef1af64d to your computer and use it in GitHub Desktop.
[Only show cheapest shipping option] #woo
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
/** | |
* Only display the cheapest shipping rate | |
*/ | |
function my_only_show_cheapest_shipping_rate( $rates, $package ) { | |
$cheapest_method = ''; | |
// Loop through shipping rates | |
if ( is_array( $rates ) ) : | |
foreach ( $rates as $key => $rate ) : | |
// Set variables when the rate is cheaper than the one saved | |
if ( empty( $cheapest_method ) || $rate->cost < $cheapest_method->cost ) : | |
$cheapest_method = $rate; | |
endif; | |
endforeach; | |
endif; | |
// Return the cheapest rate when possible | |
if ( ! empty( $cheapest_method ) ) : | |
return array( $cheapest_method->id => $cheapest_method ); | |
endif; | |
return $rates; | |
} | |
add_action( 'woocommerce_package_rates', 'my_only_show_cheapest_shipping_rate', 10, 2 ); |
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
function my_only_show_most_expensive_shipping_rate( $rates, $package ) { | |
$most_expensive_method = ''; | |
// Loop through shipping rates | |
if ( is_array( $rates ) ) : | |
foreach ( $rates as $key => $rate ) : | |
// Set variables when the rate is more expensive than the one saved | |
if ( empty( $most_expensive_method ) || $rate->cost > $most_expensive_method->cost ) : | |
$most_expensive_method = $rate; | |
endif; | |
endforeach; | |
endif; | |
// Return the most expensive rate when possible | |
if ( ! empty( $most_expensive_method ) ) : | |
return array( $most_expensive_method->id => $most_expensive_method ); | |
endif; | |
return $rates; | |
} | |
add_action( 'woocommerce_package_rates', 'my_only_show_most_expensive_shipping_rate', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment