Skip to content

Commit e4e99f9

Browse files
magento#737 Exception in Low stock report for product with another name on additional storeview
1 parent f8f0f54 commit e4e99f9

File tree

5 files changed

+80
-6
lines changed

5 files changed

+80
-6
lines changed

app/code/Magento/InventoryLowQuantityNotification/Block/Adminhtml/Product/Lowstock/Grid.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,29 @@ public function __construct(
4646
*/
4747
protected function _prepareCollection(): GridWidget
4848
{
49+
$website = $this->getRequest()->getParam('website');
50+
$group = $this->getRequest()->getParam('group');
51+
$store = $this->getRequest()->getParam('store');
52+
53+
if ($website) {
54+
$storeIds = $this->_storeManager->getWebsite($website)->getStoreIds();
55+
$storeId = array_pop($storeIds);
56+
} elseif ($group) {
57+
$storeIds = $this->_storeManager->getGroup($group)->getStoreIds();
58+
$storeId = array_pop($storeIds);
59+
} elseif ($store) {
60+
$storeId = (int)$store;
61+
} else {
62+
$storeId = null;
63+
}
64+
4965
/** @var LowQuantityCollection $collection */
5066
$collection = $this->lowQuantityCollectionFactory->create();
67+
68+
if ($storeId) {
69+
$collection->addStoreFilter($storeId);
70+
}
71+
5172
$this->setCollection($collection);
5273

5374
return parent::_prepareCollection();

app/code/Magento/InventoryLowQuantityNotification/Model/ResourceModel/LowQuantityCollection.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class LowQuantityCollection extends AbstractCollection
4646
*/
4747
private $attributeRepository;
4848

49+
/**
50+
* @var int
51+
*/
52+
private $filterStoreId;
53+
4954
/**
5055
* @param EntityFactoryInterface $entityFactory
5156
* @param LoggerInterface $logger
@@ -83,6 +88,17 @@ public function __construct(
8388
);
8489
}
8590

91+
/**
92+
* Sets store filter for use in catalog product name join
93+
* @param $storeId
94+
* @return $this
95+
*/
96+
public function addStoreFilter($storeId)
97+
{
98+
$this->filterStoreId = (int)$storeId;
99+
return $this;
100+
}
101+
86102
/**
87103
* @inheritdoc
88104
*/
@@ -91,6 +107,16 @@ protected function _construct()
91107
$this->_init(SourceItemModel::class, SourceItemResourceModel::class);
92108
}
93109

110+
/**
111+
* @inheritdoc
112+
*/
113+
protected function _beforeLoad()
114+
{
115+
parent::_beforeLoad();
116+
$this->joinCatalogProduct();
117+
$this->addProductTypeFilter();
118+
return $this;
119+
}
94120
/**
95121
* @inheritdoc
96122
*/
@@ -104,10 +130,8 @@ protected function _initSelect()
104130

105131
$this->addFieldToSelect('*');
106132

107-
$this->joinCatalogProduct();
108133
$this->joinInventoryConfiguration();
109134

110-
$this->addProductTypeFilter();
111135
$this->addNotifyStockQtyFilter();
112136
$this->addEnabledSourceFilter();
113137

@@ -135,10 +159,22 @@ private function joinCatalogProduct()
135159

136160
$this->getSelect()->joinInner(
137161
['product_entity_varchar' => $productEavVarcharTable],
138-
'product_entity_varchar.entity_id = product_entity.entity_id AND product_entity_varchar.attribute_id = '
139-
. $nameAttribute->getAttributeId(),
140-
['product_name' => 'value']
162+
'product_entity_varchar.entity_id = product_entity.entity_id ' .
163+
'AND product_entity_varchar.store_id = 0 ' .
164+
'AND product_entity_varchar.attribute_id = ' . $nameAttribute->getAttributeId(),
165+
[]
141166
);
167+
if ($this->filterStoreId) {
168+
$this->getSelect()->joinLeft(
169+
['product_entity_varchar_store' => $productEavVarcharTable],
170+
'product_entity_varchar_store.entity_id = product_entity.entity_id ' .
171+
'AND product_entity_varchar_store.store_id = ' . $this->filterStoreId .
172+
'AND product_entity_varchar_store.attribute_id = ' . $nameAttribute->getAttributeId(),
173+
['product_name' => 'IFNULL(product_entity_varchar_store.value, product_entity_varchar.value)']
174+
);
175+
} else {
176+
$this->getSelect()->columns(['product_name' => 'product_entity_varchar.value']);
177+
}
142178
}
143179

144180
/**

app/code/Magento/InventoryLowQuantityNotification/Test/Integration/Model/ResourceModel/LowQuantityCollectionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function setUp()
3535
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
3636
* @codingStandardsIgnoreLine
3737
* @magentoDataFixture ../../../../app/code/Magento/InventoryLowQuantityNotificationApi/Test/_files/source_item_configuration.php
38+
* @magentoDataFixture ../../../../app/code/Magento/InventoryLowQuantityNotificationApi/Test/_files/product_multistore_name.php
3839
*/
3940
public function testLowQuantityCollection()
4041
{

app/code/Magento/InventoryLowQuantityNotification/view/adminhtml/layout/reports_report_product_lowstock.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
99
<update handle="inventorylowquantitynotification_lowstock_grid"/>
1010
<body>
11-
<referenceBlock name="adminhtml.report.product.lowstock.store_switcher" remove="true"/>
1211
<referenceContainer name="content">
1312
<block class="Magento\Reports\Block\Adminhtml\Product\Lowstock" name="adminhtml.report.grid.container"/>
1413
</referenceContainer>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
require __DIR__ . '/../../../../../../dev/tests/integration/testsuite/Magento/Store/_files/core_fixturestore.php';
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
9+
/** @var Magento\Store\Model\Store $store */
10+
$store = $objectManager->create(\Magento\Store\Model\Store::class);
11+
$store->load('fixturestore', 'code');
12+
13+
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
14+
$product->load($product->getIdBySku('SKU-1'))
15+
->setStoreId($store->getId())
16+
->setName('StoreTitle')
17+
->save();

0 commit comments

Comments
 (0)