如何从 WooCommerce 订单中获取项目数据

How to grab item data from WooCommerce order

我需要从客户的订单中获取数据

需要取商品ID和数量

尝试过但没有用

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

您需要对来自动态订单 ID $order_id 的订单商品使用 foreach 循环 (或来自WC_Order对象$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();
}

参考文献: