Skip to content

Commit 6b1d8c5

Browse files
Merge remote-tracking branch 'origin/MC-35634' into 2.3-develop-pr52
2 parents e7268db + 46c5b9c commit 6b1d8c5

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,6 +3074,9 @@ private function formatStockDataForRow(array $rowData): array
30743074
);
30753075

30763076
if ($this->stockConfiguration->isQty($this->skuProcessor->getNewSku($sku)['type_id'])) {
3077+
if (isset($rowData['qty']) && $rowData['qty'] == 0) {
3078+
$row['is_in_stock'] = 0;
3079+
}
30773080
$stockItemDo->setData($row);
30783081
$row['is_in_stock'] = $row['is_in_stock'] ?? $this->stockStateProvider->verifyStock($stockItemDo);
30793082
if ($this->stockStateProvider->verifyNotification($stockItemDo)) {

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,57 @@ public function testSaveStockItemQty()
295295
unset($stockItems, $stockItem);
296296
}
297297

298+
/**
299+
* Test that is_in_stock set to 0 when item quantity is 0
300+
*
301+
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
302+
* @magentoAppIsolation enabled
303+
*/
304+
public function testSaveIsInStockByZeroQty()
305+
{
306+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
307+
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
308+
\Magento\Catalog\Api\ProductRepositoryInterface::class
309+
);
310+
$id1 = $productRepository->get('simple1')->getId();
311+
$id2 = $productRepository->get('simple2')->getId();
312+
$id3 = $productRepository->get('simple3')->getId();
313+
$existingProductIds = [$id1, $id2, $id3];
314+
315+
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
316+
->create(\Magento\Framework\Filesystem::class);
317+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
318+
$source = $this->objectManager->create(
319+
\Magento\ImportExport\Model\Import\Source\Csv::class,
320+
[
321+
'file' => __DIR__ . '/_files/products_to_import_zero_qty.csv',
322+
'directory' => $directory
323+
]
324+
);
325+
$errors = $this->_model->setParameters(
326+
['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product']
327+
)->setSource(
328+
$source
329+
)->validateData();
330+
331+
$this->assertTrue($errors->getErrorsCount() == 0);
332+
333+
$this->_model->importData();
334+
335+
/** @var $stockItmBeforeImport \Magento\CatalogInventory\Model\Stock\Item */
336+
foreach ($existingProductIds as $productId) {
337+
/** @var $stockRegistry StockRegistry */
338+
$stockRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
339+
StockRegistry::class
340+
);
341+
342+
$stockItemAfterImport = $stockRegistry->getStockItem($productId, 1);
343+
344+
$this->assertEquals(0, $stockItemAfterImport->getIsInStock());
345+
unset($stockItemAfterImport);
346+
}
347+
}
348+
298349
/**
299350
* Test if stock state properly changed after import
300351
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sku,qty,is_in_stock
2+
simple1,0,1
3+
simple2,0,1
4+
simple3,0,1

0 commit comments

Comments
 (0)