Skip to content

Forwardport-pull-13494: Fixing of Problem with updating stock item qty and stock status #13638

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

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private function prepareQuantityAndStockStatus(StockItemInterface $stockItem, ar
) {
unset($quantityAndStockStatus['is_in_stock']);
}
if (isset($quantityAndStockStatus['qty'])
if (array_key_exists('qty', $quantityAndStockStatus)
&& $stockItem->getQty() == $quantityAndStockStatus['qty']
) {
unset($quantityAndStockStatus['qty']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class ProductRepositoryInterfaceTest extends WebapiAbstract
const KEY_PRODUCT_ID = StockStatusInterface::PRODUCT_ID;
const KEY_CUSTOM_ATTRIBUTES = 'custom_attributes';
const KEY_ATTRIBUTE_CODE = \Magento\Eav\Api\Data\AttributeInterface::ATTRIBUTE_CODE;
const CODE_QUANTITY_AND_STOCK_STATUS = 'quantity_and_stock_status';
const KEY_IS_IN_STOCK = 'is_in_stock';

const CODE_QUANTITY_AND_STOCK_STATUS = 'quantity_and_stock_status';
const PRODUCT_SKU = 'sku-test-catalog-inventory';

/**
Expand Down Expand Up @@ -159,6 +160,36 @@ public function testSimpleProductCreationWithoutSpecifyingCatalogInventory()
$this->assertTrue($response);
}

/**
* Tests updating product stock item data when previously product was created without specified stock_item
*/
public function testUpdatingQuantity()
{
// create a simple product with catalog inventory
$qty = null;
$productData = $this->getSimpleProductData($qty);
$response = $this->saveProduct($productData);
$stockItemData = $response[self::KEY_EXTENSION_ATTRIBUTES][self::KEY_STOCK_ITEM];

$this->assertEquals($qty, $stockItemData[self::KEY_QTY]);
$this->assertEquals(false, $stockItemData[self::KEY_IS_IN_STOCK]);

// update a created product with catalog inventory
$qty = 1;
$inStock = true;
$response[self::KEY_EXTENSION_ATTRIBUTES][self::KEY_STOCK_ITEM][self::KEY_QTY] = $qty;
$response[self::KEY_EXTENSION_ATTRIBUTES][self::KEY_STOCK_ITEM][self::KEY_IS_IN_STOCK] = $inStock;
$responseUpdated = $this->updateProduct($response);
$stockItemDataUpdated = $responseUpdated[self::KEY_EXTENSION_ATTRIBUTES][self::KEY_STOCK_ITEM];

$this->assertEquals($qty, $stockItemDataUpdated[self::KEY_QTY]);
$this->assertEquals($inStock, $stockItemDataUpdated[self::KEY_IS_IN_STOCK]);

// delete the product; expect that all goes well
$response = $this->deleteProduct($productData[ProductInterface::SKU]);
$this->assertTrue($response);
}

// --- my helpers -----------------------------------------------------------------------------

/**
Expand Down Expand Up @@ -195,7 +226,7 @@ protected function getSimpleProductData($qty = 1000)
[self::KEY_ATTRIBUTE_CODE => 'description', 'value' => 'My Product Description'],
[
self::KEY_ATTRIBUTE_CODE => self::CODE_QUANTITY_AND_STOCK_STATUS,
'value' => ['is_in_stock' => true, 'qty' => $qty]
'value' => [self::KEY_IS_IN_STOCK => true, 'qty' => $qty]
],
];
}
Expand All @@ -214,7 +245,7 @@ protected function getStockItemData($qty = 1000)
return [
self::KEY_STOCK_ITEM => [
self::KEY_QTY => $qty,
'is_in_stock' => true,
self::KEY_IS_IN_STOCK => true,
'is_qty_decimal' => false,
'show_default_notification_message' => false,
'use_config_min_qty' => true,
Expand Down