Skip to content

Commit 8a15be9

Browse files
ENGCOM-7105: MC-26683: Removed get errors of cart allowing to add product to cart #27015
- Merge Pull Request #27015 from AleksLi/magento2:MC-26683 - Merged commits: 1. 77fee57 2. 0a79989 3. af251eb 4. c47a6a2 5. 39ade1f 6. 2d87219 7. 158f788 8. fff5ce9
2 parents ba5f8eb + fff5ce9 commit 8a15be9

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\Quote\Model\Quote;
1414

1515
/**
16-
* Add products to cart
16+
* Adding products to cart using GraphQL
1717
*/
1818
class AddProductsToCart
1919
{
@@ -54,16 +54,6 @@ public function execute(Quote $cart, array $cartItems): void
5454
$this->addProductToCart->execute($cart, $cartItemData);
5555
}
5656

57-
if ($cart->getData('has_error')) {
58-
$e = new GraphQlInputException(__('Shopping cart errors'));
59-
$errors = $cart->getErrors();
60-
foreach ($errors as $error) {
61-
/** @var MessageInterface $error */
62-
$e->addError(new GraphQlInputException(__($error->getText())));
63-
}
64-
throw $e;
65-
}
66-
6757
$this->cartRepository->save($cart);
6858
}
6959
}

app/code/Magento/QuoteGraphQl/Model/Resolver/CartItems.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1213
use Magento\Framework\GraphQl\Query\ResolverInterface;
1314
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1415
use Magento\Quote\Model\Quote\Item as QuoteItem;
@@ -29,6 +30,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
2930
$cart = $value['model'];
3031

3132
$itemsData = [];
33+
if ($cart->getData('has_error')) {
34+
$errors = $cart->getErrors();
35+
foreach ($errors as $error) {
36+
$itemsData[] = new GraphQlInputException(__($error->getText()));
37+
}
38+
}
3239
foreach ($cart->getAllVisibleItems() as $cartItem) {
3340
/**
3441
* @var QuoteItem $cartItem

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
namespace Magento\GraphQl\Quote\Guest;
99

1010
use Exception;
11+
use Magento\Framework\Exception\NoSuchEntityException;
1112
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
1213
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
1315
use Magento\TestFramework\TestCase\GraphQlAbstract;
1416

1517
/**
@@ -79,6 +81,57 @@ public function testAddSimpleProductToCart()
7981
self::assertEquals('USD', $rowTotalIncludingTax['currency']);
8082
}
8183

84+
/**
85+
* Add disabled product to cart
86+
*
87+
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
88+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
89+
* @return void
90+
*/
91+
public function testAddDisabledProductToCart(): void
92+
{
93+
$sku = 'simple3';
94+
$quantity = 2;
95+
96+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
97+
$query = $this->getQuery($maskedQuoteId, $sku, $quantity);
98+
99+
$this->expectException(ResponseContainsErrorsException::class);
100+
$this->expectExceptionMessage(
101+
'Could not add the product with SKU ' . $sku . ' to the shopping cart: ' .
102+
'Product that you are trying to add is not available.'
103+
);
104+
105+
$this->graphQlMutation($query);
106+
}
107+
108+
/**
109+
* Add out of stock product to cart
110+
*
111+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
112+
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
113+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
114+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
115+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_out_of_stock.php
116+
* @return void
117+
* @throws NoSuchEntityException
118+
*/
119+
public function testAddOutOfStockProductToCart(): void
120+
{
121+
$sku = 'simple1';
122+
$quantity = 1;
123+
124+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
125+
$query = $this->getQuery($maskedQuoteId, $sku, $quantity);
126+
127+
$this->expectException(ResponseContainsErrorsException::class);
128+
$this->expectExceptionMessage(
129+
'Some of the products are out of stock.'
130+
);
131+
132+
$this->graphQlMutation($query);
133+
}
134+
82135
/**
83136
* @expectedException Exception
84137
* @expectedExceptionMessage Required parameter "cart_id" is missing
@@ -191,7 +244,7 @@ public function testAddSimpleProductToCustomerCart()
191244
private function getQuery(string $maskedQuoteId, string $sku, float $quantity): string
192245
{
193246
return <<<QUERY
194-
mutation {
247+
mutation {
195248
addSimpleProductsToCart(
196249
input: {
197250
cart_id: "{$maskedQuoteId}"

0 commit comments

Comments
 (0)