1717use Magento \Catalog \Model \Product \Attribute \Source \Status ;
1818use Magento \Catalog \Model \Product \Visibility ;
1919use Magento \Catalog \Model \ProductFactory ;
20+ use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
2021use Magento \Framework \App \ObjectManager ;
2122use Magento \Framework \Registry ;
2223
@@ -42,6 +43,8 @@ class Product extends Extended
4243 */
4344 private $ visibility ;
4445
46+ private ProductCollectionFactory $ productCollectionFactory ;
47+
4548 /**
4649 * @param Context $context
4750 * @param Data $backendHelper
@@ -50,6 +53,7 @@ class Product extends Extended
5053 * @param array $data
5154 * @param Visibility|null $visibility
5255 * @param Status|null $status
56+ * @param ProductCollectionFactory|null $productCollectionFactory
5357 */
5458 public function __construct (
5559 Context $ context ,
@@ -58,12 +62,16 @@ public function __construct(
5862 Registry $ coreRegistry ,
5963 array $ data = [],
6064 Visibility $ visibility = null ,
61- Status $ status = null
65+ Status $ status = null ,
66+ ProductCollectionFactory $ productCollectionFactory = null
6267 ) {
6368 $ this ->_productFactory = $ productFactory ;
6469 $ this ->_coreRegistry = $ coreRegistry ;
6570 $ this ->visibility = $ visibility ?: ObjectManager::getInstance ()->get (Visibility::class);
6671 $ this ->status = $ status ?: ObjectManager::getInstance ()->get (Status::class);
72+ $ this ->productCollectionFactory = $ productCollectionFactory ?: ObjectManager::getInstance ()->get (
73+ ProductCollectionFactory::class
74+ );
6775 parent ::__construct ($ context , $ backendHelper , $ data );
6876 }
6977
@@ -125,31 +133,29 @@ protected function _prepareCollection()
125133 if ($ this ->getCategory ()->getId ()) {
126134 $ this ->setDefaultFilter (['in_category ' => 1 ]);
127135 }
128-
129- $ collection = $ this ->_productFactory ->create ()->getCollection ()->addAttributeToSelect (
130- 'name '
131- )->addAttributeToSelect (
132- 'sku '
133- )->addAttributeToSelect (
134- 'visibility '
135- )->addAttributeToSelect (
136- 'status '
137- )->addAttributeToSelect (
138- 'price '
139- )->joinField (
136+ $ collection = $ this ->productCollectionFactory ->create ();
137+ $ storeId = (int )$ this ->getRequest ()->getParam ('store ' , 0 );
138+ if ($ storeId > 0 ) {
139+ $ collection ->addStoreFilter ($ storeId );
140+ }
141+ $ collection ->addAttributeToSelect (
142+ [
143+ 'name ' ,
144+ 'sku ' ,
145+ 'visibility ' ,
146+ 'status ' ,
147+ 'price '
148+ ]
149+ );
150+ $ collection ->joinField (
140151 'position ' ,
141152 'catalog_category_product ' ,
142153 'position ' ,
143154 'product_id=entity_id ' ,
144155 'category_id= ' . (int )$ this ->getRequest ()->getParam ('id ' , 0 ),
145156 'left '
146157 );
147- $ storeId = (int )$ this ->getRequest ()->getParam ('store ' , 0 );
148- $ collection ->setStoreId ($ storeId );
149- if ($ storeId > 0 ) {
150- $ collection ->addStoreFilter ($ storeId );
151- }
152-
158+ $ collection ->getSelect ()->group ('e.entity_id ' );
153159 $ this ->setCollection ($ collection );
154160
155161 if ($ this ->getCategory ()->getProductsReadonly ()) {
0 commit comments