Cart.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Adminhtml\Edit\Tab\View;
  7. use Magento\Customer\Controller\RegistryConstants;
  8. use Magento\Directory\Model\Currency;
  9. /**
  10. * Adminhtml customer cart items grid block
  11. *
  12. * @SuppressWarnings(PHPMD.LongVariable)
  13. *
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Cart extends \Magento\Backend\Block\Widget\Grid\Extended
  18. {
  19. /**
  20. * Core registry
  21. *
  22. * @var \Magento\Framework\Registry
  23. */
  24. protected $_coreRegistry = null;
  25. /**
  26. * @var \Magento\Framework\Data\CollectionFactory
  27. */
  28. protected $_dataCollectionFactory;
  29. /**
  30. * @var \Magento\Quote\Api\CartRepositoryInterface
  31. */
  32. protected $quoteRepository;
  33. /**
  34. * @var \Magento\Quote\Model\Quote
  35. */
  36. protected $quote = null;
  37. /**
  38. * @var \Magento\Quote\Model\QuoteFactory
  39. */
  40. protected $quoteFactory;
  41. /**
  42. * @param \Magento\Backend\Block\Template\Context $context
  43. * @param \Magento\Backend\Helper\Data $backendHelper
  44. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  45. * @param \Magento\Framework\Data\CollectionFactory $dataCollectionFactory
  46. * @param \Magento\Framework\Registry $coreRegistry
  47. * @param \Magento\Quote\Model\QuoteFactory $quoteFactory
  48. * @param array $data
  49. */
  50. public function __construct(
  51. \Magento\Backend\Block\Template\Context $context,
  52. \Magento\Backend\Helper\Data $backendHelper,
  53. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
  54. \Magento\Framework\Data\CollectionFactory $dataCollectionFactory,
  55. \Magento\Framework\Registry $coreRegistry,
  56. \Magento\Quote\Model\QuoteFactory $quoteFactory,
  57. array $data = []
  58. ) {
  59. $this->_dataCollectionFactory = $dataCollectionFactory;
  60. $this->_coreRegistry = $coreRegistry;
  61. $this->quoteRepository = $quoteRepository;
  62. $this->quoteFactory = $quoteFactory;
  63. parent::__construct($context, $backendHelper, $data);
  64. }
  65. /**
  66. * @inheritdoc
  67. */
  68. protected function _construct()
  69. {
  70. parent::_construct();
  71. $this->setId('customer_view_cart_grid');
  72. $this->setDefaultSort('added_at', 'desc');
  73. $this->setSortable(false);
  74. $this->setPagerVisibility(false);
  75. $this->setFilterVisibility(false);
  76. $this->setEmptyText(__('There are no items in customer\'s shopping cart.'));
  77. }
  78. /**
  79. * Prepare the cart collection.
  80. *
  81. * @return $this
  82. */
  83. protected function _prepareCollection()
  84. {
  85. $quote = $this->getQuote();
  86. if ($quote) {
  87. $collection = $quote->getItemsCollection(true);
  88. } else {
  89. $collection = $this->_dataCollectionFactory->create();
  90. }
  91. $collection->addFieldToFilter('parent_item_id', ['null' => true]);
  92. $this->setCollection($collection);
  93. return parent::_prepareCollection();
  94. }
  95. /**
  96. * @inheritdoc
  97. */
  98. protected function _prepareColumns()
  99. {
  100. $this->addColumn('product_id', ['header' => __('ID'), 'index' => 'product_id', 'width' => '100px']);
  101. $this->addColumn('name', ['header' => __('Product'), 'index' => 'name']);
  102. $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku', 'width' => '100px']);
  103. $this->addColumn('qty', ['header' => __('Qty'), 'index' => 'qty', 'type' => 'number', 'width' => '60px']);
  104. $this->addColumn(
  105. 'price',
  106. [
  107. 'header' => __('Price'),
  108. 'index' => 'price',
  109. 'type' => 'currency',
  110. 'currency_code' => $this->getQuote()->getQuoteCurrencyCode(),
  111. 'rate' => $this->getQuote()->getBaseToQuoteRate(),
  112. ]
  113. );
  114. $this->addColumn(
  115. 'total',
  116. [
  117. 'header' => __('Total'),
  118. 'index' => 'row_total',
  119. 'type' => 'currency',
  120. 'currency_code' => $this->getQuote()->getQuoteCurrencyCode(),
  121. 'rate' => 1,
  122. ]
  123. );
  124. return parent::_prepareColumns();
  125. }
  126. /**
  127. * @inheritdoc
  128. */
  129. public function getRowUrl($row)
  130. {
  131. return $this->getUrl('catalog/product/edit', ['id' => $row->getProductId()]);
  132. }
  133. /**
  134. * @inheritdoc
  135. */
  136. public function getHeadersVisibility()
  137. {
  138. return $this->getCollection()->getSize() >= 0;
  139. }
  140. /**
  141. * Get quote
  142. *
  143. * @return \Magento\Quote\Model\Quote
  144. */
  145. protected function getQuote()
  146. {
  147. if (null == $this->quote) {
  148. $storeIds = $this->_storeManager->getWebsite($this->getWebsiteId())->getStoreIds();
  149. $this->quote = $this->quoteFactory->create()->setSharedStoreIds($storeIds);
  150. $currentCustomerId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
  151. if (!empty($currentCustomerId)) {
  152. try {
  153. $this->quote = $this->quoteRepository->getForCustomer($currentCustomerId, $storeIds);
  154. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  155. }
  156. }
  157. }
  158. return $this->quote;
  159. }
  160. }