We have the "Shipping tax class" option on "Shipping tax class based on cart items".
When I add a free tax product to the shopping cart and a 21% tax product to the shopping cart together, the total tax for the shipment is 0.
That is incorrect because it should be 21%.
When I add only one product to the shopping cart it uses the correct tax, but not with 2 products of differtent taxe rates.
We have the "Shipping tax class" option on "Shipping tax class based on cart items".
When I add a free tax product to the shopping cart and a 21% tax product to the shopping cart together, the total tax for the shipment is 0.
That is incorrect because it should be 21%.
When I add only one product to the shopping cart it uses the correct tax, but not with 2 products of differtent taxe rates.
1 Answer
Reset to default 0I have found the answer by override the shipping tax filter
// 0% and 21% tax producdts added combined to the cart needs to have 21% shipping tax
add_filter('woocommerce_shipping_packages', 'override_woocommerce_shipping_packages');
function override_woocommerce_shipping_packages($packages) {
$shipment_needs_tax = false;
foreach ($packages[0]['contents'] as $cartitem) {
if (!empty($cartitem['data']->tax_class)) $shipment_needs_tax = true;
}
if ($shipment_needs_tax && empty($packages[0]['rates']['flat_rate:3']->get_taxes())) {
$shipcost = $packages[0]['rates']['flat_rate:3']->get_cost();
$shiptax = $shipcost * 0.21;
if ($shiptax > 0) $packages[0]['rates']['flat_rate:3']->set_taxes([4 => $shiptax]);
}
return $packages;
}