Skip to content

Commit 71e2c41

Browse files
committed
#38233 Fixed products collection on category products grid
1 parent 6b34507 commit 71e2c41

File tree

1 file changed

+25
-19
lines changed
  • app/code/Magento/Catalog/Block/Adminhtml/Category/Tab

1 file changed

+25
-19
lines changed

app/code/Magento/Catalog/Block/Adminhtml/Category/Tab/Product.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1818
use Magento\Catalog\Model\Product\Visibility;
1919
use Magento\Catalog\Model\ProductFactory;
20+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
2021
use Magento\Framework\App\ObjectManager;
2122
use 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

Comments
 (0)