Skip to content

Fixes issue with backorders enabled and qty < 0, is_in_stock = 1 #32366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -3176,7 +3176,8 @@ private function formatStockDataForRow(array $rowData): array
$stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']);
$existStockData = $stockItemDo->getData();

if (isset($rowData['qty']) && $rowData['qty'] == 0 && !isset($rowData['is_in_stock'])) {
//If qty is <= 0 and is_in_stock is not set, we should set it to 0
if (isset($rowData['qty']) && $rowData['qty'] <= 0 && !isset($rowData['is_in_stock'])) {
$rowData['is_in_stock'] = 0;
}

Expand All @@ -3188,11 +3189,19 @@ private function formatStockDataForRow(array $rowData): array
);

if ($this->stockConfiguration->isQty($this->skuProcessor->getNewSku($sku)['type_id'])) {
if (isset($rowData['qty']) && $rowData['qty'] == 0) {

//if qty is 0 and backorders are not enabled, is_in_stock should become 0.
if (isset($rowData['qty']) && $rowData['qty'] <= 0 && !$stockItemDo->getBackorders()) {
$row['is_in_stock'] = 0;
}

$stockItemDo->setData($row);
$row['is_in_stock'] = $row['is_in_stock'] ?? $this->stockStateProvider->verifyStock($stockItemDo);

//if back-orders are not enabled, we can verify the stock.
if (!$stockItemDo->getBackorders()) {
$row['is_in_stock'] = $row['is_in_stock'] ?? $this->stockStateProvider->verifyStock($stockItemDo);
}

if ($this->stockStateProvider->verifyNotification($stockItemDo)) {
$date = $this->dateTimeFactory->create('now', new \DateTimeZone('UTC'));
$row['low_stock_date'] = $date->format(DateTime::DATETIME_PHP_FORMAT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2909,6 +2909,20 @@ public function testImportWithBackordersDisabled(): void
$this->assertFalse($product->getDataByKey('quantity_and_stock_status')['is_in_stock']);
}

/**
* Test that imported product stock status with stock quantity < 0 and backorders functionality enabled
* can be set to 'In stock'.
*
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*/
public function testInStockQtyEqualOrBelowZeroImportWithBackordersEnabled(): void
{
$this->importFile('products_to_import_with_backorders_enabled_in_stock_and_qty_0.csv');
$product = $this->getProductBySku('simple_new');
$this->assertTrue($product->getDataByKey('quantity_and_stock_status')['is_in_stock']);
}

/**
* Import file by providing import filename and bunch size.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sku,store_view_code,attribute_set_code,product_type,categories,product_websites,name,description,short_description,weight,product_online,tax_class_name,visibility,price,special_price,special_price_from_date,special_price_to_date,url_key,meta_title,meta_keywords,meta_description,base_image,base_image_label,small_image,small_image_label,thumbnail_image,thumbnail_image_label,created_at,updated_at,new_from_date,new_to_date,display_product_options_in,map_price,msrp_price,map_enabled,gift_message_available,custom_design,custom_design_from,custom_design_to,custom_layout_update,page_layout,product_options_container,msrp_display_actual_price_type,country_of_manufacture,additional_attributes,qty,out_of_stock_qty,use_config_min_qty,is_qty_decimal,allow_backorders,use_config_backorders,min_cart_qty,use_config_min_sale_qty,max_cart_qty,use_config_max_sale_qty,is_in_stock,notify_on_stock_below,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,use_config_qty_increments,qty_increments,use_config_enable_qty_inc,enable_qty_increments,is_decimal_divided,website_id,related_skus,crosssell_skus,upsell_skus,additional_images,additional_image_labels,hide_from_product_page,custom_options,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values,associated_skus
simple_new,,Default,simple,,base,New Product,,,,1,Taxable Goods,"Catalog, Search",10,,,,new-product,New Product,New Product,New Product ,,,,,,,10/20/2015 7:05,10/20/2015 7:05,,,Block after Info Column,,,,,,,,,,,,,"has_options=1,quantity_and_stock_status=In Stock,required_options=1",0,0,1,0,1,0,1,1,10000,1,1,1,1,1,0,1,1,0,0,0,1,,,,,,,,,,,,,