I’m trying to add a 65% increase across all shipping methods.
I’ve used variations of this function but this doesn’t affect the rates that are returned.
function increase_shipping_costs() {
// Get the current shipping rates
$shipping_zones = WC_Shipping_Zones::get_zones();
foreach ( $shipping_zones as $zone ) {
foreach ( $zone['shipping_methods'] as $shipping_method ) {
// Check if the shipping method is a flat rate
if ( !$shipping_method->id === 'free_shipping' ) {
// Get the current cost
$current_cost = $shipping_method->cost;
// Calculate the increased cost
$increased_cost = $current_cost + ( $current_cost * 0.65 );
// Update the shipping cost
$shipping_method->cost = $increased_cost;
$shipping_method->save();
}
}
}
}
add_filter('woocommerce_package_rates', 'increase_shipping_costs', 10, 2);
2
Answers
The filter’s callback is not setup properly:
You’ll want to use the data provided through the filter (ex:
$package_rates
) rather thanWC_Shipping_Zones::get_zones()
.To add a 65% increase cost across all shipping methods rates,
woocommerce_package_rates
is the correct filter hook to be used, but you will need to modify mostly everything in your function to get it working properly (as there are some missing things and some mistakes in your code):Code goes in functions.php file of the active child theme (or active theme). It should work.