/**
  * 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;
 }