We are using Magento as a headless backend for our front-facing application, we are using the GraphQL API to build the frontend.
We noticed if we added a product to cart, and that product later became out of stock, any further attempts to add items to cart will fail with Some cart items are out of stock effectively locking up the cart from adding other items or changing other items quantities.
This is the returned GraphQL error:
"message": "Some of the products are out of stock.",
This is a breaking issue for us, as the alternative would result in a very poor experience for our users.
Currently, we have things set up that the frontend is able to detect whenever this exception is thrown and tries to amend it by removing the offending items and attempting to re-play whatever mutations are done to the cart since then.
Needless to say, this is a very flaky solution and it doesn't make sense to have this restriction in the first place since the REST API seems to be fine with this scenario.
To Reproduce:
I have successfully reproduced this on a fresh magento environment
Ideally, the GraphQL API should allow mutating the cart as long as the out-of-stock item is not being the target of the mutation (updating its quantity for example).
A slightly less robust solution is to allow the mutation to go through, but return errors alongside the data prompting the GQL client to update their UI and also be aware of possible problems.
Create an empty cart
mutation {
createEmptyCart
}
Add product to cart
mutation {
addSimpleProductsToCart(
input: {
cart_id: "{ CART_ID }" # <--- cart id from the previous request goes here
cart_items: [
{
data: {
quantity: 1
sku: "simple-product" # <--- product sku goes here
}
}
]
}
) {
cart {
items {
id
product {
sku
stock_status
}
quantity
}
}
}
Thanks for opening this issue!
Hi @logaretm. Thank you for your report.
To help us process this issue please make sure that you provided the following information:
Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:
@magento give me 2.4-develop instance - upcoming 2.4.x release
For more details, please, review the Magento Contributor Assistant documentation.
@logaretm do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?
@magento give me 2.4-develop instance
Hi @logaretm. Thank you for your request. I'm working on Magento 2.4-develop instance for you
Hi @logaretm, here is your Magento instance.
Admin access: https://i-26683-2-4-develop.instances.magento-community.engineering/admin_c31c
Login: b054e3e7 Password: 656c5038bda6
Instance will be terminated in up to 3 hours.
Thanks for opening this issue!
Hi @engcom-Bravo. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.[ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.
[ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
[ ] 4. Verify that the issue is reproducible on 2.4-develop branchDetails
- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 5. Add label Issue: Confirmed once verification is complete.
[ ] 6. Make sure that automatic system confirms that report has been added to the backlog.
Overcome this issue by overriding Cart model adding line below to remove error during process.
<?php
declare(strict_types=1);
namespace Robusta\CartStockErrorHandler\Model\Cart;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\Message\AbstractMessage;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;
use Magento\QuoteGraphQl\Model\Cart\AddProductsToCart as MAddProductsToCart;
use Magento\QuoteGraphQl\Model\Cart\AddSimpleProductToCart;
class AddProductsToCart extends MAddProductsToCart
{
/**
* @var CartRepositoryInterface
*/
private $cartRepository;
/**
* @var AddSimpleProductToCart
*/
private $addProductToCart;
/**
* @param CartRepositoryInterface $cartRepository
* @param AddSimpleProductToCart $addProductToCart
*/
public function __construct(CartRepositoryInterface $cartRepository, AddSimpleProductToCart $addProductToCart)
{
$this->cartRepository = $cartRepository;
$this->addProductToCart = $addProductToCart;
parent::__construct($cartRepository, $addProductToCart);
}
/**
* Add products to cart
*
* @param Quote $cart
* @param array $cartItems
* @throws GraphQlInputException
* @throws LocalizedException
* @throws GraphQlNoSuchEntityException
*/
public function execute(Quote $cart, array $cartItems): void
{
foreach ($cartItems as $cartItemData) {
$this->addProductToCart->execute($cart, $cartItemData);
}
// This line must be added to remove thrown error
$cart->removeErrorInfosByParams('stock', []);
if ($cart->getData('has_error')) {
throw new GraphQlInputException(
__('Shopping cart error: %message', ['message' => $this->getCartErrors($cart)])
);
}
$this->cartRepository->save($cart);
}
/**
* Collecting cart errors
*
* @param Quote $cart
* @return string
*/
private function getCartErrors(Quote $cart): string
{
$errorMessages = [];
/** @var AbstractMessage $error */
foreach ($cart->getErrors() as $error) {
$errorMessages[] = $error->getText();
}
return implode(PHP_EOL, $errorMessages);
}
}
Thanks for opening this issue!
:white_check_mark: Confirmed by @engcom-Bravo
Thank you for verifying the issue. Based on the provided information internal tickets MC-31084 were created
Issue Available: @engcom-Bravo, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._
Hi @AleksLi. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.4-develop branchDetails
- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.
This is a complex task that requires framework changes.
Removing errors is not an option. It leads to the wrong flow when user will see cart errors only on place order step. Errors should be returned under error node like it is right now, but cart data should be present under data node. This is not supported now but this is the way how this issue should be resolved.
Hi @cpartica. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.4-develop branchDetails
- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.
Hi @nrkapoor. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.4-develop branchDetails
- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.
Hi @logaretm. Thank you for your report.
The issue has been fixed in magento/magento2#27015 by @AleksLi in 2.4-develop branch
Related commit(s):
The fix will be available with the upcoming 2.4.0 release.
Most helpful comment
Overcome this issue by overriding Cart model adding line below to remove error during process.