Skip to content

Fix add products to cart parent SKU configurable#40741

Open
mohaelmrabet wants to merge 11 commits intomagento:2.4-developfrom
mohaelmrabet:fix/GH-40598-addProductsToCart-parent-sku-configurable
Open

Fix add products to cart parent SKU configurable#40741
mohaelmrabet wants to merge 11 commits intomagento:2.4-developfrom
mohaelmrabet:fix/GH-40598-addProductsToCart-parent-sku-configurable

Conversation

@mohaelmrabet
Copy link
Copy Markdown
Contributor

Description (*)

addProductsToCart mutation ignores parent_sku for configurable products, causing the item to be added without the correct variant options (wrong product type instance processes the buy request).

Two root causes are fixed:

1. QuoteConfigurableOptions\SuperAttributeDataProvider
When selected_options contains no configurable UIDs, the provider now falls back to resolving super_attribute data from parent_sku + child sku using ConfigurableProductGraphQl\Model\Options\Collection — mirroring the existing logic in ConfigurableProductGraphQl\SuperAttributeDataProvider.

2. Quote\Model\Cart\AddProductsToCart::addItemToCart
When parent_sku is present, the service now loads the parent configurable product and passes it to Quote::addProduct() instead of the child simple product. This ensures the Configurable type instance processes the buyRequest with the resolved super_attribute data.

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes addProductsToCart graphql mutation ignores parent_sku for configurable products #40598

Manual testing scenarios (*)

Prerequisites: A configurable product (e.g. SKU MH01) with at least one child variant (e.g. SKU MH01-XS-Black).

Scenario 1 — parent_sku without selected_options (main fix)

  1. Create an empty cart: createEmptyCart.
  2. Run addProductsToCart with:
cartItems: [{
  quantity: 1
  sku: "MH01-XS-Black"
  parent_sku: "MH01"
}]
  1. Check the cart — the item must appear as a ConfigurableCartItem with the correct variant options selected.
  2. Before the fix: the item was either rejected or added without variant info.
    Scenario 2 — selected_options (regression check)
  3. Create an empty cart.
  4. Run addProductsToCart with a base64-encoded configurable UID in selected_options (no parent_sku).
  5. Check the cart — behaviour must be unchanged.
    Scenario 3 — invalid parent_sku
  6. Run addProductsToCart with a parent_sku that does not own the given child sku.
  7. Expect a user-friendly error: "The product ... is not a variant of ...".

Questions or comments

  • The fallback reads extension attributes (getConfigurableProductLinks) which are loaded only when the product is fetched via ProductRepository. If the product is already in the object cache this is a no-op.
  • Unit tests (8 cases) cover: selected_options path, parent_sku fallback, priority rules, invalid variant, product not found, malformed UID.

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)

…rable products

Two root causes addressed:

1. QuoteConfigurableOptions\SuperAttributeDataProvider
   - Add parent_sku fallback: when selected_options contain no configurable
     UIDs, resolve super_attribute data from parent_sku + child sku using
     ConfigurableProductGraphQl OptionCollection (mirrors the existing logic
     in ConfigurableProductGraphQl\SuperAttributeDataProvider).
   - Inject ProductRepositoryInterface, OptionCollection and MetadataPool.
   - Add module.xml sequence dependency on Magento_ConfigurableProductGraphQl
     and declare the new composer requirements.

2. Quote\Model\Cart\AddProductsToCart::addItemToCart
   - When parent_sku is present, load/clone the parent configurable product
     instead of the child simple product so that Quote::addProduct() invokes
     the Configurable type instance, which correctly processes super_attribute.
   - Pre-load parent SKUs in addItemsToCart to keep a single DB round-trip.
   - Use the child product SKU only for stock quantity display.

Tests: 8 unit tests added for SuperAttributeDataProvider covering
selected_options path, parent_sku fallback, priority rules, error cases.
@m2-assistant
Copy link
Copy Markdown

m2-assistant Bot commented Apr 15, 2026

Hi @mohaelmrabet. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@mohaelmrabet
Copy link
Copy Markdown
Contributor Author

@magento run all tests

@mohaelmrabet
Copy link
Copy Markdown
Contributor Author

@magento run Static tests

@engcom-Bravo engcom-Bravo added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Apr 16, 2026
@github-project-automation github-project-automation Bot moved this to Pending Review in Pull Requests Dashboard Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: pending review

Projects

Status: Pending Review

Development

Successfully merging this pull request may close these issues.

addProductsToCart graphql mutation ignores parent_sku for configurable products

2 participants