Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/029b990dc64baecab05c648a8d962f21 to your computer and use it in GitHub Desktop.
Save FrancoStino/029b990dc64baecab05c648a8d962f21 to your computer and use it in GitHub Desktop.
Add extra charge on shipping rate for each KG weight on cart by zone
<?php
/**
* Add extra charge on shipping rate for each KG weight on cart based by zone
*/
add_filter( 'woocommerce_package_rates', 'custom_delivery_flat_rate_cost_calculation_by_zone', 10, 2 );
function custom_delivery_flat_rate_cost_calculation_by_zone( $rates, $package )
{
// The total cart items weight
$cart_weight = WC()->cart->get_cart_contents_weight();
// Shipping zone
$shipping_zone = wc_get_shipping_zone( $package );
// Get zone ID
$zone_id = $shipping_zone->get_id();
foreach($rates as $rate_key => $rate_values){
$method_id = $rate_values->method_id;
$rate_id = $rate_values->id;
if ($zone_id === 2 && 'wbs' === $method_id && $cart_weight > 100) { // Se lo slug di spedizione è uguale a, e il peso è maggiore di 500
## SHIPPING COSTS AND TAXES CALCULATIONS ##
for( $i = 200; $i <= $cart_weight; $i += 100 ){
$rates[$rate_id]->cost += 15.49;
}
}
return $rates;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment