I'm looking for a way to get the single price of an order item in WooCommerce. I've followed this post here and used the get_price()
method but this method seems to be not available anymore:
Woocommerce - Getting the order item price and quantity.
foreach ( $order_items as $order_item ) {
error_log( $order_item->get_price() );
error_log( print_r( $order_item, true ) );
}
Uncaught Error: Call to undefined method WC_Order_Item_Product::get_price()
The problem is that I can't just get the product and read the normal price there because I need the price set during the order placement and a product price can be changed later a lot of times.
I've also printed out the whole order item to find the single price field there but found nothing:
[data:protected] => Array
(
[order_id] => 24
[name] => Dings Teil
[product_id] => 23
[variation_id] => 0
[quantity] => 2
[tax_class] =>
[subtotal] => 42.4
[subtotal_tax] => 6.78
[total] => 42.4
[total_tax] => 6.78
[taxes] => Array
(
[total] => Array
(
[6] => 6.784
)
[subtotal] => Array
(
[6] => 6.784
)
)
)
So all in all I need the single price from my order item somehow. WooCommerce seems to have a way getting it in the order items view but I can't find the way they deal with this:
Because I'm writing a plugin, any WooCommerce changes are not a good idea at all.
Update:
Yes, I've also had the idea dividing the subtotal by the quantity but this may cause some rounding problems in the case my rounding is not 100% like the WooCommerce rounding.