Skip to content

Commit f13beab

Browse files
MAGETWO-85288: 8624: Stock status not coming back after qty update #955
2 parents 18bb122 + 08b4a41 commit f13beab

File tree

15 files changed

+150
-15
lines changed

15 files changed

+150
-15
lines changed

app/code/Magento/CatalogInventory/Model/Stock/StockItemRepository.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function __construct(
145145
/**
146146
* @inheritdoc
147147
*/
148-
public function save(\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem)
148+
public function save(StockItemInterface $stockItem)
149149
{
150150
try {
151151
/** @var \Magento\Catalog\Model\Product $product */
@@ -161,10 +161,7 @@ public function save(\Magento\CatalogInventory\Api\Data\StockItemInterface $stoc
161161
$typeId = $product->getTypeId() ?: $product->getTypeInstance()->getTypeId();
162162
$isQty = $this->stockConfiguration->isQty($typeId);
163163
if ($isQty) {
164-
$isInStock = $this->stockStateProvider->verifyStock($stockItem);
165-
if ($stockItem->getManageStock() && !$isInStock) {
166-
$stockItem->setIsInStock(false)->setStockStatusChangedAutomaticallyFlag(true);
167-
}
164+
$this->changeIsInStockIfNecessary($stockItem);
168165
// if qty is below notify qty, update the low stock date to today date otherwise set null
169166
$stockItem->setLowStockDate(null);
170167
if ($this->stockStateProvider->verifyNotification($stockItem)) {
@@ -260,4 +257,28 @@ private function getStockRegistryStorage()
260257
}
261258
return $this->stockRegistryStorage;
262259
}
260+
261+
/**
262+
* Change is_in_stock value if necessary.
263+
*
264+
* @param StockItemInterface $stockItem
265+
*
266+
* @return void
267+
*/
268+
private function changeIsInStockIfNecessary(StockItemInterface $stockItem)
269+
{
270+
$isInStock = $this->stockStateProvider->verifyStock($stockItem);
271+
if ($stockItem->getManageStock() && !$isInStock) {
272+
$stockItem->setIsInStock(false)->setStockStatusChangedAutomaticallyFlag(true);
273+
}
274+
275+
if ($stockItem->getManageStock()
276+
&& $isInStock
277+
&& !$stockItem->getIsInStock()
278+
&& $stockItem->getOrigData(\Magento\CatalogInventory\Api\Data\StockItemInterface::QTY) == 0
279+
&& $stockItem->getOrigData(\Magento\CatalogInventory\Api\Data\StockItemInterface::QTY) !== null
280+
) {
281+
$stockItem->setIsInStock(true)->setStockStatusChangedAutomaticallyFlag(true);
282+
}
283+
}
263284
}

app/code/Magento/CatalogInventory/Test/Unit/Model/Stock/StockItemRepositoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public function testSave()
276276
->method('verifyStock')
277277
->with($this->stockItemMock)
278278
->willReturn(false);
279-
$this->stockItemMock->expects($this->once())->method('getManageStock')->willReturn(true);
279+
$this->stockItemMock->expects($this->exactly(2))->method('getManageStock')->willReturn(true);
280280
$this->stockItemMock->expects($this->once())->method('setIsInStock')->with(false)->willReturnSelf();
281281
$this->stockItemMock->expects($this->once())
282282
->method('setStockStatusChangedAutomaticallyFlag')

dev/tests/integration/testsuite/Magento/Catalog/Model/ProductTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,4 +549,41 @@ public function testGetOptions()
549549
}
550550
}
551551
}
552+
553+
/**
554+
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
555+
*/
556+
public function testSaveWithDifferentQty()
557+
{
558+
//if save (out of stock product with qty 0) with new qty > 0 it should become in stock.
559+
//if set out of stock for product with qty > 0 it should become out of stock
560+
$product = $this->productRepository->get('simple-out-of-stock', true, null, true);
561+
$stockItem = $product->getExtensionAttributes()->getStockItem();
562+
$this->assertEquals(false, $stockItem->getIsInStock());
563+
$stockData = [
564+
'qty' => 5,
565+
'is_in_stock' => 0,
566+
];
567+
$product->setStockData($stockData);
568+
$product->save();
569+
570+
/** @var \Magento\CatalogInventory\Model\StockRegistryStorage $stockRegistryStorage */
571+
$stockRegistryStorage = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
572+
->get(\Magento\CatalogInventory\Model\StockRegistryStorage::class);
573+
$stockRegistryStorage->removeStockItem($product->getId());
574+
$product = $this->productRepository->get('simple-out-of-stock', true, null, true);
575+
$stockItem = $product->getExtensionAttributes()->getStockItem();
576+
$this->assertEquals(true, $stockItem->getIsInStock());
577+
$stockData = [
578+
'qty' => 3,
579+
'is_in_stock' => 0,
580+
];
581+
$product->setStockData($stockData);
582+
$product->save();
583+
584+
$stockRegistryStorage->removeStockItem($product->getId());
585+
$product = $this->productRepository->get('simple-out-of-stock', true, null, true);
586+
$stockItem = $product->getExtensionAttributes()->getStockItem();
587+
$this->assertEquals(false, $stockItem->getIsInStock());
588+
}
552589
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
\Magento\TestFramework\Helper\Bootstrap::getInstance()->reinitialize();
8+
9+
/** @var \Magento\TestFramework\ObjectManager $objectManager */
10+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
11+
12+
/** @var \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement */
13+
$categoryLinkManagement = $objectManager->get(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
14+
15+
/** @var $product \Magento\Catalog\Model\Product */
16+
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
17+
$product->isObjectNew(true);
18+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
19+
->setId(1)
20+
->setAttributeSetId(4)
21+
->setWebsiteIds([1])
22+
->setName('Simple Product')
23+
->setSku('simple-out-of-stock')
24+
->setPrice(10)
25+
->setWeight(1)
26+
->setShortDescription("Short description")
27+
->setTaxClassId(0)
28+
->setDescription('Description with <b>html tag</b>')
29+
->setMetaTitle('meta title')
30+
->setMetaKeyword('meta keyword')
31+
->setMetaDescription('meta description')
32+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
33+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
34+
->setStockData(
35+
[
36+
'use_config_manage_stock' => 1,
37+
'qty' => 0,
38+
'is_qty_decimal' => 0,
39+
'is_in_stock' => 0,
40+
]
41+
)->setCanSaveCustomOptions(true)
42+
->setHasOptions(true);
43+
44+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryFactory */
45+
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
46+
$productRepository->save($product);
47+
48+
$categoryLinkManagement->assignProductToCategories(
49+
$product->getSku(),
50+
[2]
51+
);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
use Magento\Framework\Exception\NoSuchEntityException;
7+
8+
\Magento\TestFramework\Helper\Bootstrap::getInstance()->getInstance()->reinitialize();
9+
10+
/** @var \Magento\Framework\Registry $registry */
11+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
12+
13+
$registry->unregister('isSecureArea');
14+
$registry->register('isSecureArea', true);
15+
16+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
17+
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
18+
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
19+
try {
20+
$product = $productRepository->get('simple-out-of-stock', false, null, true);
21+
$productRepository->delete($product);
22+
} catch (NoSuchEntityException $e) {
23+
}
24+
$registry->unregister('isSecureArea');
25+
$registry->register('isSecureArea', false);

dev/tests/integration/testsuite/Magento/CatalogInventory/Model/StockItemSave/ByStockItemRepositoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ByStockItemRepositoryTest extends \PHPUnit\Framework\TestCase
4040
private $stockItemData = [
4141
StockItemInterface::QTY => 555,
4242
StockItemInterface::MANAGE_STOCK => true,
43-
StockItemInterface::IS_IN_STOCK => false,
43+
StockItemInterface::IS_IN_STOCK => true,
4444
];
4545

4646
public function setUp()

dev/tests/integration/testsuite/Magento/CatalogInventory/Model/StockItemSave/OnProductCreate/ByProductModel/ByQuantityAndStockStatusTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ByQuantityAndStockStatusTest extends \PHPUnit\Framework\TestCase
5353
private $stockItemData = [
5454
StockItemInterface::QTY => 555,
5555
StockItemInterface::MANAGE_STOCK => true,
56-
StockItemInterface::IS_IN_STOCK => false,
56+
StockItemInterface::IS_IN_STOCK => true,
5757
];
5858

5959
public function setUp()

dev/tests/integration/testsuite/Magento/CatalogInventory/Model/StockItemSave/OnProductCreate/ByProductModel/ByStockDataTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ByStockDataTest extends \PHPUnit\Framework\TestCase
5353
private $stockItemData = [
5454
StockItemInterface::QTY => 555,
5555
StockItemInterface::MANAGE_STOCK => true,
56-
StockItemInterface::IS_IN_STOCK => false,
56+
StockItemInterface::IS_IN_STOCK => true,
5757
];
5858

5959
public function setUp()

dev/tests/integration/testsuite/Magento/CatalogInventory/Model/StockItemSave/OnProductCreate/ByProductModel/ByStockItemTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ByStockItemTest extends \PHPUnit\Framework\TestCase
5959
private $stockItemData = [
6060
StockItemInterface::QTY => 555,
6161
StockItemInterface::MANAGE_STOCK => true,
62-
StockItemInterface::IS_IN_STOCK => false,
62+
StockItemInterface::IS_IN_STOCK => true,
6363
];
6464

6565
public function setUp()

dev/tests/integration/testsuite/Magento/CatalogInventory/Model/StockItemSave/OnProductCreate/ByProductRepository/ByQuantityAndStockStatusTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ByQuantityAndStockStatusTest extends \PHPUnit\Framework\TestCase
5959
private $stockItemData = [
6060
StockItemInterface::QTY => 555,
6161
StockItemInterface::MANAGE_STOCK => true,
62-
StockItemInterface::IS_IN_STOCK => false,
62+
StockItemInterface::IS_IN_STOCK => true,
6363
];
6464

6565
public function setUp()

0 commit comments

Comments
 (0)