最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Woocommerce - Shipping tax class based on cart items not using the highest tax available

programmeradmin0浏览0评论

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.

Share Improve this question asked Sep 17, 2019 at 14:49 FamousWollufFamousWolluf 1391 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I 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;
}
发布评论

评论列表(0)

  1. 暂无评论