Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cart.php
Go to the documentation of this file.
1 <?php
7 
11 
18 class Cart extends \Magento\Backend\Block\Widget\Grid\Extended
19 {
25  protected $_coreRegistry = null;
26 
31 
35  protected $quoteRepository;
36 
40  protected $quote = null;
41 
45  protected $_parentTemplate;
46 
50  protected $quoteFactory;
51 
61  public function __construct(
62  \Magento\Backend\Block\Template\Context $context,
63  \Magento\Backend\Helper\Data $backendHelper,
64  \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
65  \Magento\Framework\Data\CollectionFactory $dataCollectionFactory,
66  \Magento\Framework\Registry $coreRegistry,
67  \Magento\Quote\Model\QuoteFactory $quoteFactory,
68  array $data = []
69  ) {
70  $this->_dataCollectionFactory = $dataCollectionFactory;
71  $this->_coreRegistry = $coreRegistry;
72  $this->quoteRepository = $quoteRepository;
73  $this->quoteFactory = $quoteFactory;
74  parent::__construct($context, $backendHelper, $data);
75  }
76 
80  protected function _construct()
81  {
82  parent::_construct();
83  $this->setUseAjax(true);
84  $this->_parentTemplate = $this->getTemplate();
85  $this->setTemplate('Magento_Customer::tab/cart.phtml');
86  }
87 
93  protected function _prepareGrid()
94  {
95  $this->setId('customer_cart_grid' . $this->getWebsiteId());
96  parent::_prepareGrid();
97  }
98 
104  protected function _prepareCollection()
105  {
106  $quote = $this->getQuote();
107 
108  if ($quote) {
109  $collection = $quote->getItemsCollection(false);
110  } else {
111  $collection = $this->_dataCollectionFactory->create();
112  }
113 
114  $collection->addFieldToFilter('parent_item_id', ['null' => true]);
115 
116  $this->setCollection($collection);
117 
118  return parent::_prepareCollection();
119  }
120 
124  protected function _prepareColumns()
125  {
126  $this->addColumn('product_id', ['header' => __('ID'), 'index' => 'product_id', 'width' => '100px']);
127 
128  $this->addColumn(
129  'name',
130  [
131  'header' => __('Product'),
132  'index' => 'name',
133  'renderer' => \Magento\Customer\Block\Adminhtml\Edit\Tab\View\Grid\Renderer\Item::class
134  ]
135  );
136 
137  $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku', 'width' => '100px']);
138 
139  $this->addColumn(
140  'qty',
141  ['header' => __('Quantity'), 'index' => 'qty', 'type' => 'number', 'width' => '60px']
142  );
143 
144  $this->addColumn(
145  'price',
146  [
147  'header' => __('Price'),
148  'index' => 'price',
149  'type' => 'currency',
150  'currency_code' => $this->getQuote()->getQuoteCurrencyCode(),
151  'rate' => $this->getQuote()->getBaseToQuoteRate(),
152  ]
153  );
154 
155  $this->addColumn(
156  'total',
157  [
158  'header' => __('Total'),
159  'index' => 'row_total',
160  'type' => 'currency',
161  'currency_code' => $this->getQuote()->getQuoteCurrencyCode(),
162  'rate' => 1,
163  ]
164  );
165 
166  $this->addColumn(
167  'action',
168  [
169  'header' => __('Action'),
170  'index' => 'quote_item_id',
171  'renderer' => \Magento\Customer\Block\Adminhtml\Grid\Renderer\Multiaction::class,
172  'filter' => false,
173  'sortable' => false,
174  'actions' => [
175  [
176  'caption' => __('Configure'),
177  'url' => 'javascript:void(0)',
178  'process' => 'configurable',
179  'control_object' => $this->getJsObjectName() . 'cartControl',
180  ],
181  [
182  'caption' => __('Delete'),
183  'url' => '#',
184  'onclick' => 'return ' . $this->getJsObjectName() . 'cartControl.removeItem($item_id);'
185  ],
186  ]
187  ]
188  );
189 
190  return parent::_prepareColumns();
191  }
192 
198  public function getCustomerId()
199  {
200  return $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
201  }
202 
206  public function getGridUrl()
207  {
208  return $this->getUrl('customer/*/cart', ['_current' => true, 'website_id' => $this->getWebsiteId()]);
209  }
210 
216  public function getGridParentHtml()
217  {
218  $templateName = $this->resolver->getTemplateFileName($this->_parentTemplate, ['_relative' => true]);
219  return $this->fetchView($templateName);
220  }
221 
225  public function getRowUrl($row)
226  {
227  return $this->getUrl('catalog/product/edit', ['id' => $row->getProductId()]);
228  }
229 
235  protected function getQuote()
236  {
237  if (null === $this->quote) {
238  $customerId = $this->getCustomerId();
239  $storeIds = $this->_storeManager->getWebsite($this->getWebsiteId())->getStoreIds();
240 
241  try {
242  $this->quote = $this->quoteRepository->getForCustomer($customerId, $storeIds);
243  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
244  $this->quote = $this->quoteFactory->create()->setSharedStoreIds($storeIds);
245  }
246  }
247  return $this->quote;
248  }
249 }
__()
Definition: __.php:13
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Backend\Helper\Data $backendHelper, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, \Magento\Framework\Data\CollectionFactory $dataCollectionFactory, \Magento\Framework\Registry $coreRegistry, \Magento\Quote\Model\QuoteFactory $quoteFactory, array $data=[])
Definition: Cart.php:61