Skip to content

Commit 1ba9dd6

Browse files
committed
Merge branch 'ACP2E-2704' of https://github.com/adobe-commerce-tier-4/magento2ce into L3-PR-2024-02-16
2 parents 0d7d5b9 + 1b188ee commit 1ba9dd6

File tree

7 files changed

+81
-32
lines changed

7 files changed

+81
-32
lines changed

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Api\AttributeValueFactory;
1313
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
1414
use Magento\Framework\App\ObjectManager;
15+
use Magento\Framework\DataObject;
1516
use Magento\Framework\Exception\NoSuchEntityException;
1617
use Magento\Framework\Model\AbstractExtensibleModel;
1718
use Magento\Quote\Api\Data\PaymentInterface;
@@ -1619,7 +1620,7 @@ public function addItem(\Magento\Quote\Model\Quote\Item $item)
16191620
* Add product. Returns error message if product type instance can't prepare product.
16201621
*
16211622
* @param mixed $product
1622-
* @param null|float|\Magento\Framework\DataObject $request
1623+
* @param null|float|DataObject $request
16231624
* @param null|string $processMode
16241625
* @return \Magento\Quote\Model\Quote\Item|string
16251626
* @throws \Magento\Framework\Exception\LocalizedException
@@ -1637,11 +1638,12 @@ public function addProduct(
16371638
if (is_numeric($request)) {
16381639
$request = $this->objectFactory->create(['qty' => $request]);
16391640
}
1640-
if (!$request instanceof \Magento\Framework\DataObject) {
1641+
if (!$request instanceof DataObject) {
16411642
throw new \Magento\Framework\Exception\LocalizedException(
16421643
__('We found an invalid request for adding product to quote.')
16431644
);
16441645
}
1646+
$invalidProductAddFlag = $this->checkForInvalidProductAdd($request);
16451647

16461648
if (!$product->isSalable()) {
16471649
throw new \Magento\Framework\Exception\LocalizedException(
@@ -1699,7 +1701,9 @@ public function addProduct(
16991701

17001702
// collect errors instead of throwing first one
17011703
if ($item->getHasError()) {
1702-
$this->deleteItem($item);
1704+
if (!$invalidProductAddFlag) {
1705+
$this->deleteItem($item);
1706+
}
17031707
foreach ($item->getMessage(false) as $message) {
17041708
if (!in_array($message, $errors)) {
17051709
// filter duplicate messages
@@ -1717,6 +1721,20 @@ public function addProduct(
17171721
return $parentItem;
17181722
}
17191723

1724+
/**
1725+
* Checks if invalid products should be added to quote
1726+
*
1727+
* @param DataObject $request
1728+
* @return bool
1729+
*/
1730+
private function checkForInvalidProductAdd(DataObject $request): bool
1731+
{
1732+
$forceAdd = $request->getAddToCartInvalidProduct();
1733+
$request->unsetData('add_to_cart_invalid_product');
1734+
1735+
return (bool) $forceAdd;
1736+
}
1737+
17201738
/**
17211739
* Adding catalog product object data to quote
17221740
*
@@ -1772,8 +1790,8 @@ protected function _addCatalogProduct(\Magento\Catalog\Model\Product $product, $
17721790
* For more options see \Magento\Catalog\Helper\Product->addParamsToBuyRequest()
17731791
*
17741792
* @param int $itemId
1775-
* @param \Magento\Framework\DataObject $buyRequest
1776-
* @param null|array|\Magento\Framework\DataObject $params
1793+
* @param DataObject $buyRequest
1794+
* @param null|array|DataObject $params
17771795
* @return \Magento\Quote\Model\Quote\Item
17781796
* @throws \Magento\Framework\Exception\LocalizedException
17791797
*
@@ -1795,9 +1813,9 @@ public function updateItem($itemId, $buyRequest, $params = null)
17951813
$product = clone $this->productRepository->getById($productId, false, $this->getStore()->getId());
17961814

17971815
if (!$params) {
1798-
$params = new \Magento\Framework\DataObject();
1816+
$params = new DataObject();
17991817
} elseif (is_array($params)) {
1800-
$params = new \Magento\Framework\DataObject($params);
1818+
$params = new DataObject($params);
18011819
}
18021820
$params->setCurrentConfig($item->getBuyRequest());
18031821
$buyRequest = $this->_catalogProduct->addParamsToBuyRequest($buyRequest, $params);
@@ -2146,7 +2164,7 @@ protected function _clearErrorInfo()
21462164
* @param string|null $origin Usually a name of module, that embeds error
21472165
* @param int|null $code Error code, unique for origin, that sets it
21482166
* @param string|null $message Error message
2149-
* @param \Magento\Framework\DataObject|null $additionalData Any additional data, that caller would like to store
2167+
* @param DataObject|null $additionalData Any additional data, that caller would like to store
21502168
* @return $this
21512169
*/
21522170
public function addErrorInfo(

app/code/Magento/Quote/Test/Unit/Model/QuoteTest.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Magento\Framework\DataObject\Copy;
3131
use Magento\Framework\DataObject\Factory;
3232
use Magento\Framework\Event\Manager;
33+
use Magento\Framework\Exception\LocalizedException;
3334
use Magento\Framework\Exception\NoSuchEntityException;
3435
use Magento\Framework\Model\Context;
3536
use Magento\Framework\Phrase;
@@ -1072,17 +1073,28 @@ public function testAddProductItemPreparation(): void
10721073
}
10731074

10741075
/**
1076+
* @param $request
1077+
* @param $hasError
10751078
* @return void
1079+
* @throws LocalizedException
1080+
* @dataProvider dataProviderForTestAddProductItem
10761081
*/
1077-
public function testAddProductItemNew(): void
1082+
public function testAddProductItemNew($request, $hasError): void
10781083
{
1079-
$itemMock = $this->createMock(Item::class);
1084+
$itemMock = $this->getMockBuilder(Item::class)
1085+
->disableOriginalConstructor()
1086+
->addMethods(['getHasError'])
1087+
->onlyMethods(['representProduct', 'setProduct', 'setOptions', 'setQuote', 'getProduct'])
1088+
->getMock();
1089+
$itemMock->expects($this->once())->method('getHasError')->willReturn($hasError);
1090+
$product = $this->createMock(Product::class);
1091+
$itemMock->expects($this->any())->method('getProduct')->willReturn($product);
10801092

10811093
$expectedResult = $itemMock;
10821094
$requestMock = $this->createMock(
10831095
DataObject::class
10841096
);
1085-
$this->objectFactoryMock->expects($this->once())
1097+
$this->objectFactoryMock->expects($this->any())
10861098
->method('create')
10871099
->with(['qty' => 1])
10881100
->willReturn($requestMock);
@@ -1145,10 +1157,29 @@ public function testAddProductItemNew(): void
11451157
->method('getTypeInstance')
11461158
->willReturn($typeInstanceMock);
11471159

1148-
$result = $this->quote->addProduct($this->productMock, null);
1160+
$result = $this->quote->addProduct($this->productMock, $request);
11491161
$this->assertEquals($expectedResult, $result);
11501162
}
11511163

1164+
/**
1165+
* @return array[]
1166+
*/
1167+
public function dataProviderForTestAddProductItem(): array
1168+
{
1169+
return [
1170+
'not_invalid_product_add' => [null, false],
1171+
'invalid_product_add' => [
1172+
new DataObject(
1173+
[
1174+
'add_to_cart_invalid_product' => true,
1175+
'qty' => 1
1176+
]
1177+
),
1178+
true
1179+
]
1180+
];
1181+
}
1182+
11521183
/**
11531184
* @return void
11541185
*/

app/code/Magento/Sales/Controller/AbstractController/Reorder.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ public function execute()
9494
// to session for guest customer, as it does \Magento\Checkout\Model\Cart::save which is deprecated.
9595
$this->checkoutSession->setQuoteId($reorderOutput->getCart()->getId());
9696

97-
$errors = $reorderOutput->getErrors();
98-
if (!empty($errors)) {
99-
$useNotice = $this->_objectManager->get(\Magento\Checkout\Model\Session::class)->getUseNotice(true);
100-
foreach ($errors as $error) {
101-
$useNotice
102-
? $this->messageManager->addNoticeMessage($error->getMessage())
103-
: $this->messageManager->addErrorMessage($error->getMessage());
104-
}
105-
}
106-
10797
return $resultRedirect->setPath('checkout/cart');
10898
}
10999
}

app/code/Magento/Sales/Model/Reorder/Reorder.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ class Reorder
111111
*/
112112
private $storeManager;
113113

114+
/**
115+
* @var bool
116+
*/
117+
private bool $addToCartInvalidProduct;
118+
114119
/**
115120
* @param OrderFactory $orderFactory
116121
* @param CustomerCartResolver $customerCartProvider
@@ -121,6 +126,9 @@ class Reorder
121126
* @param ProductCollectionFactory $productCollectionFactory
122127
* @param OrderInfoBuyRequestGetter $orderInfoBuyRequestGetter
123128
* @param StoreManagerInterface|null $storeManager
129+
* @param bool $addToCartInvalidProduct
130+
*
131+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
124132
*/
125133
public function __construct(
126134
OrderFactory $orderFactory,
@@ -131,7 +139,8 @@ public function __construct(
131139
LoggerInterface $logger,
132140
ProductCollectionFactory $productCollectionFactory,
133141
OrderInfoBuyRequestGetter $orderInfoBuyRequestGetter,
134-
?StoreManagerInterface $storeManager = null
142+
?StoreManagerInterface $storeManager = null,
143+
bool $addToCartInvalidProduct = false
135144
) {
136145
$this->orderFactory = $orderFactory;
137146
$this->cartRepository = $cartRepository;
@@ -143,6 +152,7 @@ public function __construct(
143152
$this->orderInfoBuyRequestGetter = $orderInfoBuyRequestGetter;
144153
$this->storeManager = $storeManager
145154
?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
155+
$this->addToCartInvalidProduct = $addToCartInvalidProduct;
146156
}
147157

148158
/**
@@ -276,6 +286,7 @@ private function addItemToCart(OrderItemInterface $orderItem, Quote $cart, Produ
276286

277287
$addProductResult = null;
278288
try {
289+
$infoBuyRequest->setAddToCartInvalidProduct($this->addToCartInvalidProduct);
279290
$addProductResult = $cart->addProduct($product, $infoBuyRequest);
280291
} catch (\Magento\Framework\Exception\LocalizedException $e) {
281292
$this->addError($this->getCartItemErrorMessage($orderItem, $product, $e->getMessage()));

app/code/Magento/Sales/etc/frontend/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@
2222
</argument>
2323
</arguments>
2424
</type>
25+
<type name="Magento\Sales\Model\Reorder\Reorder">
26+
<arguments>
27+
<argument name="addToCartInvalidProduct" xsi:type="boolean">true</argument>
28+
</arguments>
29+
</type>
2530
</config>

dev/tests/integration/testsuite/Magento/Sales/Controller/Order/ReorderTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,7 @@ public function testReorderProductLowQty(): void
108108
$order = $this->orderFactory->create()->loadByIncrementId('55555555');
109109
$this->customerSession->setCustomerId($order->getCustomerId());
110110
$this->dispatchReorderRequest((int)$order->getId());
111-
$origMessage = (string)__('The requested qty is not available');
112-
$message = $this->escaper->escapeHtml(
113-
__('Could not add the product with SKU "%1" to the shopping cart: %2', 'simple-1', $origMessage)
114-
);
115-
$constraint = $this->logicalOr($this->containsEqual($origMessage), $this->containsEqual($message));
116-
$this->assertThat($this->getMessages(MessageInterface::TYPE_ERROR), $constraint);
117-
$this->quote = $this->checkoutSession->getQuote();
111+
$this->assertRedirect($this->stringContains('checkout/cart'));
118112
}
119113

120114
/**

dev/tests/integration/testsuite/Magento/Sales/Controller/Order/ReorderWithDifferentStoreTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function testReorderWithDifferentStoreAndGlobalCustomerAccount(): void
117117
$this->dispatch('sales/order/reorder/');
118118
$this->assertRedirect($this->stringContains('checkout/cart'));
119119
$this->quote = $this->checkoutSession->getQuote();
120-
$quoteItemsCollection = $this->quote->getAllItems();
121-
$this->assertCount(0, $quoteItemsCollection);
120+
121+
$this->assertCount(1, $this->quote->getErrors());
122122
}
123123
}

0 commit comments

Comments
 (0)