Skip to content

Commit 2a0942a

Browse files
Merge remote-tracking branch 'origin/MC-35836' into 2.4-develop-pr35
2 parents 2abab6d + 91d9a73 commit 2a0942a

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-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
@@ -3079,6 +3079,9 @@ private function formatStockDataForRow(array $rowData): array
30793079
);
30803080

30813081
if ($this->stockConfiguration->isQty($this->skuProcessor->getNewSku($sku)['type_id'])) {
3082+
if (isset($rowData['qty']) && $rowData['qty'] == 0) {
3083+
$row['is_in_stock'] = 0;
3084+
}
30823085
$stockItemDo->setData($row);
30833086
$row['is_in_stock'] = $row['is_in_stock'] ?? $this->stockStateProvider->verifyStock($stockItemDo);
30843087
if ($this->stockStateProvider->verifyNotification($stockItemDo)) {

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,59 @@ 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+
* @return void
305+
*/
306+
public function testSaveIsInStockByZeroQty(): void
307+
{
308+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
309+
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
310+
\Magento\Catalog\Api\ProductRepositoryInterface::class
311+
);
312+
$id1 = $productRepository->get('simple1')->getId();
313+
$id2 = $productRepository->get('simple2')->getId();
314+
$id3 = $productRepository->get('simple3')->getId();
315+
$existingProductIds = [$id1, $id2, $id3];
316+
317+
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
318+
->create(\Magento\Framework\Filesystem::class);
319+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
320+
$source = $this->objectManager->create(
321+
\Magento\ImportExport\Model\Import\Source\Csv::class,
322+
[
323+
'file' => __DIR__ . '/_files/products_to_import_zero_qty.csv',
324+
'directory' => $directory
325+
]
326+
);
327+
$errors = $this->_model->setParameters(
328+
['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product']
329+
)->setSource(
330+
$source
331+
)->validateData();
332+
333+
$this->assertTrue($errors->getErrorsCount() == 0);
334+
335+
$this->_model->importData();
336+
337+
/** @var $stockItmBeforeImport \Magento\CatalogInventory\Model\Stock\Item */
338+
foreach ($existingProductIds as $productId) {
339+
/** @var $stockRegistry StockRegistry */
340+
$stockRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
341+
StockRegistry::class
342+
);
343+
344+
$stockItemAfterImport = $stockRegistry->getStockItem($productId, 1);
345+
346+
$this->assertEquals(0, $stockItemAfterImport->getIsInStock());
347+
unset($stockItemAfterImport);
348+
}
349+
}
350+
298351
/**
299352
* Test if stock state properly changed after import
300353
*
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)