Accessing WC_Order Items and WC_Order_Item_Product in WooCommerce 3
WooCommerce 3 has introduced changes to the way order items are accessed and manipulated. This includes the deprecation of the WC_Order_Item_Product construct, requiring alternative methods for retrieving item data.
Getting Order Item Properties
Previously, the get_id() method was used to obtain the order item ID. In WooCommerce 3, the following methods should be used to retrieve specific item properties:
Getting WC_Product and WC_Order Objects
To access the WC_Product and WC_Order objects associated with an order item, use the following methods:
Accessing Data and Meta Data
The get_data() and get_meta_data() methods can be used to obtain the item's common data and meta data, respectively. To access specific meta data by key, use the get_meta() method.
Example:
$order_item_id = 15; $item = new WC_Order_Item_Product($order_item_id); // Get product ID $product_id = $item->get_product_id(); // Access common data as an array $item_data = $item->get_data(); // Access meta data as an array $item_meta = $item->get_meta_data();
Alternative Access:
WooCommerce still allows direct array access for backwards compatibility with legacy systems:
$product_id = $item['product_id']; $variation_id = $item['variation_id'];
Additional Notes:
The above is the detailed content of How Do I Access and Manipulate WooCommerce 3 Order Items and Their Data?. For more information, please follow other related articles on the PHP Chinese website!