Skip to content

Commit 3d228f3

Browse files
committed
Fix message when maxSaleQty is set and qty is more than maxSaleQty
1 parent 584b8f8 commit 3d228f3

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,7 @@ public function __construct(
5050
public function execute(Quote $cart, array $cartItems): void
5151
{
5252
foreach ($cartItems as $cartItemData) {
53-
try {
54-
$this->addProductToCart->execute($cart, $cartItemData);
55-
} catch (\Exception $error) {
56-
throw new GraphQlInputException(
57-
__('Shopping cart error: %message', ['message' => $error->getMessage()])
58-
);
59-
}
53+
$this->addProductToCart->execute($cart, $cartItemData);
6054
}
6155

6256
$this->cartRepository->save($cart);

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductToCartTest.php

+59-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ protected function setUp()
4141
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
4242
}
4343

44+
/**
45+
* @magentoApiDataFixture Magento/Catalog/_files/products.php
46+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
47+
*/
48+
public function testAddSimpleProductsToCart()
49+
{
50+
$sku = 'simple';
51+
$qty = 2;
52+
$maskedQuoteId = $this->getMaskedQuoteId();
53+
$query = $this->getQueryAddSimpleProduct($maskedQuoteId, $sku, $qty);
54+
$response = $this->graphQlQuery($query);
55+
self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']);
56+
$cartQty = $response['addSimpleProductsToCart']['cart']['items'][0]['qty'];
57+
58+
$this->assertEquals($qty, $cartQty);
59+
}
60+
4461
/**
4562
* @magentoApiDataFixture Magento/Catalog/_files/products.php
4663
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
@@ -52,14 +69,51 @@ public function testAddProductIfQuantityIsNotAvailable()
5269
$sku = 'simple';
5370
$qty = 200;
5471

72+
$maskedQuoteId = $this->getMaskedQuoteId();
73+
$query = $this->getQueryAddSimpleProduct($maskedQuoteId, $sku, $qty);
74+
$this->graphQlQuery($query);
75+
}
76+
77+
/**
78+
* @magentoApiDataFixture Magento/Catalog/_files/products.php
79+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
80+
* @expectedException \Exception
81+
* @expectedExceptionMessage The most you may purchase is 10000.
82+
*/
83+
public function testAddMoreProductsThatAllowed()
84+
{
85+
$sku = 'simple-product-with-huge-amount';
86+
$qty = 20000;
87+
$maskedQuoteId = $this->getMaskedQuoteId();
88+
$query = $this->getQueryAddSimpleProduct($maskedQuoteId, $sku, $qty);
89+
$this->graphQlQuery($query);
90+
}
91+
92+
/**
93+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
94+
* @return string
95+
* @throws \Magento\Framework\Exception\NoSuchEntityException
96+
*/
97+
public function getMaskedQuoteId()
98+
{
5599
$this->quoteResource->load(
56100
$this->quote,
57101
'test_order_1',
58102
'reserved_order_id'
59103
);
60-
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
104+
return $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
105+
}
61106

62-
$query = <<<QUERY
107+
/**
108+
* @param string $maskedQuoteId
109+
* @param string $sku
110+
* @param int $qty
111+
*
112+
* @return string
113+
*/
114+
public function getQueryAddSimpleProduct(string $maskedQuoteId, string $sku, int $qty) : string
115+
{
116+
return <<<QUERY
63117
mutation {
64118
addSimpleProductsToCart(
65119
input: {
@@ -76,11 +130,12 @@ public function testAddProductIfQuantityIsNotAvailable()
76130
) {
77131
cart {
78132
cart_id
133+
items {
134+
qty
135+
}
79136
}
80137
}
81138
}
82139
QUERY;
83-
84-
$this->graphQlQuery($query);
85140
}
86141
}

dev/tests/integration/testsuite/Magento/Catalog/_files/products.php

+13
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,16 @@
3636
->setStockData(['use_config_manage_stock' => 1, 'qty' => 24, 'is_in_stock' => 1])
3737
->setQty(24)
3838
->save();
39+
40+
$productWithHugeQty = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
41+
->create(\Magento\Catalog\Model\Product::class, ['data' => $product->getData()]);
42+
43+
$productWithHugeQty->setUrlKey('simple-product-with-huge-amount')
44+
->setId(2)
45+
->setRowId(2)
46+
->setName('Simple Product With Huge Amount')
47+
->setSku('simple-product-with-huge-amount')
48+
->setCustomDesign('Magento/blank')
49+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 50000, 'is_in_stock' => 1])
50+
->setQty(50000)
51+
->save();

0 commit comments

Comments
 (0)