Skip to content

Commit

Permalink
magento#737 Exception in Low stock report for product with another na…
Browse files Browse the repository at this point in the history
…me on additional storeview
  • Loading branch information
AlexandrVolkov committed Mar 24, 2018
1 parent f8f0f54 commit e4e99f9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,29 @@ public function __construct(
*/
protected function _prepareCollection(): GridWidget
{
$website = $this->getRequest()->getParam('website');
$group = $this->getRequest()->getParam('group');
$store = $this->getRequest()->getParam('store');

if ($website) {
$storeIds = $this->_storeManager->getWebsite($website)->getStoreIds();
$storeId = array_pop($storeIds);
} elseif ($group) {
$storeIds = $this->_storeManager->getGroup($group)->getStoreIds();
$storeId = array_pop($storeIds);
} elseif ($store) {
$storeId = (int)$store;
} else {
$storeId = null;
}

/** @var LowQuantityCollection $collection */
$collection = $this->lowQuantityCollectionFactory->create();

if ($storeId) {
$collection->addStoreFilter($storeId);
}

$this->setCollection($collection);

return parent::_prepareCollection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class LowQuantityCollection extends AbstractCollection
*/
private $attributeRepository;

/**
* @var int
*/
private $filterStoreId;

/**
* @param EntityFactoryInterface $entityFactory
* @param LoggerInterface $logger
Expand Down Expand Up @@ -83,6 +88,17 @@ public function __construct(
);
}

/**
* Sets store filter for use in catalog product name join
* @param $storeId
* @return $this
*/
public function addStoreFilter($storeId)
{
$this->filterStoreId = (int)$storeId;
return $this;
}

/**
* @inheritdoc
*/
Expand All @@ -91,6 +107,16 @@ protected function _construct()
$this->_init(SourceItemModel::class, SourceItemResourceModel::class);
}

/**
* @inheritdoc
*/
protected function _beforeLoad()
{
parent::_beforeLoad();
$this->joinCatalogProduct();
$this->addProductTypeFilter();
return $this;
}
/**
* @inheritdoc
*/
Expand All @@ -104,10 +130,8 @@ protected function _initSelect()

$this->addFieldToSelect('*');

$this->joinCatalogProduct();
$this->joinInventoryConfiguration();

$this->addProductTypeFilter();
$this->addNotifyStockQtyFilter();
$this->addEnabledSourceFilter();

Expand Down Expand Up @@ -135,10 +159,22 @@ private function joinCatalogProduct()

$this->getSelect()->joinInner(
['product_entity_varchar' => $productEavVarcharTable],
'product_entity_varchar.entity_id = product_entity.entity_id AND product_entity_varchar.attribute_id = '
. $nameAttribute->getAttributeId(),
['product_name' => 'value']
'product_entity_varchar.entity_id = product_entity.entity_id ' .
'AND product_entity_varchar.store_id = 0 ' .
'AND product_entity_varchar.attribute_id = ' . $nameAttribute->getAttributeId(),
[]
);
if ($this->filterStoreId) {
$this->getSelect()->joinLeft(
['product_entity_varchar_store' => $productEavVarcharTable],
'product_entity_varchar_store.entity_id = product_entity.entity_id ' .
'AND product_entity_varchar_store.store_id = ' . $this->filterStoreId .
'AND product_entity_varchar_store.attribute_id = ' . $nameAttribute->getAttributeId(),
['product_name' => 'IFNULL(product_entity_varchar_store.value, product_entity_varchar.value)']
);
} else {
$this->getSelect()->columns(['product_name' => 'product_entity_varchar.value']);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected function setUp()
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
* @codingStandardsIgnoreLine
* @magentoDataFixture ../../../../app/code/Magento/InventoryLowQuantityNotificationApi/Test/_files/source_item_configuration.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryLowQuantityNotificationApi/Test/_files/product_multistore_name.php
*/
public function testLowQuantityCollection()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="inventorylowquantitynotification_lowstock_grid"/>
<body>
<referenceBlock name="adminhtml.report.product.lowstock.store_switcher" remove="true"/>
<referenceContainer name="content">
<block class="Magento\Reports\Block\Adminhtml\Product\Lowstock" name="adminhtml.report.grid.container"/>
</referenceContainer>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
require __DIR__ . '/../../../../../../dev/tests/integration/testsuite/Magento/Store/_files/core_fixturestore.php';
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

/** @var Magento\Store\Model\Store $store */
$store = $objectManager->create(\Magento\Store\Model\Store::class);
$store->load('fixturestore', 'code');

$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
$product->load($product->getIdBySku('SKU-1'))
->setStoreId($store->getId())
->setName('StoreTitle')
->save();

0 comments on commit e4e99f9

Please sign in to comment.