diff --git a/app/code/Magento/CatalogInventory/Observer/ProcessInventoryDataObserver.php b/app/code/Magento/CatalogInventory/Observer/ProcessInventoryDataObserver.php index e473f714bd21d..cea19c098b928 100644 --- a/app/code/Magento/CatalogInventory/Observer/ProcessInventoryDataObserver.php +++ b/app/code/Magento/CatalogInventory/Observer/ProcessInventoryDataObserver.php @@ -97,7 +97,7 @@ private function prepareQuantityAndStockStatus(StockItemInterface $stockItem, ar ) { unset($quantityAndStockStatus['is_in_stock']); } - if (isset($quantityAndStockStatus['qty']) + if (array_key_exists('qty', $quantityAndStockStatus) && $stockItem->getQty() == $quantityAndStockStatus['qty'] ) { unset($quantityAndStockStatus['qty']); diff --git a/dev/tests/api-functional/testsuite/Magento/CatalogInventory/Api/ProductRepositoryInterfaceTest.php b/dev/tests/api-functional/testsuite/Magento/CatalogInventory/Api/ProductRepositoryInterfaceTest.php index 9478eca88a3fa..5422362afd73c 100644 --- a/dev/tests/api-functional/testsuite/Magento/CatalogInventory/Api/ProductRepositoryInterfaceTest.php +++ b/dev/tests/api-functional/testsuite/Magento/CatalogInventory/Api/ProductRepositoryInterfaceTest.php @@ -23,8 +23,9 @@ class ProductRepositoryInterfaceTest extends WebapiAbstract const KEY_PRODUCT_ID = StockStatusInterface::PRODUCT_ID; const KEY_CUSTOM_ATTRIBUTES = 'custom_attributes'; const KEY_ATTRIBUTE_CODE = \Magento\Eav\Api\Data\AttributeInterface::ATTRIBUTE_CODE; - const CODE_QUANTITY_AND_STOCK_STATUS = 'quantity_and_stock_status'; + const KEY_IS_IN_STOCK = 'is_in_stock'; + const CODE_QUANTITY_AND_STOCK_STATUS = 'quantity_and_stock_status'; const PRODUCT_SKU = 'sku-test-catalog-inventory'; /** @@ -159,6 +160,36 @@ public function testSimpleProductCreationWithoutSpecifyingCatalogInventory() $this->assertTrue($response); } + /** + * Tests updating product stock item data when previously product was created without specified stock_item + */ + public function testUpdatingQuantity() + { + // create a simple product with catalog inventory + $qty = null; + $productData = $this->getSimpleProductData($qty); + $response = $this->saveProduct($productData); + $stockItemData = $response[self::KEY_EXTENSION_ATTRIBUTES][self::KEY_STOCK_ITEM]; + + $this->assertEquals($qty, $stockItemData[self::KEY_QTY]); + $this->assertEquals(false, $stockItemData[self::KEY_IS_IN_STOCK]); + + // update a created product with catalog inventory + $qty = 1; + $inStock = true; + $response[self::KEY_EXTENSION_ATTRIBUTES][self::KEY_STOCK_ITEM][self::KEY_QTY] = $qty; + $response[self::KEY_EXTENSION_ATTRIBUTES][self::KEY_STOCK_ITEM][self::KEY_IS_IN_STOCK] = $inStock; + $responseUpdated = $this->updateProduct($response); + $stockItemDataUpdated = $responseUpdated[self::KEY_EXTENSION_ATTRIBUTES][self::KEY_STOCK_ITEM]; + + $this->assertEquals($qty, $stockItemDataUpdated[self::KEY_QTY]); + $this->assertEquals($inStock, $stockItemDataUpdated[self::KEY_IS_IN_STOCK]); + + // delete the product; expect that all goes well + $response = $this->deleteProduct($productData[ProductInterface::SKU]); + $this->assertTrue($response); + } + // --- my helpers ----------------------------------------------------------------------------- /** @@ -195,7 +226,7 @@ protected function getSimpleProductData($qty = 1000) [self::KEY_ATTRIBUTE_CODE => 'description', 'value' => 'My Product Description'], [ self::KEY_ATTRIBUTE_CODE => self::CODE_QUANTITY_AND_STOCK_STATUS, - 'value' => ['is_in_stock' => true, 'qty' => $qty] + 'value' => [self::KEY_IS_IN_STOCK => true, 'qty' => $qty] ], ]; } @@ -214,7 +245,7 @@ protected function getStockItemData($qty = 1000) return [ self::KEY_STOCK_ITEM => [ self::KEY_QTY => $qty, - 'is_in_stock' => true, + self::KEY_IS_IN_STOCK => true, 'is_qty_decimal' => false, 'show_default_notification_message' => false, 'use_config_min_qty' => true,