Skip to content

Commit d322368

Browse files
committed
Merge remote-tracking branch 'origin/MC-24466' into 2.4-develop-pr13
2 parents 03c84ec + 0c24621 commit d322368

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

app/code/Magento/Quote/Model/Quote/Item/CartItemPersister.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66

77
namespace Magento\Quote\Model\Quote\Item;
88

9-
use Magento\Quote\Api\Data\CartInterface;
10-
use Magento\Quote\Api\Data\CartItemInterface;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
1110
use Magento\Framework\Exception\CouldNotSaveException;
1211
use Magento\Framework\Exception\InputException;
13-
use Magento\Framework\Exception\NoSuchEntityException;
1412
use Magento\Framework\Exception\LocalizedException;
15-
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Quote\Api\Data\CartInterface;
15+
use Magento\Quote\Api\Data\CartItemInterface;
1616

17+
/**
18+
* Cart item save handler
19+
*/
1720
class CartItemPersister
1821
{
1922
/**
@@ -39,6 +42,8 @@ public function __construct(
3942
}
4043

4144
/**
45+
* Save cart item into cart
46+
*
4247
* @param CartInterface $quote
4348
* @param CartItemInterface $item
4449
* @return CartItemInterface
@@ -73,12 +78,13 @@ public function save(CartInterface $quote, CartItemInterface $item)
7378
$item = $quote->updateItem($itemId, $buyRequestData);
7479
} else {
7580
if ($item->getQty() !== $currentItem->getQty()) {
81+
$currentItem->clearMessage();
7682
$currentItem->setQty($qty);
7783
/**
7884
* Qty validation errors are stored as items message
7985
* @see \Magento\CatalogInventory\Model\Quote\Item\QuantityValidator::validate
8086
*/
81-
if (!empty($currentItem->getMessage())) {
87+
if (!empty($currentItem->getMessage()) && $currentItem->getHasError()) {
8288
throw new LocalizedException(__($currentItem->getMessage()));
8389
}
8490
}

dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartItemRepositoryTest.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
namespace Magento\Quote\Api;
88

9+
use Magento\CatalogInventory\Api\StockRegistryInterface;
10+
use Magento\CatalogInventory\Model\Stock;
911
use Magento\TestFramework\TestCase\WebapiAbstract;
1012

1113
class GuestCartItemRepositoryTest extends WebapiAbstract
@@ -167,9 +169,13 @@ public function testRemoveItem()
167169

168170
/**
169171
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
172+
* @param array $stockData
173+
* @param string|null $errorMessage
174+
* @dataProvider updateItemDataProvider
170175
*/
171-
public function testUpdateItem()
176+
public function testUpdateItem(array $stockData, string $errorMessage = null)
172177
{
178+
$this->updateStockData('simple_one', $stockData);
173179
/** @var \Magento\Quote\Model\Quote $quote */
174180
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
175181
$quote->load('test_order_item_with_items', 'reserved_order_id');
@@ -215,6 +221,9 @@ public function testUpdateItem()
215221
],
216222
];
217223
}
224+
if ($errorMessage) {
225+
$this->expectExceptionMessage($errorMessage);
226+
}
218227
$this->_webApiCall($serviceInfo, $requestData);
219228
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
220229
$quote->load('test_order_item_with_items', 'reserved_order_id');
@@ -223,4 +232,66 @@ public function testUpdateItem()
223232
$this->assertEquals(5, $item->getQty());
224233
$this->assertEquals($itemId, $item->getItemId());
225234
}
235+
236+
/**
237+
* @return array
238+
*/
239+
public function updateItemDataProvider(): array
240+
{
241+
return [
242+
[
243+
[]
244+
],
245+
[
246+
[
247+
'qty' => 0,
248+
'is_in_stock' => 1,
249+
'use_config_manage_stock' => 0,
250+
'manage_stock' => 1,
251+
'use_config_backorders' => 0,
252+
'backorders' => Stock::BACKORDERS_YES_NOTIFY,
253+
]
254+
],
255+
[
256+
[
257+
'qty' => 0,
258+
'is_in_stock' => 1,
259+
'use_config_manage_stock' => 0,
260+
'manage_stock' => 1,
261+
'use_config_backorders' => 0,
262+
'backorders' => Stock::BACKORDERS_NO,
263+
],
264+
'This product is out of stock.'
265+
],
266+
[
267+
[
268+
'qty' => 2,
269+
'is_in_stock' => 1,
270+
'use_config_manage_stock' => 0,
271+
'manage_stock' => 1,
272+
'use_config_backorders' => 0,
273+
'backorders' => Stock::BACKORDERS_NO,
274+
],
275+
'The requested qty is not available'
276+
]
277+
];
278+
}
279+
280+
/**
281+
* Update product stock
282+
*
283+
* @param string $sku
284+
* @param array $stockData
285+
* @return void
286+
*/
287+
private function updateStockData(string $sku, array $stockData): void
288+
{
289+
if ($stockData) {
290+
/** @var $stockRegistry StockRegistryInterface */
291+
$stockRegistry = $this->objectManager->create(StockRegistryInterface::class);
292+
$stockItem = $stockRegistry->getStockItemBySku($sku);
293+
$stockItem->addData($stockData);
294+
$stockRegistry->updateStockItemBySku($sku, $stockItem);
295+
}
296+
}
226297
}

0 commit comments

Comments
 (0)