WooCommerce-基于购物车项目的运输税类,不使用可用的最高税额

时间:2019-09-17 作者:FamousWolluf

我们在“基于购物车项目的运输税类别”上有“运输税类别”选项
当我将免税产品添加到购物车,将21%的免税产品添加到购物车时,装运的总税额为0<这是不正确的,因为它应该是21%<当我只向购物车添加一种产品时,它使用正确的税,但不使用两种不同税率的产品。

1 个回复
最合适的回答,由SO网友:FamousWolluf 整理而成

我通过覆盖运费税过滤器找到了答案

// 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;
}