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

[GraphQl] Cart error messages are faulty #34546

Closed
1 of 5 tasks
Hexmage opened this issue Nov 5, 2021 · 28 comments
Closed
1 of 5 tasks

[GraphQl] Cart error messages are faulty #34546

Hexmage opened this issue Nov 5, 2021 · 28 comments
Labels
Area: APIs Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: done Project: GraphQL Reported on 2.4.2 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch

Comments

@Hexmage
Copy link

Hexmage commented Nov 5, 2021

Preconditions (*)

  1. Magento 2.4.2+

Steps to reproduce (*)

  1. Create a cart
  2. Add 2 products to the cart with at least 4 quantities each.
  3. Set the quantity of added products below the quantity in the cart.
  4. Retrieve cart through graphql
{
  cart(cart_id: "RQg4YPcQX1htp47j6GMpTKRshfY6SVm6") {
    items {
      id
      product {
        name
        sku
        stock_status
      }
      quantity
    }
  }
}

Expected result (*)

  1. A error message which tells me which item in the cart causes the error. So that we can give correct feedback to the Customer.
  2. A cart with valid values for items.
  3. An error message for each issue with the cart.

Actual result (*)

{
  "errors": [
    {
      "message": "The requested qty is not available",
      "extensions": {
        "category": "graphql-input"
      },
      "locations": [
        {
          "line": 7,
          "column": 5
        }
      ],
      "path": [
        "cart",
        "items",
        0
      ]
    }
  ],
  "data": {
    "cart": {
      "items": [
        null,
        {
          "id": "32",
          "product": {
            "name": "Strive Shoulder Pack",
            "sku": "24-MB04",
            "stock_status": "IN_STOCK"
          },
          "quantity": 3
        },
        {
          "id": "33",
          "product": {
            "name": "Joust Duffle Bag",
            "sku": "24-MB01",
            "stock_status": "IN_STOCK"
          },
          "quantity": 4
        }
      ]
    }
  }
}
  1. In the items node there is a null entry, this only shows up when there is an error. In default Venia this breaks the cart page.
  2. I assume the path node in the error node should reference the item it's affecting. It currently points to the above null value.
  3. Only the first error of the cart shows up.

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
@m2-assistant
Copy link

m2-assistant bot commented Nov 5, 2021

Hi @Hexmage. Thank you for your report.
To speed up processing of this issue, make sure that you provided the following information:

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

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@m2-assistant
Copy link

m2-assistant bot commented Nov 8, 2021

Hi @engcom-Hotel. 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.

@engcom-Hotel
Copy link
Contributor

Hello @Hexmage,

Thanks for posting the issue!

I would like to suggest you add stock_status, so that it will give the exact situation of stock.

for eg:
The below query:

{
  cart(cart_id: "5VrDt8LhknXPP3ugJuXZZerItlVDrtB0") {
    items {
      id
      product {
        name
        sku
        stock_status
      }
      quantity
    }
  }
}

Will return:

{
  "errors": [
    {
      "message": "Some of the products are out of stock.",
      "extensions": {
        "category": "graphql-input"
      },
      "locations": [
        {
          "line": 7,
          "column": 5
        }
      ],
      "path": [
        "cart",
        "items",
        0
      ]
    }
  ],
  "data": {
    "cart": {
      "items": [
        null,
        {
          "id": "12",
          "product": {
            "name": "Joust Duffle Bag",
            "sku": "24-MB01",
            "stock_status": "OUT_OF_STOCK"
          },
          "quantity": 1
        },
        {
          "id": "13",
          "product": {
            "name": "Strive Shoulder Pack",
            "sku": "24-MB04",
            "stock_status": "IN_STOCK"
          },
          "quantity": 1
        }
      ]
    }
  }
}

You can see stock_status as OUT_OF_STOCK where the product is out of stock.

Thanks

@engcom-Hotel engcom-Hotel added the Issue: needs update Additional information is require, waiting for response label Nov 26, 2021
@Hexmage
Copy link
Author

Hexmage commented Dec 6, 2021

@engcom-Hotel
But what if the stock of a product changes from 3 to 2 and I had 3 in my cart. stock_status won't help me identify what the problem is in that scenario.

It's nice that there are error messages in the cart response, but they need a second pass over. Because as they are now they aren't good enough.

@engcom-Hotel
Copy link
Contributor

Hello @Hexmage,

Thanks for the reply!

This is the valid point, if the cart item is less in stock then proper handling is required. I have followed the below steps to reproduce the issue:

  • Add 2 items in the cart with 4 quantities each
  • Set the quantity of a product to 2 in the admin panel, it should be in the customer cart.
  • Run the cart query:
{
  cart(cart_id: "RQg4YPcQX1htp47j6GMpTKRshfY6SVm6") {
    items {
      id
      product {
        name
        sku
        stock_status
      }
      quantity
    }
  }
}
  • The response for the same is as follows:
{
  "errors": [
    {
      "message": "The requested qty is not available",
      "extensions": {
        "category": "graphql-input"
      },
      "locations": [
        {
          "line": 7,
          "column": 5
        }
      ],
      "path": [
        "cart",
        "items",
        0
      ]
    }
  ],
  "data": {
    "cart": {
      "items": [
        null,
        {
          "id": "32",
          "product": {
            "name": "Strive Shoulder Pack",
            "sku": "24-MB04",
            "stock_status": "IN_STOCK"
          },
          "quantity": 3
        },
        {
          "id": "33",
          "product": {
            "name": "Joust Duffle Bag",
            "sku": "24-MB01",
            "stock_status": "IN_STOCK"
          },
          "quantity": 4
        }
      ]
    }
  }
}

It should display which item has not enough quantity.

Updating the main description as well.

Thanks

@engcom-Hotel engcom-Hotel added Reported on 2.4.2 Indicates original Magento version for the Issue report. Area: APIs Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels Dec 10, 2021
@m2-community-project m2-community-project bot removed the Issue: needs update Additional information is require, waiting for response label Dec 10, 2021
@github-jira-sync-bot
Copy link

Unfortunately, not enough information was provided to create a Jira ticket. Please make sure you added the following label(s): Reproduced on 2.4.x, ^Area:.*

Once all required labels are present, please add Issue: Confirmed label again.

@github-jira-sync-bot github-jira-sync-bot removed the Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed label Dec 10, 2021
@engcom-Hotel engcom-Hotel added Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels Dec 10, 2021
@github-jira-sync-bot
Copy link

✅ Jira issue https://jira.corp.magento.com/browse/AC-1965 is successfully created for this GitHub issue.

@m2-assistant
Copy link

m2-assistant bot commented Dec 10, 2021

✅ Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@Hexmage
Copy link
Author

Hexmage commented Dec 15, 2021

@Usik2203 I'd rather have something that has to be unique to identify the product, because the product name can be identical for products. Also your solution doesn't solve the null object that suddenly got added to the items array.

@alitopaloglu
Copy link

Any update about null value? I can still reproduce this on 2.4.3-p2

@Hexmage
Copy link
Author

Hexmage commented Apr 21, 2022

@alitopaloglu
I've learned that the null value is the actual exception object. Which is handled by the resolver to make the error node in the response. But this node is never deleted after its handled. I'm currently testing some code on my own environment where I moved the Exception Object to a new node in the item that created it. But this will not work with default Venia.

@PauloPhagula
Copy link

Hi @Hexmage did you make any progress on this? I'm desperately looking for a solution.

@Hexmage
Copy link
Author

Hexmage commented May 27, 2022

@PauloPhagula
Think they fixed it in https://github.com/magento/magento2-pwa which is a repo with pwa specific fixes because the normal magento repo doesn't update fast enough.

@marwan-corals
Copy link

Hello,
Any Update on this error, its been setting here for months @m2-assistant

@marwan-corals
Copy link

@Hexmage I just checked and magento2-pwa didnt fix the issue either

@Hexmage
Copy link
Author

Hexmage commented Jul 13, 2022

@marwan-corals
They sort of did. They added a new graphql endpoint for just errors. While this doesn't fix the fact that there is an empty error object in the cart, it does allow you to easier find out which errors are in the cart.

Personally I used a plugin on the function to add an error string element to each order_item with the raw exception message in there instead of the empty node.

@marwan-corals
Copy link

marwan-corals commented Jul 13, 2022

@Hexmage, thanks for the reply, but if the item is shown as null, how is this handled?
also can you please share with me this plugin

@marwan-corals
Copy link

@Hexmage are you willing to help here ?

@Hexmage
Copy link
Author

Hexmage commented Jul 19, 2022

@marwan-corals
The Plugin

/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Experius\CartFix\Plugin\Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\Uid;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Magento\QuoteGraphQl\Model\Cart\GetCartProducts;

/**
 * @inheritdoc
 */
class CartItems
{
    /**
     * @var GetCartProducts
     */
    private $getCartProducts;

    /** @var Uid */
    private $uidEncoder;

    /**
     * @param GetCartProducts $getCartProducts
     * @param Uid $uidEncoder
     */
    public function __construct(
        GetCartProducts $getCartProducts,
        Uid $uidEncoder
    ) {
        $this->getCartProducts = $getCartProducts;
        $this->uidEncoder = $uidEncoder;
    }

    /**
     * @inheritdoc
     */
    public function afterResolve($subject, $result, Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
    {
        if (!isset($value['model'])) {
            throw new LocalizedException(__('"model" value should be specified'));
        }
        /** @var Quote $cart */
        $cart = $value['model'];

        $itemsData = [];
        $cartProductsData = $this->getCartProductsData($cart);
        $cartItems = $cart->getAllVisibleItems();
        /** @var QuoteItem $cartItem */
        foreach ($cartItems as $cartItem) {
            $productId = $cartItem->getProduct()->getId();
            if (!isset($cartProductsData[$productId])) {
                $itemsData[] = new GraphQlNoSuchEntityException(
                    __("The product that was requested doesn't exist. Verify the product and try again.")
                );
                continue;
            }
            $productData = $cartProductsData[$productId];
            $errorMessages = null;
            if ($cart->getData('has_error')) {
                foreach ($cartItem->getErrorInfos() as $error) {
                    $errorMessages[] = $error;
                }
            }

            $itemsData[] = [
                'id' => $cartItem->getItemId(),
                'uid' => $this->uidEncoder->encode((string) $cartItem->getItemId()),
                'quantity' => $cartItem->getQty(),
                'product' => $productData,
                'errors' =>  $errorMessages,
                'model' => $cartItem,
            ];
        }
        return $itemsData;
    }

    /**
     * Get product data for cart items
     *
     * @param Quote $cart
     * @return array
     */
    private function getCartProductsData(Quote $cart): array
    {
        $products = $this->getCartProducts->execute($cart);
        $productsData = [];
        foreach ($products as $product) {
            $productsData[$product->getId()] = $product->getData();
            $productsData[$product->getId()]['model'] = $product;
            $productsData[$product->getId()]['uid'] = $this->uidEncoder->encode((string) $product->getId());
        }

        return $productsData;
    }
}

The schema.graphqls additions

interface CartItemInterface {
    errors: [Error]
}

type Error {
    origin: String
    type: Int
    message: String
}

@alexmtch
Copy link

They removed the module : magento/magento2-pwa@96c5f5f

@Hexmage
Copy link
Author

Hexmage commented Jul 21, 2022

That explains a lot.

@marwan-corals
Copy link

Thanks, @Hexmage the provided code is helpful; one more note here is to add to di.xml

    <type name="Magento\QuoteGraphQl\Model\Resolver\CartItems">
        <plugin name="Magento_CartItems_Add_Source_Errors" type="PathTo\Your\Class\Resolver\CartItems" />
    </type>

@engcom-Lima
Copy link
Contributor

Hi @Hexmage ,

Thanks for your contribution and collaboration.

I have tried to reproduce the issue in 2.4-develop but issue is not reproducible to me.

Followed below steps :

  1. Create a cart
  2. Add 2 products to the cart with at least 4 quantities each.
  3. Set the quantity of added products below the quantity in the cart from admin.
  4. Get cart details through graphql query:
query {
  
  customerCart {
    id
    items {
      uid
      product {
        name
        sku
      }
      quantity
    }
  }
}

After running above query I am not getting any error message "message": "The requested qty is not available", as you have reported.
Below is the screenshot for your reference:
image
If anything missing in above steps kindly provide the information.
Please note that I did PR changes but the response of step 4 was same.

Please test it in latest 2.4-develop if you are facing any issue kindly provide the information.

Thanks

@Hexmage
Copy link
Author

Hexmage commented Aug 26, 2022

@engcom-Lima
Feels like you don't have manage stock turned on. Because there should be an error message when the stock is below the available stock.

@engcom-Lima
Copy link
Contributor

HI @Hexmage ,

I have tried to reproduce the issue in 2.4-develop and followed below steps but issue is not reproducible to me:

  1. Create a cart
  2. Add 2 products to the cart with at least 4 quantities each.
  3. Set the quantity of added products below the quantity in the cart from admin.
  4. Get cart details through graphql query:
query {
  
  customerCart {
    id
    items {
      uid
      product {
        name
        sku
      }
      quantity
    }
  }
}
  1. Also Stores >> configuration >> Catalog >> inventory >>Product Stock Options >> Set Yes in Manage Stock.

Kindly provide me more information inorder to reproduce the isssue.
Also, please check in latest 2.4-develop and inform us whether issue is reproducible or not.

Thanks

@engcom-Lima
Copy link
Contributor

Hi @Hexmage ,

Thank for your contribution and collaboration.

Please provide more information required to reproduce the issue.

Thanks

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: APIs Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: done Project: GraphQL Reported on 2.4.2 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

10 participants