Created
November 18, 2019 23:56
-
-
Save NickGreen/ae3abafeec8822dfd5edddd5f08e68b4 to your computer and use it in GitHub Desktop.
Set a sitewide max shipping amount
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 | |
/** | |
* Function to set a max cap on the shipping rates. | |
*/ | |
function my_max_limit_shipping_rates_function( $rates, $package ) { | |
$methods = WC()->shipping()->get_shipping_methods(); | |
$shipping_limit = 15; // The maximum amount would be $15 | |
// Loop through all rates | |
foreach ( $rates as $rate_id => $rate ) { | |
// Check if the rate is higher then a certain amount | |
if ( $rate->cost > $shipping_limit ) { | |
$rate->cost = $shipping_limit; | |
// Recalculate shipping taxes | |
if ( $method = $methods[ $rate->get_method_id() ] ) { | |
$taxes = WC_Tax::calc_shipping_tax( $rate->cost, WC_Tax::get_shipping_tax_rates() ); | |
$rate->set_taxes( $method->is_taxable() ? $taxes : array() ); | |
} | |
} | |
} | |
return $rates; | |
} | |
add_filter( 'woocommerce_package_rates', 'my_max_limit_shipping_rates_function', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment