woocommerce get original item unit price in order

Viewed 2642

I want to display the item unit price (include sale price,and regular price ) in the customer email and customer view order page.

In woocommerce order line item object, you are only can see the item line subtotal, subtotal , qty..etc. But it doesn't show the original price unit price (including regular price and sale price) in the data. At most I can get the unit price by adding the discounted amount and line subtotal. But it is no way I know if the item is in sale or not as well as the responding price.

Right now I can use the following codes to get the unit price in order item:

$product = $item->get_product();
$product->get_price_html();

The problem with this get_price_html() is display the real time product price. Which means if I changed the product price after sometime, then the price in previous order details will changed as well. This will caused a lot of confusion when someone view the older order.

So how can I get the original unit price of the line item in order details?

1 Answers

This may not be the best answer. But seeing that it is still unanswered. I have tried $item->get_price(); and it returns it's an undefined function. However based on my problem I was able to use $item->get_total() and $item->get_quantity(). Based on this, I can just divide price by total. This is my temporary fix:

            foreach ($items as $item):
                $product_name = $item->get_name();
                $product_price = $item->get_total();
                $product_quantity = $item->get_quantity();
                //This gets per unit price
                $unit_price = $product_price / $product_quantity;
                ?>
            <tr>
                <td>
                    <?= $product_name ?>
                </td>
                <td>
                    <?= number_format($unit_price, 2) ?> / Unit
                </td>
                <td>
                    <?= $product_quantity ?>
                </td>
                <td>
                    <?= number_format($product_price, 2) ?>
                </td>
            </tr>
            <?php
            endforeach;

Update:

https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/

Easy way to select everything in your order.

Related