Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Grid.php
Go to the documentation of this file.
1 <?php
7 
14 class Grid extends \Magento\Backend\Block\Dashboard\Grid
15 {
20 
24  protected $_moduleManager;
25 
33  public function __construct(
34  \Magento\Backend\Block\Template\Context $context,
35  \Magento\Backend\Helper\Data $backendHelper,
36  \Magento\Framework\Module\Manager $moduleManager,
37  \Magento\Reports\Model\ResourceModel\Order\CollectionFactory $collectionFactory,
38  array $data = []
39  ) {
40  $this->_moduleManager = $moduleManager;
41  $this->_collectionFactory = $collectionFactory;
42  parent::__construct($context, $backendHelper, $data);
43  }
44 
48  protected function _construct()
49  {
50  parent::_construct();
51  $this->setId('lastOrdersGrid');
52  }
53 
57  protected function _prepareCollection()
58  {
59  if (!$this->_moduleManager->isEnabled('Magento_Reports')) {
60  return $this;
61  }
62  $collection = $this->_collectionFactory->create()->addItemCountExpr()->joinCustomerName(
63  'customer'
64  )->orderByCreatedAt();
65 
66  if ($this->getParam('store') || $this->getParam('website') || $this->getParam('group')) {
67  if ($this->getParam('store')) {
68  $collection->addAttributeToFilter('store_id', $this->getParam('store'));
69  } elseif ($this->getParam('website')) {
70  $storeIds = $this->_storeManager->getWebsite($this->getParam('website'))->getStoreIds();
71  $collection->addAttributeToFilter('store_id', ['in' => $storeIds]);
72  } elseif ($this->getParam('group')) {
73  $storeIds = $this->_storeManager->getGroup($this->getParam('group'))->getStoreIds();
74  $collection->addAttributeToFilter('store_id', ['in' => $storeIds]);
75  }
76 
77  $collection->addRevenueToSelect();
78  } else {
79  $collection->addRevenueToSelect(true);
80  }
81 
82  $this->setCollection($collection);
83 
84  return parent::_prepareCollection();
85  }
86 
92  protected function _afterLoadCollection()
93  {
94  foreach ($this->getCollection() as $item) {
95  $item->getCustomer() ?: $item->setCustomer($item->getBillingAddress()->getName());
96  }
97  return $this;
98  }
99 
105  protected function _preparePage()
106  {
107  $this->getCollection()->setPageSize($this->getParam($this->getVarNameLimit(), $this->_defaultLimit));
108  // Remove count of total orders
109  // $this->getCollection()->setCurPage($this->getParam($this->getVarNamePage(), $this->_defaultPage));
110  }
111 
115  protected function _prepareColumns()
116  {
117  $this->addColumn(
118  'customer',
119  ['header' => __('Customer'), 'sortable' => false, 'index' => 'customer', 'default' => __('Guest')]
120  );
121 
122  $this->addColumn(
123  'items',
124  [
125  'header' => __('Items'),
126  'type' => 'number',
127  'sortable' => false,
128  'index' => 'items_count'
129  ]
130  );
131 
132  $baseCurrencyCode = $this->_storeManager->getStore((int)$this->getParam('store'))->getBaseCurrencyCode();
133 
134  $this->addColumn(
135  'total',
136  [
137  'header' => __('Total'),
138  'sortable' => false,
139  'type' => 'currency',
140  'currency_code' => $baseCurrencyCode,
141  'index' => 'revenue'
142  ]
143  );
144 
145  $this->setFilterVisibility(false);
146  $this->setPagerVisibility(false);
147 
148  return parent::_prepareColumns();
149  }
150 
154  public function getRowUrl($row)
155  {
156  return $this->getUrl('sales/order/view', ['order_id' => $row->getId()]);
157  }
158 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
setPagerVisibility($visible=true)
Definition: Grid.php:588
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Backend\Helper\Data $backendHelper, \Magento\Framework\Module\Manager $moduleManager, \Magento\Reports\Model\ResourceModel\Order\CollectionFactory $collectionFactory, array $data=[])
Definition: Grid.php:33
$moduleManager
Definition: products.php:75
getParam($paramName, $default=null)
Definition: Grid.php:729