/** * {@inheritdoc} */ protected function _prepareCollection() { $collection = $this->_collectionFactory->create(); /* @var $collection \Magento\Reports\Model\ResourceModel\Order\Collection */ $collection->groupByCustomer()->addOrdersCount()->joinCustomerName(); $storeFilter = 0; if ($this->getParam('store')) { $collection->addAttributeToFilter('store_id', $this->getParam('store')); $storeFilter = 1; } elseif ($this->getParam('website')) { $storeIds = $this->_storeManager->getWebsite($this->getParam('website'))->getStoreIds(); $collection->addAttributeToFilter('store_id', ['in' => $storeIds]); } elseif ($this->getParam('group')) { $storeIds = $this->_storeManager->getGroup($this->getParam('group'))->getStoreIds(); $collection->addAttributeToFilter('store_id', ['in' => $storeIds]); } $collection->addSumAvgTotals($storeFilter)->orderByTotalAmount(); $this->setCollection($collection); return parent::_prepareCollection(); }
/** * @return $this */ protected function _prepareCollection() { if (!$this->_moduleManager->isEnabled('Magento_Reports')) { return $this; } $collection = $this->_collectionFactory->create()->addItemCountExpr()->joinCustomerName('customer')->orderByCreatedAt(); if ($this->getParam('store') || $this->getParam('website') || $this->getParam('group')) { if ($this->getParam('store')) { $collection->addAttributeToFilter('store_id', $this->getParam('store')); } elseif ($this->getParam('website')) { $storeIds = $this->_storeManager->getWebsite($this->getParam('website'))->getStoreIds(); $collection->addAttributeToFilter('store_id', ['in' => $storeIds]); } elseif ($this->getParam('group')) { $storeIds = $this->_storeManager->getGroup($this->getParam('group'))->getStoreIds(); $collection->addAttributeToFilter('store_id', ['in' => $storeIds]); } $collection->addRevenueToSelect(); } else { $collection->addRevenueToSelect(true); } $this->setCollection($collection); return parent::_prepareCollection(); }