Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to execute addSimpleProduct mutation while other items out of stock #26683

Closed
logaretm opened this issue Feb 3, 2020 · 15 comments · Fixed by #27015
Closed

Unable to execute addSimpleProduct mutation while other items out of stock #26683

logaretm opened this issue Feb 3, 2020 · 15 comments · Fixed by #27015
Assignees
Labels
Component: GraphQL GraphQL Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch

Comments

@logaretm
Copy link

logaretm commented Feb 3, 2020

Summary (*)

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.

Examples (*)

To Reproduce:

  1. Using the GraphQL API: Add an item to cart.
  2. Mark it as out-of-stock from the admin dashboard.
  3. Using the GraphQL API: Try to add other products to cart.

I have successfully reproduced this on a fresh magento environment

Proposed solution

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.

GraphQL requests for reproducing

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
      }
    }
  }
@logaretm logaretm added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Feb 3, 2020
@magento-deployment-service
Copy link

Thanks for opening this issue!

@m2-assistant
Copy link

m2-assistant bot commented Feb 3, 2020

Hi @logaretm. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

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?

  • yes
  • no

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Feb 3, 2020
@logaretm
Copy link
Author

logaretm commented Feb 3, 2020

@magento give me 2.4-develop instance

@magento-engcom-team
Copy link
Contributor

Hi @logaretm. Thank you for your request. I'm working on Magento 2.4-develop instance for you

@magento-engcom-team
Copy link
Contributor

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.

@magento-deployment-service
Copy link

Thanks for opening this issue!

@engcom-Bravo engcom-Bravo self-assigned this Feb 4, 2020
@m2-assistant
Copy link

m2-assistant bot commented Feb 4, 2020

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: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label 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 branch

    Details- 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.

@MeeGz
Copy link

MeeGz commented Feb 4, 2020

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);
    }
}

@magento-deployment-service
Copy link

Thanks for opening this issue!

@engcom-Bravo engcom-Bravo added Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Component: GraphQL GraphQL Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels Feb 4, 2020
@ghost ghost removed the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Feb 4, 2020
@ghost ghost unassigned engcom-Bravo Feb 4, 2020
@magento-engcom-team magento-engcom-team added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Feb 4, 2020
@magento-engcom-team
Copy link
Contributor

✅ 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.

@AleksLi AleksLi self-assigned this Feb 24, 2020
@m2-assistant
Copy link

m2-assistant bot commented Feb 24, 2020

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:

  • 1. Add/Edit 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 branch

    Details- 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.


@lenaorobei
Copy link
Contributor

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.

@m2-assistant
Copy link

m2-assistant bot commented Mar 11, 2020

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:

  • 1. Add/Edit 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 branch

    Details- 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.


@m2-assistant
Copy link

m2-assistant bot commented Mar 11, 2020

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:

  • 1. Add/Edit 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 branch

    Details- 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.


@magento-engcom-team magento-engcom-team added the Fixed in 2.4.x The issue has been fixed in 2.4-develop branch label Mar 19, 2020
@magento-engcom-team
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: GraphQL GraphQL Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants