Skip to content

Commit 60e6c68

Browse files
MC-39104: 2 error messages in Cart when product is out of stock
1 parent 7f12792 commit 60e6c68

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

app/code/Magento/CatalogInventory/Model/Quote/Item/QuantityValidator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ public function validate(Observer $observer)
157157
if ($stockStatus->getStockStatus() === Stock::STOCK_OUT_OF_STOCK
158158
|| $parentStockStatus && $parentStockStatus->getStockStatus() == Stock::STOCK_OUT_OF_STOCK
159159
) {
160-
if (!$quoteItem->getStockStateResult() && !$quoteItem->getStockStateResult()->getHasError()) {
160+
$hasError = $quoteItem->getStockStateResult()
161+
? $quoteItem->getStockStateResult()->getHasError() : false;
162+
if (!$hasError) {
161163
$quoteItem->addErrorInfo(
162164
'cataloginventory',
163165
Data::ERROR_QTY,

app/code/Magento/CatalogInventory/Test/Unit/Model/Quote/Item/QuantityValidator/Initializer/QuantityValidatorTest.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ protected function setUp(): void
153153
->getMock();
154154
$this->storeMock = $this->createMock(Store::class);
155155
$this->quoteItemMock = $this->getMockBuilder(Item::class)
156-
->addMethods(['getProductId', 'getHasError'])
156+
->addMethods(['getProductId', 'getHasError', 'getStockStateResult'])
157157
->onlyMethods(
158158
[
159159
'getQuote',
@@ -460,6 +460,54 @@ public function testException()
460460
$this->quantityValidator->validate($this->observerMock);
461461
}
462462

463+
/**
464+
* This tests the scenario when the error is in the quote item already
465+
*
466+
* @return void
467+
*/
468+
public function testValidateOutStockWithAlreadyErrorInQuoteItem(): void
469+
{
470+
$this->createInitialStub(1);
471+
$resultMock = $this->getMockBuilder(DataObject::class)
472+
->addMethods(['checkQtyIncrements', 'getMessage', 'getQuoteMessage', 'getHasError'])
473+
->getMock();
474+
$resultMock->method('getHasError')
475+
->willReturn(true);
476+
$this->stockRegistryMock->method('getStockItem')
477+
->willReturn($this->stockItemMock);
478+
$this->stockRegistryMock->expects($this->at(1))
479+
->method('getStockStatus')
480+
->willReturn($this->stockStatusMock);
481+
$this->quoteItemMock->method('getParentItem')
482+
->willReturn($this->parentItemMock);
483+
$this->quoteItemMock->method('getStockStateResult')
484+
->willReturn($resultMock);
485+
$this->stockRegistryMock->expects($this->at(2))
486+
->method('getStockStatus')
487+
->willReturn($this->parentStockItemMock);
488+
$this->parentStockItemMock->method('getStockStatus')
489+
->willReturn(0);
490+
$this->stockStatusMock->expects($this->atLeastOnce())
491+
->method('getStockStatus')
492+
->willReturn(1);
493+
$this->quoteItemMock->expects($this->never())
494+
->method('addErrorInfo')
495+
->with(
496+
'cataloginventory',
497+
Data::ERROR_QTY,
498+
__('This product is out of stock.')
499+
);
500+
$this->quoteMock->expects($this->once())
501+
->method('addErrorInfo')
502+
->with(
503+
'stock',
504+
'cataloginventory',
505+
Data::ERROR_QTY,
506+
__('Some of the products are out of stock.')
507+
);
508+
$this->quantityValidator->validate($this->observerMock);
509+
}
510+
463511
/**
464512
* @param $qty
465513
* @param $hasError

0 commit comments

Comments
 (0)