【问题标题】:How to grab item data from WooCommerce order如何从 WooCommerce 订单中获取商品数据
【发布时间】:2019-02-21 18:18:29
【问题描述】:
我需要从客户的订单中获取数据
需要带物品ID和数量。
试过了,没用
$item_data = $item_values->get_data();
foreach ($item_data as $item) {
$product_id = $item['product_id'];
$quantity = $item['quantity'];
}
【问题讨论】:
标签:
php
wordpress
woocommerce
product
orders
【解决方案1】:
您需要使用 foreach 循环来处理来自动态订单 ID $order_id (或来自 WC_Order 对象 em> $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();
}
参考文献: