Home > Backend Development > PHP Tutorial > How to Access Order Item Data in WooCommerce 3?

How to Access Order Item Data in WooCommerce 3?

DDD
Release: 2024-12-19 13:45:10
Original
240 people have browsed it

How to Access Order Item Data in WooCommerce 3?

Access Order Items and WC_Order_Item_Product in WooCommerce 3

One notable change in WooCommerce 3 is the inability to directly access properties from order items. The following code, which previously worked, now results in an error:

$order_item_id = 15;
$order_item = new WC_Order_Item_Product($order_item_id);
$return = $order_item->get_id() ? $order_item : false;
Copy after login

Understanding the New Mechanisms

In WooCommerce 3, the WC_Order_Item_Product class does not have a constructor, and its properties can be accessed through dedicated methods. The following are the key methods for retrieving specific data:

Retrieving Specific Data

  • Product ID: get_product_id()
  • Variation ID: get_variation_id()
  • Order ID: get_order_id()
  • WC_Product Object: get_product()
  • WC_Order Object: get_order()
  • Item ID: get_id()
  • Product Name: get_name()
  • Product SKU: get_product()->get_sku()

Retrieving Totals

  • Subtotal (non-discounted): get_subtotal()
  • Subtotal Tax (non-discounted): get_subtotal_tax()
  • Total (discounted): get_total()
  • Total Tax (discounted): get_total_tax()

Retrieving Order Items

To retrieve order items from a WC_Order object and access their data (using the WC_Product Object), use the following code:

$order_id = 156; // The order_id
$order = wc_get_order( $order_id );

foreach( $order->get_items() as $item_id => $item ){

    // Product ID
    $product_id = $item->get_product_id();

    // Variation ID
    $variation_id = $item->get_variation_id();

    // WC_Product Object
    $product = $item->get_product();

    // Product Name
    $product_name = $item->get_name();
}
Copy after login

Accessing Data and Custom Metadata

Unprotecting Data and Metadata:

$formatted_meta_data = $item->get_formatted_meta_data( ' ', true );
$meta_value = $item->get_meta( 'custom_meta_key', true );
Copy after login

Array Access:

$product_id    = $item['product_id']; // Get the product ID
$variation_id  = $item['variation_id']; // Get the variation ID
Copy after login

Refer to the linked resources below for further insights:

  • [Get the metadata of an order item in woocommerce 3](https://stackoverflow.com/questions/54304498/get-the-metadata-of-an-order-item-in-woocommerce-3)
  • [How to get WooCommerce order details](https://stackoverflow.com/questions/32144098/how-to-get-woocommerce-order-details)

The above is the detailed content of How to Access Order Item Data in WooCommerce 3?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template