public function getLoadedProductCollection() { $collection = array(); $mode = $this->getRequest()->getActionName(); $limit = $this->recommnededHelper->getDisplayLimitByMode($mode); $from = $this->recommnededHelper->getTimeFromConfig($mode); $to = new \Zend_Date($this->_localeDate->date()->getTimestamp()); $productCollection = $this->_productSoldFactory->create()->addAttributeToSelect('*')->addOrderedQty($from, $to->tostring(\Zend_Date::ISO_8601))->setOrder('ordered_qty', 'desc')->setPageSize($limit); //filter collection by category by category_id if ($cat_id = $this->getRequest()->getParam('category_id')) { $category = $this->_categoryFactory->create()->load($cat_id); if ($category->getId()) { $productCollection->getSelect()->joinLeft(array("ccpi" => 'catalog_category_product_index'), "e.entity_id = ccpi.product_id", array("category_id"))->where('ccpi.category_id =?', $cat_id); } else { $this->helper->log('Best seller. Category id ' . $cat_id . ' is invalid. It does not exist.'); } } //filter collection by category by category_name if ($cat_name = $this->getRequest()->getParam('category_name')) { $category = $this->_categoryFactory->create()->loadByAttribute('name', $cat_name); if ($category) { $productCollection->getSelect()->joinLeft(array("ccpi" => 'catalog_category_product_index'), "e.entity_id = ccpi.product_id", array("category_id"))->where('ccpi.category_id =?', $category->getId()); } else { $this->helper->log('Best seller. Category name ' . $cat_name . ' is invalid. It does not exist.'); } } if ($productCollection->getSize()) { foreach ($productCollection as $order) { foreach ($order->getAllVisibleItems() as $orderItem) { $collection[] = $orderItem->getProduct(); } } } return $collection; }
/** * Get product collection. * @return array */ public function getLoadedProductCollection() { $productsToDisplay = array(); $mode = $this->getRequest()->getActionName(); $limit = $this->recommnededHelper->getDisplayLimitByMode($mode); $from = $this->recommnededHelper->getTimeFromConfig($mode); $to = new \Zend_Date($this->_localeDate->date()->getTimestamp()); $productCollection = $this->_productCollection->create()->addViewsCount($from, $to->tostring(\Zend_Date::ISO_8601))->setPageSize($limit); //filter collection by category by category_id if ($cat_id = $this->getRequest()->getParam('category_id')) { $category = $this->_categoryFactory->create()->load($cat_id); if ($category->getId()) { $productCollection->getSelect()->joinLeft(array("ccpi" => 'catalog_category_product_index'), "e.entity_id = ccpi.product_id", array("category_id"))->where('ccpi.category_id =?', $cat_id); } else { $this->helper->log('Most viewed. Category id ' . $cat_id . ' is invalid. It does not exist.'); } } //filter collection by category by category_name if ($cat_name = $this->getRequest()->getParam('category_name')) { $category = $this->_categoryFactory->create()->loadByAttribute('name', $cat_name); if ($category) { $productCollection->getSelect()->joinLeft(array("ccpi" => 'catalog_category_product_index'), "e.entity_id = ccpi.product_id", array("category_id"))->where('ccpi.category_id =?', $category->getId()); } else { $this->helper->log('Most viewed. Category name ' . $cat_name . ' is invalid. It does not exist.'); } } //proudct collection foreach ($productCollection as $_product) { $productId = $_product->getId(); $product = $this->_productFactory->create()->load($productId); //available for sale if ($product->isSalable()) { $productsToDisplay[] = $product; } } return $productsToDisplay; }