/**
  * Products collection.
  *
  * @return array
  */
 public function getLoadedProductCollection()
 {
     $productsToDisplay = [];
     $mode = $this->getRequest()->getActionName();
     $customerId = $this->getRequest()->getParam('customer_id');
     $limit = $this->recommnededHelper->getDisplayLimitByMode($mode);
     //login customer to receive the recent products
     $session = $this->sessionFactory->create();
     $isLoggedIn = $session->loginById($customerId);
     $collection = $this->viewed;
     $productItems = $collection->getItemsCollection()->setPageSize($limit);
     //get the product ids from items collection
     $productIds = $productItems->getColumnValues('product_id');
     //get product collection to check for salable
     $productCollection = $this->productFactory->create()->getCollection()->addAttributeToSelect('*')->addFieldToFilter('entity_id', ['in' => $productIds]);
     //show products only if is salable
     foreach ($productCollection as $product) {
         if ($product->isSalable()) {
             $productsToDisplay[$product->getId()] = $product;
         }
     }
     $this->helper->log('Recentlyviewed customer  : ' . $customerId . ', mode ' . $mode . ', limit : ' . $limit . ', items found : ' . count($productItems) . ', is customer logged in : ' . $isLoggedIn . ', products :' . count($productsToDisplay));
     $session->logout();
     return $productsToDisplay;
 }
 /**
  * Get the products to display for table.
  *
  * @return $this
  */
 public function getLoadedProductCollection()
 {
     $mode = $this->getRequest()->getActionName();
     $limit = $this->recommnededHelper->getDisplayLimitByMode($mode);
     $productIds = $this->recommnededHelper->getProductPushIds();
     $productCollection = $this->productFactory->create()->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('entity_id', ['in' => $productIds]);
     $productCollection->getSelect()->limit($limit);
     //important check the salable product in template
     return $productCollection;
 }
 /**
  * Get product collection.
  *
  * @return array
  */
 public function getLoadedProductCollection()
 {
     $productsToDisplay = [];
     $mode = $this->getRequest()->getActionName();
     $limit = $this->recommnededHelper->getDisplayLimitByMode($mode);
     $from = $this->recommnededHelper->getTimeFromConfig($mode);
     $to = $this->_localeDate->date()->format(\Zend_Date::ISO_8601);
     $reportProductCollection = $this->reportProductCollection->create()->addViewsCount($from, $to)->setPageSize($limit);
     //filter collection by category by category_id
     if ($catId = $this->getRequest()->getParam('category_id')) {
         $category = $this->categoryFactory->create()->load($catId);
         if ($category->getId()) {
             $reportProductCollection->getSelect()->joinLeft(['ccpi' => $this->coreResource->getTableName('catalog_category_product_index')], 'e.entity_id = ccpi.product_id', ['category_id'])->where('ccpi.category_id =?', $catId);
         } else {
             $this->helper->log('Most viewed. Category id ' . $catId . ' is invalid. It does not exist.');
         }
     }
     //filter collection by category by category_name
     if ($catName = $this->getRequest()->getParam('category_name')) {
         $category = $this->categoryFactory->create()->loadByAttribute('name', $catName);
         if ($category) {
             $reportProductCollection->getSelect()->joinLeft(['ccpi' => $this->coreResource->getTableName('catalog_category_product_index')], 'e.entity_id  = ccpi.product_id', ['category_id'])->where('ccpi.category_id =?', $category->getId());
         } else {
             $this->helper->log('Most viewed. Category name ' . $catName . ' is invalid. It does not exist.');
         }
     }
     //product ids from the report product collection
     $productIds = $reportProductCollection->getColumnValues('entity_id');
     $productCollectionFactory = $this->productCollectionFactory->create();
     $productCollectionFactory->addIdFilter($productIds)->addAttributeToSelect(['product_url', 'name', 'store_id', 'small_image', 'price']);
     //product collection
     foreach ($productCollectionFactory as $_product) {
         //add only saleable products
         if ($_product->isSalable()) {
             $productsToDisplay[] = $_product;
         }
     }
     return $productsToDisplay;
 }
 /**
  * Collection.
  *
  * @return array
  */
 public function getLoadedProductCollection()
 {
     //mode param grid/list
     $mode = $this->getRequest()->getActionName();
     //limit of the products to display
     $limit = $this->recommnededHelper->getDisplayLimitByMode($mode);
     //date range
     $from = $this->recommnededHelper->getTimeFromConfig($mode);
     $to = $this->_localeDate->date()->format(\Zend_Date::ISO_8601);
     //create report collection
     $reportProductCollection = $this->productSoldFactory->create();
     $connection = $this->resource->getConnection();
     $orderTableAliasName = $connection->quoteIdentifier('order');
     $fieldName = $orderTableAliasName . '.created_at';
     $orderTableAliasName = $connection->quoteIdentifier('order');
     $orderJoinCondition = [$orderTableAliasName . '.entity_id = order_items.order_id', $connection->quoteInto("{$orderTableAliasName}.state <> ?", \Magento\Sales\Model\Order::STATE_CANCELED)];
     $orderJoinCondition[] = $this->prepareBetweenSql($fieldName, $from, $to);
     $storeId = $this->_storeManager->getStore()->getId();
     $reportProductCollection->getSelect()->reset()->from(['order_items' => $reportProductCollection->getTable('sales_order_item')], ['ordered_qty' => 'SUM(order_items.qty_ordered)', 'order_items_name' => 'order_items.name'])->joinInner(['order' => $reportProductCollection->getTable('sales_order')], implode(' AND ', $orderJoinCondition), [])->columns(['sku'])->where('parent_item_id IS NULL')->group('order_items.product_id')->having('SUM(order_items.qty_ordered) > ?', 0)->order('ordered_qty DESC')->limit($limit);
     $reportProductCollection->setStoreIds([$storeId]);
     $productSkus = $reportProductCollection->getColumnValues('sku');
     $productCollection = $this->productFactory->create()->getCollection()->addAttributeToSelect('*')->addFieldToFilter('sku', ['in' => $productSkus]);
     return $productCollection;
 }
 /**
  * Number of the colums.
  *
  * @return int|mixed
  *
  * @throws \Exception
  */
 public function getColumnCount()
 {
     return $this->recommendedHelper->getDisplayLimitByMode($this->getRequest()->getActionName());
 }