Alb*_*ert 3 php wordpress woocommerce
在 WooCommerce 中,我只能输入不含税的运费。我怎样才能使给定的价格含税?
显示“含税价格”的设置仅适用于产品。
例如,如果我在设置中输入运费 €1,00 结帐时显示 €1,21(0,21 = 税)
它应该是 1,00 欧元(包括 0,21 税)(不要在意这里的计算,这只是一个例子)
谢谢如果有人有解决方案或功能。
在最初的问题一年后,我遇到了同样的问题,并且没有太多帮助。
经过调查,这原来是“class-wc-tax.php”中运费税计算的一个问题,正如您在下面看到的硬编码运费是不含税的。
/**
* Calculate the shipping tax using a passed array of rates.
*
* @param float Price
* @param array Taxation Rate
* @return array
*/
public static function calc_shipping_tax( $price, $rates ) {
$taxes = self::calc_exclusive_tax( $price, $rates );
return apply_filters( 'woocommerce_calc_shipping_tax', $taxes, $price, $rates );
}
Run Code Online (Sandbox Code Playgroud)
事实证明,出于某种原因,woo 已经通过他们的下游税收计算推动了运费是免税的假设,因此您需要向您的functions.php 添加几个过滤器以解决问题。
1) 最先计算含税
function yourname_fix_shipping_tax( $taxes, $price, $rates) {
if(wc_prices_include_tax()){
return WC_Tax::calc_inclusive_tax( $price, $rates );
}
return $taxes;
}
add_filter( 'woocommerce_calc_shipping_tax', 'yourname_fix_shipping_tax',10,3);
Run Code Online (Sandbox Code Playgroud)
注意不要忘记 add_filter 末尾的 10,3。10 是我保留的默认优先级。3 是参数的数量。你真的需要这个 - 如果你忽略它,过滤器会显示行为问题。
2)第二个过滤器用于修复下游计算中的总数。
function yourname_fix_totals( $cart) {
if(wc_prices_include_tax()){
$cart->shipping_total -= $cart->shipping_tax_total;
}
}
add_filter( 'woocommerce_calculate_totals', 'yourname_fix_totals');
Run Code Online (Sandbox Code Playgroud)
第一个过滤器相当简单——它只是重新计算包容性情况下的税收。
第二个过滤器是处理 woo 将排他税的假设进一步推入他们的税收计算中的问题。
在“class-wc-cart.php”的第 1396 行附近,我们有以下内容。
// Grand Total - Discounted product prices, discounted tax, shipping cost + tax
$this->total = max( 0, apply_filters( 'woocommerce_calculated_total', round( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total + $this->fee_total, $this->dp ), $this ) );
Run Code Online (Sandbox Code Playgroud)
这想要做的是将运费税添加到我们不希望它做的运费总额中 - 他们已经将其硬编码......
我们要做的是使用位于 1392 行附近几行位置方便的“woocommerce_calculate_totals”过滤器()从总数中减去运费,然后使用上面的第二个过滤器将其全部加起来。
// Allow plugins to hook and alter totals before final total is calculated
do_action( 'woocommerce_calculate_totals', $this );
Run Code Online (Sandbox Code Playgroud)
在几周前对这篇文章的较早剪辑中,我担心这会是一罐蠕虫——现在我可以测试东西了。
因此,巨大这里警告。彻底测试您的最终税收总额和小计 - 这可能不再是一种风险。雨已经取得的不含税航运的假设,已经通过其下游的计算推它。不幸的是,即使您的代码中存在错误,您也必须向各自的政府纳税。
还要记住,当您在 woocommerce 设置中从独占税更改为包含税时,旧价格不受影响,因此您可能需要使用干净的测试数据。
到目前为止,所有这些对我来说都很好,但是在我的情况下,我绕过了大量的 woo - 我没有使用他们的任何前端代码,这意味着我可能没有找到受税收假设影响的其他地方独家送货。
| 归档时间: |
|
| 查看次数: |
6122 次 |
| 最近记录: |