我正在尝试计算订单中有多少具有唯一父产品 ID 的变体。
例如这是我的订单:
Product A: Size: Small, Color: Red
Product A: Size Medium, Color: Blue
Product B: Size: Small, Color: Yellow
Product B: Size: Large, Color Blue
我想计算订单中存在多少唯一产品(在本例中为 2,A 和 B),以及每个唯一产品中存在多少变量(在本例中为每个 2)。
我怎样才能做到这一点?
我用这段代码尝试了一些东西,但我卡住了......
$order_id = $order->get_id();
$order_number = $order->get_order_number();
$order_quantity = $order->get_item_count();
# Iterating through each order items (WC_Order_Item_Product objects in WC 3+)
foreach ( $order_id->get_items() as $item_id => $item_values ) {
// Product_id
$product_id = $item_values->get_product_id();
// For product variation type
if( $item_values->is_type('variation') ){
if( $item_values->get_variation_id() > 0 ){
for ($x = 0; $x < $item_values->get_variation_id(); $x++) {
}
// Get the instance of the parent variable product Object
$parent_product = wc_get_product( $item->get_product_id() );