From e4e99f94ab74971abdd9d7dd4eb1a71b8fea0dde Mon Sep 17 00:00:00 2001 From: alexandr Date: Sat, 24 Mar 2018 14:56:12 +0200 Subject: [PATCH] #737 Exception in Low stock report for product with another name on additional storeview --- .../Block/Adminhtml/Product/Lowstock/Grid.php | 21 +++++++++ .../ResourceModel/LowQuantityCollection.php | 46 +++++++++++++++++-- .../LowQuantityCollectionTest.php | 1 + .../reports_report_product_lowstock.xml | 1 - .../Test/_files/product_multistore_name.php | 17 +++++++ 5 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 app/code/Magento/InventoryLowQuantityNotificationApi/Test/_files/product_multistore_name.php diff --git a/app/code/Magento/InventoryLowQuantityNotification/Block/Adminhtml/Product/Lowstock/Grid.php b/app/code/Magento/InventoryLowQuantityNotification/Block/Adminhtml/Product/Lowstock/Grid.php index c462d29e0b084..916f25ccab680 100644 --- a/app/code/Magento/InventoryLowQuantityNotification/Block/Adminhtml/Product/Lowstock/Grid.php +++ b/app/code/Magento/InventoryLowQuantityNotification/Block/Adminhtml/Product/Lowstock/Grid.php @@ -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(); diff --git a/app/code/Magento/InventoryLowQuantityNotification/Model/ResourceModel/LowQuantityCollection.php b/app/code/Magento/InventoryLowQuantityNotification/Model/ResourceModel/LowQuantityCollection.php index 462e873a4d52a..2ca3250bb4dca 100644 --- a/app/code/Magento/InventoryLowQuantityNotification/Model/ResourceModel/LowQuantityCollection.php +++ b/app/code/Magento/InventoryLowQuantityNotification/Model/ResourceModel/LowQuantityCollection.php @@ -46,6 +46,11 @@ class LowQuantityCollection extends AbstractCollection */ private $attributeRepository; + /** + * @var int + */ + private $filterStoreId; + /** * @param EntityFactoryInterface $entityFactory * @param LoggerInterface $logger @@ -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 */ @@ -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 */ @@ -104,10 +130,8 @@ protected function _initSelect() $this->addFieldToSelect('*'); - $this->joinCatalogProduct(); $this->joinInventoryConfiguration(); - $this->addProductTypeFilter(); $this->addNotifyStockQtyFilter(); $this->addEnabledSourceFilter(); @@ -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']); + } } /** diff --git a/app/code/Magento/InventoryLowQuantityNotification/Test/Integration/Model/ResourceModel/LowQuantityCollectionTest.php b/app/code/Magento/InventoryLowQuantityNotification/Test/Integration/Model/ResourceModel/LowQuantityCollectionTest.php index e18e0efb1f06d..96c9fdaf9ca2b 100644 --- a/app/code/Magento/InventoryLowQuantityNotification/Test/Integration/Model/ResourceModel/LowQuantityCollectionTest.php +++ b/app/code/Magento/InventoryLowQuantityNotification/Test/Integration/Model/ResourceModel/LowQuantityCollectionTest.php @@ -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() { diff --git a/app/code/Magento/InventoryLowQuantityNotification/view/adminhtml/layout/reports_report_product_lowstock.xml b/app/code/Magento/InventoryLowQuantityNotification/view/adminhtml/layout/reports_report_product_lowstock.xml index 10689638d9cdb..4bddb888313a4 100644 --- a/app/code/Magento/InventoryLowQuantityNotification/view/adminhtml/layout/reports_report_product_lowstock.xml +++ b/app/code/Magento/InventoryLowQuantityNotification/view/adminhtml/layout/reports_report_product_lowstock.xml @@ -8,7 +8,6 @@ - diff --git a/app/code/Magento/InventoryLowQuantityNotificationApi/Test/_files/product_multistore_name.php b/app/code/Magento/InventoryLowQuantityNotificationApi/Test/_files/product_multistore_name.php new file mode 100644 index 0000000000000..3e9044af70a79 --- /dev/null +++ b/app/code/Magento/InventoryLowQuantityNotificationApi/Test/_files/product_multistore_name.php @@ -0,0 +1,17 @@ +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(); \ No newline at end of file