PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 10, 2022

[FIXED] How to grab item data from WooCommerce order

 May 10, 2022     orders, php, product, woocommerce, wordpress     No comments   

Issue

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'];
}

Solution

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:

  • Get Order items and WC_Order_Item_Product in Woocommerce 3
  • How to get WooCommerce order details


Answered By - LoicTheAztec
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing