Skip to content
Advertisement

Woocommerce – Getting the order item price and quantity.

Using Woocommerce 2.6.8 , I can’t get the Order Item Data information as described in the docs and here on SO.

All I want is to get the Line Item price and Quantity, which should be as simple as:

JavaScript

Looking closer at what gets returned returned

JavaScript

This is all using documented Woocommerce methods, why is the information I need stored in this item_meta_array?

Does anyone know how I can get that information?

Preferably using documented methods as opposed to a crude hack of looping through the item_meta_array until I find the key I’m looking for.

I feel like I must be missing something obvious here.

Advertisement

Answer

Update (For WooCommerce 3+)

Now for the code you can use WC_Order_Item_Product (and WC_Product) methods instead, like:

JavaScript

Update: Also all the following WC_Abstract_Order methods allow to get order items data with various interesting options like:

JavaScript

Thanks to @Casper for his comment


Also WC_Data methods can be used to get order item data as an unprotected array or to get a specific nested or custom meta data value from a specific meta key:

JavaScript

This code is tested and works.

Method get_item_meta() is deprecated and has been replaced by wc_get_order_item_meta and it’s not anymore a method but a function with some parameters:

JavaScript
JavaScript
JavaScript

Prior versions of woocommerce (from 2.4 to 2.6.x)

You can use get_item_meta() WC_Abstract_order method, to get the order metadata (the item quantity and the item price total).

So your code will be:

JavaScript

This code is tested and fully functional.

Reference: Class WC_Abstract_Order Methods

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement