magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

magento/magento2#39873: addProductsToCart mutation doesn't work as expect with a given parent_sku

Open KrasnoshchokBohdan opened this issue 4 months ago • 3 comments

Description (*)

First part - In this issue, the author mentioned that responce from

this mutation
mutation {
  addProductsToCart(
    cartId: "LclKxxxxxxxxxxxxxxW"
    cartItems: [
      {
        quantity: 1
        parent_sku: "WSH12"
        sku: "WSH12-28-Green"
      }
    ]
  ) {
    cart {
      itemsV2 {
        items {
          id
          product {
            name
            sku
          }
          quantity
        }
        total_count
        page_info {
          page_size
          current_page
          total_pages
        }
      }
    }
    user_errors {
      code
      message
    }
  }
}
doesn't match
this expected result
{
  "data": {
    "addProductsToCart": {
      "cart": {
        "itemsV2": {
          "items": [
            {
              "id": "24",
              "product": {
                "name": "Erika Running Short",
                "sku": "WSH12"
              },
              "quantity": 1
            },
            {
              "id": "26",
              "product": {
                "name": "Erika Running Short-28-Green",
                "sku": "WSH12-28-Green"
              },
              "quantity": 1
            }
          ],
          "total_count": 2,
          "page_info": {
            "page_size": 20,
            "current_page": 1,
            "total_pages": 1
          }
        }
      },
      "user_errors": []
    }
  }
}
instead, we get
this actual result
{
  "data": {
    "addProductsToCart": {
      "cart": {
        "itemsV2": {
          "items": [
            {
              "id": "18",
              "product": {
                "name": "Erika Running Short-28-Green",
                "sku": "WSH12-28-Green"
              },
              "quantity": 1
            }
          ],
          "total_count": 1,
          "page_info": {
            "page_size": 20,
            "current_page": 1,
            "total_pages": 1
          }
        }
      },
      "user_errors": []
    }
  }
}

let's add a configurable product to the cart from frontend image in the "quote_item" table we have 2 items image

let's try this query image and in the responce, we see only the configurable product the reason for this is \Magento\QuoteGraphQl\Model\Resolver\CartItemsPaginated::resolve \Magento\Quote\Model\Quote::getAllVisibleItems image (item with sku "test simple" has parent_item_id)

so maybe it's better to change the responce example on this page instead of changing the code in \Magento\Quote\Model\Quote::getAllVisibleItems

Second part - let's add a configurable product to the cart using the "addProductsToCart" mutation

image

in the responce we see only "test simple", and in "quote_item" table we have only one item image

so I created ConfigurableProductPrecursor to add a configurable product to the cart along with a simple product.

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes magento/magento2#39873

Manual testing scenarios (*)

1.create an empty cart the official doc 2.add a product by specifying parent_sku following the official doc

Questions or comments

Contribution checklist (*)

  • [ ] Pull request has a meaningful description of its purpose
  • [ ] All commits are accompanied by meaningful commit messages
  • [ ] All new or changed code is covered with unit/integration tests (if applicable)
  • [ ] README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • [ ] All automated tests passed successfully (all builds are green)

KrasnoshchokBohdan avatar May 28 '25 21:05 KrasnoshchokBohdan