Skip to content

Commit e088db1

Browse files
committed
issue-#25675 Added fix for #25675 issue to the 2.4 Magento version
1 parent 90a479f commit e088db1

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\CatalogInventory\Model\Quote\Item\QuantityValidator;
77

8+
/**
9+
* Class QuoteItemQtyList collects qty of quote items
10+
*/
811
class QuoteItemQtyList
912
{
1013
/**
@@ -17,6 +20,7 @@ class QuoteItemQtyList
1720

1821
/**
1922
* Get product qty includes information from all quote items
23+
*
2024
* Need be used only in singleton mode
2125
*
2226
* @param int $productId
@@ -29,18 +33,18 @@ class QuoteItemQtyList
2933
public function getQty($productId, $quoteItemId, $quoteId, $itemQty)
3034
{
3135
$qty = $itemQty;
32-
if (isset(
33-
$this->_checkedQuoteItems[$quoteId][$productId]['qty']
34-
) && !in_array(
36+
if (isset($this->_checkedQuoteItems[$quoteId][$productId]['qty']) && !in_array(
3537
$quoteItemId,
3638
$this->_checkedQuoteItems[$quoteId][$productId]['items']
3739
)
3840
) {
3941
$qty += $this->_checkedQuoteItems[$quoteId][$productId]['qty'];
4042
}
4143

42-
$this->_checkedQuoteItems[$quoteId][$productId]['qty'] = $qty;
43-
$this->_checkedQuoteItems[$quoteId][$productId]['items'][] = $quoteItemId;
44+
if ($quoteItemId !== null) {
45+
$this->_checkedQuoteItems[$quoteId][$productId]['qty'] = $qty;
46+
$this->_checkedQuoteItems[$quoteId][$productId]['items'][] = $quoteItemId;
47+
}
4448

4549
return $qty;
4650
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogInventory\Test\Unit\Model\Quote\Item\QuantityValidator;
8+
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use PHPUnit\Framework\TestCase;
11+
use Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\QuoteItemQtyList;
12+
13+
/**
14+
* Class QuoteItemQtyListTest
15+
*/
16+
class QuoteItemQtyListTest extends TestCase
17+
{
18+
/**
19+
* @var QuoteItemQtyList
20+
*/
21+
private $quoteItemQtyList;
22+
23+
/**
24+
* @var int
25+
*/
26+
private $itemQtyTestValue;
27+
28+
/**
29+
* Sets up the fixture, for example, open a network connection.
30+
* This method is called before a test is executed.
31+
*/
32+
protected function setUp()
33+
{
34+
$objectManagerHelper = new ObjectManager($this);
35+
$this->quoteItemQtyList = $objectManagerHelper->getObject(QuoteItemQtyList::class);
36+
}
37+
38+
/**
39+
* This tests the scenario when item has not quote_item_id and after save gets a value.
40+
*
41+
* @return void
42+
*/
43+
public function testSingleQuoteItemQty()
44+
{
45+
$this->itemQtyTestValue = 1;
46+
$qty = $this->quoteItemQtyList->getQty(125, null, 11232, 1);
47+
$this->assertEquals($this->itemQtyTestValue, $qty);
48+
49+
$qty = $this->quoteItemQtyList->getQty(125, 1, 11232, 1);
50+
$this->assertEquals($this->itemQtyTestValue, $qty);
51+
}
52+
53+
/**
54+
* This tests the scenario when item has been added twice to the cart.
55+
*
56+
* @return void
57+
*/
58+
public function testMultipleQuoteItemQty()
59+
{
60+
$this->itemQtyTestValue = 1;
61+
$qty = $this->quoteItemQtyList->getQty(127, 1, 112, 1);
62+
$this->assertEquals($this->itemQtyTestValue, $qty);
63+
64+
$this->itemQtyTestValue = 2;
65+
$qty = $this->quoteItemQtyList->getQty(127, 2, 112, 1);
66+
$this->assertEquals($this->itemQtyTestValue, $qty);
67+
}
68+
}

0 commit comments

Comments
 (0)