【问题标题】:get tax rate percentage for each product in Woocommerce order获取 Woocommerce 订单中每种产品的税率百分比
【发布时间】:2017-10-27 17:44:35
【问题描述】:

我有两件事要问: 第一个是我想通过以下代码在 woocommerce 中获取订单中每个项目的税率百分比:

   $items = $order->get_items();
    if ($items) foreach ($items as $item_key => $item_value) {
        $_tax = new WC_Tax();
        $_product = $order->get_product_from_item( $item_value );
        $product_tax_class = $_product->get_tax_class();
        $tax =  $_tax->get_rates($product_tax_class);

结果是空白,所以我不知道代码有什么问题。

其次,标准费率始终显示为空白任务类。它可能与无税产品混淆。有什么问题吗?

【问题讨论】:

  • 你在哪个钩子上尝试这段代码?
  • 如果您查看数据库,“标准增值税”有一个空的tax_class_rate。这就是为什么它是“空白”

标签: wordpress woocommerce


【解决方案1】:

不知道它对你是否仍然有用,但可能对其他人有用。

  //Get items/products from order
   $items = $order->get_items();
                //Loop through each item
                foreach($items as $item){
                    //Get product by supplying variation id or product_id
                    $_product = get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
                    //Get rates of the product
                    $taxes = $tax->get_rates($wc_product->get_tax_class());
                    $rates = array_shift($taxes);
                    //Take only the item rate and round it. 
                    $item_rate = round(array_shift($rates));
                }

【讨论】:

  • 另外值得注意的是,如果您使用“标准税率”,则税类将为空。
  • 添加 $tax = new WC_Tax();在 for 循环之前。并在第 6 行中用 $wc_product 更正变量名 $_product,因为它在第 8 行中被使用。
猜你喜欢
  • 2018-04-01
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 2019-01-10
  • 2020-01-11
  • 2021-06-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多