items = $this->getItemsByOrderProducts(); } protected function getItemsByOrderProducts() { $ordersIds = isset($this->data[static::IMPORT_ITEMS_IDS_PARAM]) ? $this->data[static::IMPORT_ITEMS_IDS_PARAM] : null; $orderQuantity = isset($this->data['orderQuantity']) ? $this->data['orderQuantity'] : null; if (!$ordersIds) { return array(); } $items = $this->getOrderItemsProducts($ordersIds, $orderQuantity); return $items; } protected function getOrderItemsProducts($ordersIds, $orderQuantity = '', $itemsIds = null) { $items = array(); foreach ($ordersIds as $id) { $itemIdToProductIdMap = array(); $quantities = array(); $itemQuantities = array(); $orderItems = array(); $orderItemsByItemId = array(); $order = wc_get_order($id); if (!$order) { continue; } foreach ($order->get_items() as $itemId => $itemData) { if (!$itemsIds || in_array($itemId, $itemsIds)) { $product = $itemData->get_product(); $productId = $product->get_ID(); $quantity = $itemData->get_quantity(); if ( (!empty($this->profile) && !empty($this->profile->onlyParent) && intval($this->profile->onlyParent)) && $product->get_parent_id() > 0 ) { $product = wc_get_product($product->get_parent_id()); $productId = $product->get_id(); } $quantities[$productId] = isset($quantities[$productId]) ? $quantities[$productId] + $quantity : $quantity; $itemQuantities[$itemId] = $quantity; $orderItems[$productId] = $itemData; $itemIdToProductIdMap[$itemId] = $productId; $orderItemsByItemId[$itemId] = $itemData; } } $orderProducts = uslp_a4bGetPosts(array( 'post_type' => array('product', 'product_variation'), 'post__in' => empty($itemIdToProductIdMap) ? array(0) : array_values($itemIdToProductIdMap), 'orderby' => 'post__in', )); $orderProductsIndexedByPostId = array(); foreach ($orderProducts as $orderProduct) { $orderProductsIndexedByPostId[$orderProduct->ID] = $orderProduct; } foreach ($itemIdToProductIdMap as $itemId => $productId) { $item = clone $orderProductsIndexedByPostId[$productId]; $item->orderId = $id; $item->productInOrderQty = isset($itemQuantities[$itemId]) ? $itemQuantities[$itemId] : ''; $item->orderItem = isset($orderItemsByItemId[$itemId]) ? $orderItemsByItemId[$itemId] : null; if ($orderQuantity === 'by-orders') { if (isset($itemQuantities[$itemId])) { for ($i = 0; $i < $itemQuantities[$itemId]; $i++) { $items[] = $item; } } } else { $items[] = $item; } } } return $items; } }