Example #1
0
    /**
     * Get random items collection with or without related children
     *
     * @param int $limit
     * @param bool $nonChildrenOnly
     * @return ImportCollection
     */
    protected function _getItemsRandomCollection($limit, $nonChildrenOnly = false)
    {
        $collection = $this->_orderItemCollectionFactory->create()->setOrderFilter($this)->setRandomOrder();

        if ($nonChildrenOnly) {
            $collection->filterByParent();
        }
        $products = [];
        foreach ($collection as $item) {
            $products[] = $item->getProductId();
        }

        $productsCollection = $this->productListFactory->create()->addIdFilter(
            $products
        )->setVisibility(
            $this->_productVisibility->getVisibleInSiteIds()
        )->addPriceData()->setPageSize(
            $limit
        )->load();

        foreach ($collection as $item) {
            $product = $productsCollection->getItemById($item->getProductId());
            if ($product) {
                $item->setProduct($product);
            }
        }

        return $collection;
    }
Example #2
0
 /**
  * @param int $productId
  * @param array $customerIds
  * @return \Magento\Sales\Model\ResourceModel\Order\Item\Collection
  */
 protected function getCustomerOrderItemCollection($productId, $customerIds)
 {
     $customerOrderItemCollection = $this->orderItemCollectionFactory->create()->join(['order' => 'sales_order'], 'main_table.order_id = order.entity_id', ['customer_id' => 'customer_id', 'purchase_date' => 'order.updated_at'])->addFieldToFilter('main_table.product_id', $productId)->addFieldToFilter('order.status', 'complete')->addFieldToFilter('order.customer_id', ['in' => $customerIds]);
     return $customerOrderItemCollection;
 }