代码之家  ›  专栏  ›  技术社区  ›  Maciej Krawczyk

检查WooCommerce 3的产品价格是否含税+

  •  1
  • Maciej Krawczyk  · 技术社区  · 6 年前

    你如何通过程序检查产品的价格是否包括税?我查看了wc_产品参考资料,但找不到类似的东西。

    2 回复  |  直到 6 年前
        1
  •  1
  •   LoicTheAztec    6 年前

    您只需使用专用的条件函数 wc_prices_include_tax() 在一个 IF 声明:

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

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

    例如 wc-u价格包括税 被使用 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()。 , 包括税在内的价格 不含税的价格 有两个参数:

    艾斯蒂 $product (强制性) 这个 WC_产品 对象
    艾斯蒂 $args (可选) 包含产品价格和数量的数组

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


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

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

    订购相关项目:

        2
  •  0
  •   ArtisticPhoenix    6 年前

    好吧,产品不会告诉你是否有人缴纳销售税。为此,你需要一些与订单有关的东西。

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

    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 物体。

    祝你好运!