i found solution here to count flat rate based on specific quantity steps in woocommerce, what i need additionally to add an extra specific amount based on shipping zone. for example : i charged customer 4 dollar for each 3 items based on specific shipping class, so if the customer added 6 items to cart with the same shipping class he will charged 8 dollar, what i need then to add 50 dollar additional based on the customer shipping zone so the total shipping cost get 58 dollar (8 for items number + 50 for shipping zone), also i want to be abble to add many shipping classes and many shipping zones to the code.
Here's the code
add_filter( 'woocommerce_package_rates', 'progressive_shipping_cost_based_shipping_class_quantity_steps', 10, 2 );
function progressive_shipping_cost_based_shipping_class_quantity_steps( $rates, $package )
{
// HERE Bellow your settings
$shipping_class = "very-light-weight"; // The shipping class ID
$qty_step = 20; // Items qty threshold for a step
$item_count = 0; // Initializing
// Get the shipping class ID
$class_id = get_term_by('slug', $shipping_class, 'product_shipping_class' )->term_id;
// Loop through in cart items to get the Tshirts count
foreach( $package['contents'] as $cart_item ) {
if ( $cart_item['data']->get_shipping_class_id() == $class_id ){
$item_count += $cart_item['quantity']; // Count Tshirts
}
}
// The rate operand increase each {$qty_step} depending on {$item_count}
$rate_operand = ceil( $item_count / $qty_step );
foreach ( $rates as $rate_key => $rate ){
// Targetting "Flat rate"
if( 'flat_rate' === $rate->method_id ) {
$has_taxes = false;
// Set the new cost
$rates[$rate_key]->cost = $rate->cost * $rate_operand;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
// New tax calculated cost
$taxes[$key] = $tax * $rate_operand;
$has_taxes = true;
}
}
// Set new taxes cost
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}