【问题标题】:Check if product prices includes tax in Woocommerce 3+在 Woocommerce 3+ 中检查产品价格是否含税
【发布时间】:2019-05-22 14:12:04
【问题描述】:

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

【问题讨论】:

  • 你知道它的税前原值吗?因为如果您知道原价,那么这只是数学计算。如果您知道价格和税率就更好了。
  • 我花了 3 分钟。 WC_Order_Item_Product::get_subtotal_tax(),这就是我至少要开始的地方或类似的地方。 WC_Order_Item_Product::get_total_tax()WC_Order_Item_Product::get_taxes() docs.woocommerce.com/wc-apidocs/…

标签: php wordpress woocommerce price tax


【解决方案1】:

如果有人缴纳了销售税,产品不会告诉您。为此,您需要一些涉及订单的东西。

有订单和产品的东西,比如这个类:

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();

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

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

祝你好运!

【讨论】:

    【解决方案2】:

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

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

    它将检查 Woocommerce 中是否启用了税费,以及您的产品价格常规设置是否包含税费。

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

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

    wc_get_price_to_display()wc_get_price_including_tax()wc_get_price_excluding_tax() 有 2 个参数:

    $product (强制) WC_Product 对象
    $args (可选)一个包含产品价格和数量的数组

    相关:Display Woocommerce product price with and without tax and tax amount


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

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

    订购相关商品:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 2020-11-17
      • 2013-04-08
      • 2018-07-03
      • 1970-01-01
      • 2014-06-07
      相关资源
      最近更新 更多