wordpress - 获取 Woocommerce 订单中每个产品的税率百分比

标签 wordpress woocommerce

我有两件事要问:
第一个是我想通过以下代码获取 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);

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

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

最佳答案

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

  //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));
                }

关于wordpress - 获取 Woocommerce 订单中每个产品的税率百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32437263/

相关文章:

html - 向右浮动菜单栏的最后一项

php - 单击父项时 Wordpress 展开子菜单

php - 在 WooCommerce 3 中的特定运输方式下方添加一个复选框

wordpress - Woocommerce 获取购物车页面中的产品小计

php - 如何更改 WordPress 'retrieve_password' 电子邮件的 header ?

mysql - WordPress 迁移到子域因路径不正确而损坏

wordpress - wp_nav_menu() : custom walker

wordpress - 哪里的表包含 woocommerce 中的产品价格?

php - 在 WooCommerce 中的 WC_Product_Query 上按名称 "LIKE"过滤产品

php - 更改 WooCommerce 中生成的变量产品的产品名称