Get the tax rate for each product in a Woocommerce order

I have two questions: The first one is that I want to get the interest rate on percent of each order item in woocommerce using this code:

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

The result is empty, so I don’t know what is wrong with the code.

Secondly, the standard rate is always displayed as an empty task class. This may confuse a single tax product. Something is wrong?

+4
source share
1 answer

I do not know if it is useful for you, but it may be for others.

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

Source: https://habr.com/ru/post/1606244/


All Articles