Skip to content

Commit 0774fb8

Browse files
author
Oleksii Korshenko
authored
MAGETWO-87906: Forwardport-pull-13494: Fixing of Problem with updating stock item qty and stock status #13638
2 parents 48625f2 + 176e8d3 commit 0774fb8

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

app/code/Magento/CatalogInventory/Observer/ProcessInventoryDataObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private function prepareQuantityAndStockStatus(StockItemInterface $stockItem, ar
9797
) {
9898
unset($quantityAndStockStatus['is_in_stock']);
9999
}
100-
if (isset($quantityAndStockStatus['qty'])
100+
if (array_key_exists('qty', $quantityAndStockStatus)
101101
&& $stockItem->getQty() == $quantityAndStockStatus['qty']
102102
) {
103103
unset($quantityAndStockStatus['qty']);

dev/tests/api-functional/testsuite/Magento/CatalogInventory/Api/ProductRepositoryInterfaceTest.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ class ProductRepositoryInterfaceTest extends WebapiAbstract
2323
const KEY_PRODUCT_ID = StockStatusInterface::PRODUCT_ID;
2424
const KEY_CUSTOM_ATTRIBUTES = 'custom_attributes';
2525
const KEY_ATTRIBUTE_CODE = \Magento\Eav\Api\Data\AttributeInterface::ATTRIBUTE_CODE;
26-
const CODE_QUANTITY_AND_STOCK_STATUS = 'quantity_and_stock_status';
26+
const KEY_IS_IN_STOCK = 'is_in_stock';
2727

28+
const CODE_QUANTITY_AND_STOCK_STATUS = 'quantity_and_stock_status';
2829
const PRODUCT_SKU = 'sku-test-catalog-inventory';
2930

3031
/**
@@ -159,6 +160,36 @@ public function testSimpleProductCreationWithoutSpecifyingCatalogInventory()
159160
$this->assertTrue($response);
160161
}
161162

163+
/**
164+
* Tests updating product stock item data when previously product was created without specified stock_item
165+
*/
166+
public function testUpdatingQuantity()
167+
{
168+
// create a simple product with catalog inventory
169+
$qty = null;
170+
$productData = $this->getSimpleProductData($qty);
171+
$response = $this->saveProduct($productData);
172+
$stockItemData = $response[self::KEY_EXTENSION_ATTRIBUTES][self::KEY_STOCK_ITEM];
173+
174+
$this->assertEquals($qty, $stockItemData[self::KEY_QTY]);
175+
$this->assertEquals(false, $stockItemData[self::KEY_IS_IN_STOCK]);
176+
177+
// update a created product with catalog inventory
178+
$qty = 1;
179+
$inStock = true;
180+
$response[self::KEY_EXTENSION_ATTRIBUTES][self::KEY_STOCK_ITEM][self::KEY_QTY] = $qty;
181+
$response[self::KEY_EXTENSION_ATTRIBUTES][self::KEY_STOCK_ITEM][self::KEY_IS_IN_STOCK] = $inStock;
182+
$responseUpdated = $this->updateProduct($response);
183+
$stockItemDataUpdated = $responseUpdated[self::KEY_EXTENSION_ATTRIBUTES][self::KEY_STOCK_ITEM];
184+
185+
$this->assertEquals($qty, $stockItemDataUpdated[self::KEY_QTY]);
186+
$this->assertEquals($inStock, $stockItemDataUpdated[self::KEY_IS_IN_STOCK]);
187+
188+
// delete the product; expect that all goes well
189+
$response = $this->deleteProduct($productData[ProductInterface::SKU]);
190+
$this->assertTrue($response);
191+
}
192+
162193
// --- my helpers -----------------------------------------------------------------------------
163194

164195
/**
@@ -195,7 +226,7 @@ protected function getSimpleProductData($qty = 1000)
195226
[self::KEY_ATTRIBUTE_CODE => 'description', 'value' => 'My Product Description'],
196227
[
197228
self::KEY_ATTRIBUTE_CODE => self::CODE_QUANTITY_AND_STOCK_STATUS,
198-
'value' => ['is_in_stock' => true, 'qty' => $qty]
229+
'value' => [self::KEY_IS_IN_STOCK => true, 'qty' => $qty]
199230
],
200231
];
201232
}
@@ -214,7 +245,7 @@ protected function getStockItemData($qty = 1000)
214245
return [
215246
self::KEY_STOCK_ITEM => [
216247
self::KEY_QTY => $qty,
217-
'is_in_stock' => true,
248+
self::KEY_IS_IN_STOCK => true,
218249
'is_qty_decimal' => false,
219250
'show_default_notification_message' => false,
220251
'use_config_min_qty' => true,

0 commit comments

Comments
 (0)