Skip to content

Commit 78bb188

Browse files
committed
MAGETWO-54740: Imported products are incorrectly in stock if 'allow_backorders' enabled
1 parent 9415d45 commit 78bb188

File tree

1 file changed

+51
-35
lines changed
  • app/code/Magento/CatalogImportExport/Model/Import

1 file changed

+51
-35
lines changed

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

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\CatalogImportExport\Model\Import;
79

810
use Magento\Catalog\Model\Config as CatalogConfig;
@@ -2116,41 +2118,7 @@ protected function _saveStockItem()
21162118
$row = [];
21172119
$sku = $rowData[self::COL_SKU];
21182120
if ($this->skuProcessor->getNewSku($sku) !== null) {
2119-
$row['product_id'] = $this->skuProcessor->getNewSku($sku)['entity_id'];
2120-
$productIdsToReindex[] = $row['product_id'];
2121-
2122-
$row['website_id'] = $this->stockConfiguration->getDefaultScopeId();
2123-
$row['stock_id'] = $this->stockRegistry->getStock($row['website_id'])->getStockId();
2124-
2125-
$stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']);
2126-
$existStockData = $stockItemDo->getData();
2127-
2128-
$row = array_merge(
2129-
$this->defaultStockData,
2130-
array_intersect_key($existStockData, $this->defaultStockData),
2131-
array_intersect_key($rowData, $this->defaultStockData),
2132-
$row
2133-
);
2134-
2135-
if ($this->stockConfiguration->isQty(
2136-
$this->skuProcessor->getNewSku($sku)['type_id']
2137-
)
2138-
) {
2139-
$stockItemDo->setData($row);
2140-
$row['is_in_stock'] = $stockItemDo->getBackorders() && isset($row['is_in_stock'])
2141-
? $row['is_in_stock']
2142-
: $this->stockStateProvider->verifyStock($stockItemDo);
2143-
if ($this->stockStateProvider->verifyNotification($stockItemDo)) {
2144-
$row['low_stock_date'] = $this->dateTime->gmDate(
2145-
'Y-m-d H:i:s',
2146-
(new \DateTime())->getTimestamp()
2147-
);
2148-
}
2149-
$row['stock_status_changed_auto'] =
2150-
(int)!$this->stockStateProvider->verifyStock($stockItemDo);
2151-
} else {
2152-
$row['qty'] = 0;
2153-
}
2121+
$row = $this->formatStockDataForRow($rowData);
21542122
}
21552123

21562124
if (!isset($stockData[$sku])) {
@@ -2842,4 +2810,52 @@ private function getExistingSku($sku)
28422810
{
28432811
return $this->_oldSku[strtolower($sku)];
28442812
}
2813+
2814+
/**
2815+
* Format row data to DB compatible values
2816+
*
2817+
* @param array $rowData
2818+
* @return array
2819+
*/
2820+
private function formatStockDataForRow(array $rowData) :array
2821+
{
2822+
$sku = $rowData[self::COL_SKU];
2823+
$row['product_id'] = $this->skuProcessor->getNewSku($sku)['entity_id'];
2824+
$productIdsToReindex[] = $row['product_id'];
2825+
2826+
$row['website_id'] = $this->stockConfiguration->getDefaultScopeId();
2827+
$row['stock_id'] = $this->stockRegistry->getStock($row['website_id'])->getStockId();
2828+
2829+
$stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']);
2830+
$existStockData = $stockItemDo->getData();
2831+
2832+
$row = array_merge(
2833+
$this->defaultStockData,
2834+
array_intersect_key($existStockData, $this->defaultStockData),
2835+
array_intersect_key($rowData, $this->defaultStockData),
2836+
$row
2837+
);
2838+
2839+
if ($this->stockConfiguration->isQty(
2840+
$this->skuProcessor->getNewSku($sku)['type_id']
2841+
)
2842+
) {
2843+
$stockItemDo->setData($row);
2844+
$row['is_in_stock'] = $stockItemDo->getBackorders() && isset($row['is_in_stock'])
2845+
? $row['is_in_stock']
2846+
: $this->stockStateProvider->verifyStock($stockItemDo);
2847+
if ($this->stockStateProvider->verifyNotification($stockItemDo)) {
2848+
$row['low_stock_date'] = $this->dateTime->gmDate(
2849+
'Y-m-d H:i:s',
2850+
(new \DateTime())->getTimestamp()
2851+
);
2852+
}
2853+
$row['stock_status_changed_auto'] =
2854+
(int)!$this->stockStateProvider->verifyStock($stockItemDo);
2855+
} else {
2856+
$row['qty'] = 0;
2857+
}
2858+
2859+
return $row;
2860+
}
28452861
}

0 commit comments

Comments
 (0)