55 */
66namespace Magento \Customer \Block \Adminhtml \Edit \Tab ;
77
8- use Magento \Catalog \Model \Product ;
8+ use Magento \Backend \Block \Template \Context ;
9+ use Magento \Backend \Block \Widget \Form ;
10+ use Magento \Backend \Block \Widget \Grid \Extended ;
11+ use Magento \Backend \Helper \Data ;
12+ use Magento \Customer \Block \Adminhtml \Edit \Tab \View \Grid \Renderer \Item ;
13+ use Magento \Customer \Block \Adminhtml \Grid \Renderer \Multiaction ;
914use Magento \Customer \Controller \RegistryConstants ;
10- use Magento \Directory \Model \Currency ;
15+ use Magento \Framework \App \ObjectManager ;
16+ use Magento \Framework \Data \CollectionFactory ;
17+ use Magento \Framework \Data \FormFactory ;
18+ use Magento \Framework \Exception \NoSuchEntityException ;
19+ use Magento \Framework \Registry ;
20+ use Magento \Quote \Api \CartRepositoryInterface ;
21+ use Magento \Quote \Model \Quote ;
22+ use Magento \Quote \Model \QuoteFactory ;
23+ use Magento \Store \Model \System \Store as SystemStore ;
1124
1225/**
1326 * Adminhtml customer orders grid block
1427 *
1528 * @api
1629 * @since 100.0.2
30+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1731 */
18- class Cart extends \ Magento \ Backend \ Block \ Widget \ Grid \ Extended
32+ class Cart extends Extended
1933{
2034 /**
2135 * Core registry
2236 *
23- * @var \Magento\Framework\ Registry
37+ * @var Registry
2438 */
2539 protected $ _coreRegistry = null ;
2640
2741 /**
28- * @var \Magento\Framework\Data\ CollectionFactory
42+ * @var CollectionFactory
2943 */
3044 protected $ _dataCollectionFactory ;
3145
3246 /**
33- * @var \Magento\Quote\Api\ CartRepositoryInterface
47+ * @var CartRepositoryInterface
3448 */
3549 protected $ quoteRepository ;
3650
3751 /**
38- * @var \Magento\Quote\Model\ Quote
52+ * @var Quote
3953 */
4054 protected $ quote = null ;
4155
@@ -45,32 +59,46 @@ class Cart extends \Magento\Backend\Block\Widget\Grid\Extended
4559 protected $ _parentTemplate ;
4660
4761 /**
48- * @var \Magento\Quote\Model\ QuoteFactory
62+ * @var QuoteFactory
4963 */
5064 protected $ quoteFactory ;
65+ /**
66+ * @var SystemStore
67+ */
68+ private $ systemStore ;
69+ /**
70+ * @var FormFactory
71+ */
72+ private $ formFactory ;
5173
5274 /**
53- * @param \Magento\Backend\Block\Template\ Context $context
54- * @param \Magento\Backend\Helper\ Data $backendHelper
55- * @param \Magento\Quote\Api\ CartRepositoryInterface $quoteRepository
56- * @param \Magento\Framework\Data\ CollectionFactory $dataCollectionFactory
57- * @param \Magento\Framework\ Registry $coreRegistry
58- * @param \Magento\Quote\Model\ QuoteFactory $quoteFactory
75+ * @param Context $context
76+ * @param Data $backendHelper
77+ * @param CartRepositoryInterface $quoteRepository
78+ * @param CollectionFactory $dataCollectionFactory
79+ * @param Registry $coreRegistry
80+ * @param QuoteFactory $quoteFactory
5981 * @param array $data
82+ * @param SystemStore|null $systemStore
83+ * @param FormFactory|null $formFactory
6084 */
6185 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 = []
86+ Context $ context ,
87+ Data $ backendHelper ,
88+ CartRepositoryInterface $ quoteRepository ,
89+ CollectionFactory $ dataCollectionFactory ,
90+ Registry $ coreRegistry ,
91+ QuoteFactory $ quoteFactory ,
92+ array $ data = [],
93+ ?SystemStore $ systemStore = null ,
94+ ?FormFactory $ formFactory = null
6995 ) {
7096 $ this ->_dataCollectionFactory = $ dataCollectionFactory ;
7197 $ this ->_coreRegistry = $ coreRegistry ;
7298 $ this ->quoteRepository = $ quoteRepository ;
7399 $ this ->quoteFactory = $ quoteFactory ;
100+ $ this ->systemStore = $ systemStore ?? ObjectManager::getInstance ()->get (SystemStore::class);
101+ $ this ->formFactory = $ formFactory ?? ObjectManager::getInstance ()->get (FormFactory::class);
74102 parent ::__construct ($ context , $ backendHelper , $ data );
75103 }
76104
@@ -92,8 +120,11 @@ protected function _construct()
92120 */
93121 protected function _prepareGrid ()
94122 {
95- $ this ->setId ('customer_cart_grid ' . $ this -> getWebsiteId () );
123+ $ this ->setId ('customer_cart_grid ' );
96124 parent ::_prepareGrid ();
125+ if (!$ this ->_storeManager ->isSingleStoreMode ()) {
126+ $ this ->prepareWebsiteFilter ();
127+ }
97128 }
98129
99130 /**
@@ -129,7 +160,7 @@ protected function _prepareColumns()
129160 [
130161 'header ' => __ ('Product ' ),
131162 'index ' => 'name ' ,
132- 'renderer ' => \ Magento \ Customer \ Block \ Adminhtml \ Edit \ Tab \ View \ Grid \ Renderer \ Item::class
163+ 'renderer ' => Item::class
133164 ]
134165 );
135166
@@ -167,7 +198,7 @@ protected function _prepareColumns()
167198 [
168199 'header ' => __ ('Action ' ),
169200 'index ' => 'quote_item_id ' ,
170- 'renderer ' => \ Magento \ Customer \ Block \ Adminhtml \ Grid \ Renderer \ Multiaction::class,
201+ 'renderer ' => Multiaction::class,
171202 'filter ' => false ,
172203 'sortable ' => false ,
173204 'actions ' => [
@@ -245,10 +276,59 @@ protected function getQuote()
245276
246277 try {
247278 $ this ->quote = $ this ->quoteRepository ->getForCustomer ($ customerId , $ storeIds );
248- } catch (\ Magento \ Framework \ Exception \ NoSuchEntityException $ e ) {
279+ } catch (NoSuchEntityException $ e ) {
249280 $ this ->quote = $ this ->quoteFactory ->create ()->setSharedStoreIds ($ storeIds );
250281 }
251282 }
252283 return $ this ->quote ;
253284 }
285+
286+ /**
287+ * Add website filter block to the layout
288+ *
289+ * @return void
290+ */
291+ private function prepareWebsiteFilter (): void
292+ {
293+ $ form = $ this ->formFactory ->create ();
294+ $ form ->addField (
295+ 'website_filter ' ,
296+ 'select ' ,
297+ [
298+ 'name ' => 'website_id ' ,
299+ 'values ' => $ this ->systemStore ->getWebsiteOptionHash (),
300+ 'value ' => $ this ->getWebsiteId () ?? $ this ->_storeManager ->getWebsite ()->getId (),
301+ 'no_span ' => true ,
302+ 'onchange ' => "{$ this ->getJsObjectName ()}.loadByElement(this); " ,
303+ ]
304+ );
305+ /**
306+ * @var Form $formWidget
307+ */
308+ $ formWidget = $ this ->getLayout ()->createBlock (Form::class);
309+ $ formWidget ->setForm ($ form );
310+ $ formWidget ->setTemplate ('Magento_Customer::tab/cart_website_filter_form.phtml ' );
311+ $ this ->setChild (
312+ 'website_filter_block ' ,
313+ $ formWidget
314+ );
315+ }
316+
317+ /**
318+ * @inheritDoc
319+ */
320+ public function getMainButtonsHtml ()
321+ {
322+ return $ this ->getWebsiteFilterHtml () . parent ::getMainButtonsHtml ();
323+ }
324+
325+ /**
326+ * Generate website filter
327+ *
328+ * @return string
329+ */
330+ private function getWebsiteFilterHtml (): string
331+ {
332+ return $ this ->getChildHtml ('website_filter_block ' );
333+ }
254334}
0 commit comments