Cart.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Customer\Controller\RegistryConstants;
  9. use Magento\Directory\Model\Currency;
  10. /**
  11. * Adminhtml customer orders grid block
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Cart extends \Magento\Backend\Block\Widget\Grid\Extended
  17. {
  18. /**
  19. * Core registry
  20. *
  21. * @var \Magento\Framework\Registry
  22. */
  23. protected $_coreRegistry = null;
  24. /**
  25. * @var \Magento\Framework\Data\CollectionFactory
  26. */
  27. protected $_dataCollectionFactory;
  28. /**
  29. * @var \Magento\Quote\Api\CartRepositoryInterface
  30. */
  31. protected $quoteRepository;
  32. /**
  33. * @var \Magento\Quote\Model\Quote
  34. */
  35. protected $quote = null;
  36. /**
  37. * @var string
  38. */
  39. protected $_parentTemplate;
  40. /**
  41. * @var \Magento\Quote\Model\QuoteFactory
  42. */
  43. protected $quoteFactory;
  44. /**
  45. * @param \Magento\Backend\Block\Template\Context $context
  46. * @param \Magento\Backend\Helper\Data $backendHelper
  47. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  48. * @param \Magento\Framework\Data\CollectionFactory $dataCollectionFactory
  49. * @param \Magento\Framework\Registry $coreRegistry
  50. * @param \Magento\Quote\Model\QuoteFactory $quoteFactory
  51. * @param array $data
  52. */
  53. public function __construct(
  54. \Magento\Backend\Block\Template\Context $context,
  55. \Magento\Backend\Helper\Data $backendHelper,
  56. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
  57. \Magento\Framework\Data\CollectionFactory $dataCollectionFactory,
  58. \Magento\Framework\Registry $coreRegistry,
  59. \Magento\Quote\Model\QuoteFactory $quoteFactory,
  60. array $data = []
  61. ) {
  62. $this->_dataCollectionFactory = $dataCollectionFactory;
  63. $this->_coreRegistry = $coreRegistry;
  64. $this->quoteRepository = $quoteRepository;
  65. $this->quoteFactory = $quoteFactory;
  66. parent::__construct($context, $backendHelper, $data);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. protected function _construct()
  72. {
  73. parent::_construct();
  74. $this->setUseAjax(true);
  75. $this->_parentTemplate = $this->getTemplate();
  76. $this->setTemplate('Magento_Customer::tab/cart.phtml');
  77. }
  78. /**
  79. * Prepare grid
  80. *
  81. * @return void
  82. */
  83. protected function _prepareGrid()
  84. {
  85. $this->setId('customer_cart_grid' . $this->getWebsiteId());
  86. parent::_prepareGrid();
  87. }
  88. /**
  89. * Prepare collection
  90. *
  91. * @return $this
  92. */
  93. protected function _prepareCollection()
  94. {
  95. $quote = $this->getQuote();
  96. if ($quote) {
  97. $collection = $quote->getItemsCollection(false);
  98. } else {
  99. $collection = $this->_dataCollectionFactory->create();
  100. }
  101. $collection->addFieldToFilter('parent_item_id', ['null' => true]);
  102. $this->setCollection($collection);
  103. return parent::_prepareCollection();
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. protected function _prepareColumns()
  109. {
  110. $this->addColumn('product_id', ['header' => __('ID'), 'index' => 'product_id', 'width' => '100px']);
  111. $this->addColumn(
  112. 'name',
  113. [
  114. 'header' => __('Product'),
  115. 'index' => 'name',
  116. 'renderer' => \Magento\Customer\Block\Adminhtml\Edit\Tab\View\Grid\Renderer\Item::class
  117. ]
  118. );
  119. $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku', 'width' => '100px']);
  120. $this->addColumn(
  121. 'qty',
  122. ['header' => __('Quantity'), 'index' => 'qty', 'type' => 'number', 'width' => '60px']
  123. );
  124. $this->addColumn(
  125. 'price',
  126. [
  127. 'header' => __('Price'),
  128. 'index' => 'price',
  129. 'type' => 'currency',
  130. 'currency_code' => $this->getQuote()->getQuoteCurrencyCode(),
  131. 'rate' => $this->getQuote()->getBaseToQuoteRate(),
  132. ]
  133. );
  134. $this->addColumn(
  135. 'total',
  136. [
  137. 'header' => __('Total'),
  138. 'index' => 'row_total',
  139. 'type' => 'currency',
  140. 'currency_code' => $this->getQuote()->getQuoteCurrencyCode(),
  141. 'rate' => 1,
  142. ]
  143. );
  144. $this->addColumn(
  145. 'action',
  146. [
  147. 'header' => __('Action'),
  148. 'index' => 'quote_item_id',
  149. 'renderer' => \Magento\Customer\Block\Adminhtml\Grid\Renderer\Multiaction::class,
  150. 'filter' => false,
  151. 'sortable' => false,
  152. 'actions' => [
  153. [
  154. 'caption' => __('Configure'),
  155. 'url' => 'javascript:void(0)',
  156. 'process' => 'configurable',
  157. 'control_object' => $this->getJsObjectName() . 'cartControl',
  158. ],
  159. [
  160. 'caption' => __('Delete'),
  161. 'url' => '#',
  162. 'onclick' => 'return ' . $this->getJsObjectName() . 'cartControl.removeItem($item_id);'
  163. ],
  164. ]
  165. ]
  166. );
  167. return parent::_prepareColumns();
  168. }
  169. /**
  170. * Gets customer assigned to this block
  171. *
  172. * @return int
  173. */
  174. public function getCustomerId()
  175. {
  176. return $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function getGridUrl()
  182. {
  183. return $this->getUrl('customer/*/cart', ['_current' => true, 'website_id' => $this->getWebsiteId()]);
  184. }
  185. /**
  186. * Gets grid parent html
  187. *
  188. * @return string
  189. */
  190. public function getGridParentHtml()
  191. {
  192. $templateName = $this->resolver->getTemplateFileName($this->_parentTemplate, ['_relative' => true]);
  193. return $this->fetchView($templateName);
  194. }
  195. /**
  196. * @inheritdoc
  197. */
  198. public function getRowUrl($row)
  199. {
  200. return $this->getUrl('catalog/product/edit', ['id' => $row->getProductId()]);
  201. }
  202. /**
  203. * Get the quote of the cart
  204. *
  205. * @return \Magento\Quote\Model\Quote
  206. */
  207. protected function getQuote()
  208. {
  209. if (null === $this->quote) {
  210. $customerId = $this->getCustomerId();
  211. $storeIds = $this->_storeManager->getWebsite($this->getWebsiteId())->getStoreIds();
  212. try {
  213. $this->quote = $this->quoteRepository->getForCustomer($customerId, $storeIds);
  214. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  215. $this->quote = $this->quoteFactory->create()->setSharedStoreIds($storeIds);
  216. }
  217. }
  218. return $this->quote;
  219. }
  220. }