在 Woocommerce 3+ 中检查产品价格是否含税

Check if product prices includes tax in Woocommerce 3+

如何以编程方式检查产品价格是否含税?我检查了 WC_Product 参考,但找不到类似的东西。

Well 产品不会告诉你是否有人支付了销售税。为此,您需要涉及订单的东西。

有订单和产品的东西,像这样class:

WC_Order_Item_Product

供参考

https://docs.woocommerce.com/wc-apidocs/source-class-WC_Order_Item_Product.html#262-270

这至少有3个相关方法:

//Get subtotal tax.
WC_Order_Item_Product::get_subtotal_tax();

//Get total tax.
WC_Order_Item_Product::get_total_tax();

Get taxes.
WC_Order_Item_Product::get_taxes();

注意这些不是 "static" 我只是喜欢它的外观,在 PHP 文档页面上这样做很常见 :-p

现在你如何从你所在的地方到达那里,我不知道。意思是你如何获得这些神秘的 WC_Order_Item_Product 对象之一。

祝你好运!

您只需在 IF 语句中使用专用条件函数 wc_prices_include_tax()

if( wc_prices_include_tax() ) {
    // Price include tax
} else {
    // Price doesn't include tax
}

It will check if taxes are enabled in Woocommerce and if your product prices general setting are including tax or not.

例如wc_prices_include_tax()wc_get_price_including_tax()使用WC_Product价格函数(不是方法), 当需要在产品页面中显示产品价格(含税)时,她在 wc_get_price_to_display() 价格函数中使用了自己……

如果产品页面需要显示不含税的产品价格,wc_get_price_to_display()将使用wc_get_price_excluding_tax()

wc_get_price_to_display(), wc_get_price_including_tax() and wc_get_price_excluding_tax() have 2 arguments:

$product (mandatory) the WC_Product object
$args (optional) an array containing the product price and the quantity

相关:


在购物车、结帐和订单上还有另一个常规设置,可让您显示含税或不含税的价格。您可以使用以下方法检查显示的价格是否含税:

if( get_option( 'woocommerce_tax_display_cart' ) ) {
    // Prices displayed including tax
}

相关订单项目: