Henry Wyrick
Henry Wyrick

Reputation: 53

How to grab item data from WooCommerce order

I need to take data from order of customer

Need to take item ID and quantity.

Tried with but not working

$item_data = $item_values->get_data();
foreach ($item_data as $item) {
    $product_id = $item['product_id'];
    $quantity = $item['quantity'];
}

Upvotes: 1

Views: 99

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253867

You need to use a foreach loop with order items from a dynamic Order ID $order_id (or from the WC_Order object $order):

// Get an instance of the WC_Order object (if you don't have the WC_Order object yet)
$order = wc_get_order( $order_id );

// Loop through order items
foreach( $order->get_items() as $item_id => $item ) { 
    // Get an instance of the WC_Product Object
    $product = $item->get_product();

    // Get the product ID
    $product_id = $product->get_id();

    // Get the item quantity
    $quantity = $item->get_quantity();
}

References:

Upvotes: 2

Related Questions