Skip to content

Commit

Permalink
Update GridProduct.php
Browse files Browse the repository at this point in the history
  • Loading branch information
magiccart committed Apr 7, 2022
1 parent 66de340 commit d2a0202
Showing 1 changed file with 17 additions and 32 deletions.
49 changes: 17 additions & 32 deletions Block/Category/GridProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Catalog\Block\Product\AbstractProduct;

class GridProduct extends \Magento\Catalog\Block\Product\ListProduct
class GridProduct extends \Magiccart\Magicproduct\Block\Product\ListProduct
{

protected $_limit; // Limit Product
Expand Down Expand Up @@ -48,8 +48,8 @@ public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Data\Helper\PostHelper $postDataHelper,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
CategoryRepositoryInterface $categoryRepository,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Framework\Url\Helper\Data $urlHelper,
\Magento\CatalogWidget\Model\RuleFactory $ruleFactory,
\Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder,
Expand Down Expand Up @@ -104,15 +104,7 @@ protected function _getProductCollection()

if (is_null($this->_productCollection)) {
$this->setCategoryId($this->getTypeFilter());
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->_productCollectionFactory->create();
$collection->addCategoriesFilter(['in' => $this->getTypeFilter()]);
$this->_catalogLayer->prepareProductCollection($collection);
$collection = $this->_addProductAttributesAndPrices(
$collection
);
$collection->addStoreFilter();
$this->_productCollection = $collection;
$this->_productCollection = parent::_getProductCollection();
}

$this->_limit = (int) $this->getWidgetCfg('limit');
Expand Down Expand Up @@ -142,10 +134,12 @@ protected function getRule($conditions)

public function getBestseller($collection){

$producIds = $collection->getAllIds();
$producIds = [];
foreach ($collection as $product) {
$producIds[] = $product->getId();
}

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$collection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory')->create();
$collection = $this->_productCollectionFactory->create();
$collection->joinField(
'qty_ordered', 'sales_bestsellers_aggregated_yearly', 'qty_ordered', 'product_id=entity_id', null, 'inner'
);
Expand All @@ -155,7 +149,7 @@ public function getBestseller($collection){
$collection->addCategoriesFilter(['in' => [$categoryId]]);
}

$collection->addAttributeToFilter('entity_id', array('in' => $producIds))
$collection->addAttributeToFilter('entity_id', ['in' => $producIds])
->groupByAttribute('entity_id')
->addAttributeToSort('qty_ordered', 'desc')
->addStoreFilter()
Expand Down Expand Up @@ -187,22 +181,6 @@ public function getLatest($collection){

}

public function getMostviewed($collection){

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$report = $objectManager->get('\Magento\Reports\Model\ResourceModel\Report\Product\Viewed\CollectionFactory')->create();
$ids = $collection->getAllIds();
$report->addFieldToFilter('product_id', array('in' => $ids))->setPageSize($this->_limit)->setCurPage(1);
$producIds = array();
foreach ($report as $product) {
$producIds[] = $product->getProductId();
}

$collection->addAttributeToFilter('entity_id', array('in' => $producIds));

return $collection;
}

public function getNew($collection) {

$todayStartOfDayDate = $this->_localeDate->date()->setTime(0, 0, 0)->format('Y-m-d H:i:s');
Expand Down Expand Up @@ -238,9 +216,16 @@ public function getNew($collection) {

public function getRandom($collection) {

$producIds = [];
foreach ($collection as $product) {
$producIds[] = $product->getId();
}
$collection = $this->_productCollectionFactory->create();
$collection = $collection->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', ['in' => $producIds]);
$collection->getSelect()->order('rand()');
return $collection;

return $collection;
}

public function getRecently($collection) {
Expand Down

0 comments on commit d2a0202

Please sign in to comment.