magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

AdaptAddQuantityFilterPlugin: incorrect SKUs exclusion due to imploded string in NOT IN clause

Open ChickenBenny opened this issue 7 months ago • 8 comments

Preconditions and environment

  • Magento 2.4.x with MSI enabled
  • Module: magento/module-inventory-bundle-product
  • PHP 7.4 / 8.1 (any)
  • Database: MySQL 8.0

Steps to reproduce

  1. Setup

    • Create a Bundle product with two or more child SKUs that are out of stock in the current MSI stock (so inventory_stock_<id>.is_salable = 0).
    • Ensure that \Magento\InventorySalesApi\Api\AreProductsSalableInterface::execute() returns false for those SKUs.
  2. Trigger the SQL

    • On the storefront (or in a debugger), load the bundle product page so Magento builds the bundle-selection collection.
  3. Inspect the generated SQL

    • Look for the AdaptAddQuantityFilterPlugin filter clause. You’ll see something like:
      … AND e.sku NOT IN('SKU1,SKU2')
      
  4. Observe the bug

    • Because the two SKUs are concatenated into one quoted string, neither SKU1 nor SKU2 is actually excluded.

Expected result

The SQL should exclude each SKU individually:

e.sku NOT IN ('SKU1','SKU2')

Actual result

Out-of-stock SKUs are not excluded, because:

// in AdaptAddQuantityFilterPlugin
if ($skusToExclude) {
    $subject->getSelect()->where('e.sku NOT IN(?)', implode(',', $skusToExclude));
}

turns ['SKU1','SKU2'] into the single string "SKU1,SKU2", resulting in:

e.sku NOT IN ('SKU1,SKU2')  -- only one entry, never matches SKU1 or SKU2

Additional information

Proposed fix

IN

vendor/magento/module-inventory-bundle-product/Plugin/Bundle/Model/ResourceModel/Selection/Collection/AdaptAddQuantityFilterPlugin.php

Replace

if ($skusToExclude) {
    $subject->getSelect()
        ->where('e.sku NOT IN(?)', implode(',', $skusToExclude));
}

With

if ($skusToExclude) {
    // pass the array directly so Zend_Adapter expands it into individual, quoted literals
    $subject->getSelect()
        ->where('e.sku NOT IN(?)', $skusToExclude);
}

This lets the DB adapter render

e.sku NOT IN ('SKU1','SKU2')

Additional context This bug causes unsalable children to slip into the “max price” calculation for bundle products and can lead to incorrect price ranges on both product and category pages.

Release note

No response

Triage and priority

  • [ ] 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”.

ChickenBenny avatar May 27 '25 06:05 ChickenBenny

Hi @ChickenBenny. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce.


Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: 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.

m2-assistant[bot] avatar May 27 '25 06:05 m2-assistant[bot]

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

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • [ ] 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- 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.

m2-assistant[bot] avatar May 30 '25 11:05 m2-assistant[bot]

Hello @ChickenBenny,

Thanks for the report and collaboration!

We have tried to reproduce the issue with the latest 2.4-develop branch and its reproducible for us. Please refer to the below screenshot for reference:

Image

Hence confirming the issue.

Thanks

engcom-Hotel avatar May 30 '25 11:05 engcom-Hotel

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-14813 is successfully created for this GitHub issue.

github-jira-sync-bot avatar May 30 '25 11:05 github-jira-sync-bot

:white_check_mark: 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.

m2-assistant[bot] avatar May 30 '25 11:05 m2-assistant[bot]

Hi team 👋,

I’ve just pushed a fix for this issue in magento/inventory#3427. The patch adjusts the SKU-exclusion logic so the resulting NOT IN clause is built correctly when multiple SKUs are involved.

Could someone from the core team please take a look and let me know if anything needs to be updated?

ChickenBenny avatar May 30 '25 16:05 ChickenBenny

Hi @ChickenBenny,

Adobe Commerce Engineering team started working on this issue. We will reach out to you if we need more information and you will get notified once the issue is fixed. Please leave comments for any further questions. Thank you!

engcom-Bravo avatar Dec 08 '25 06:12 engcom-Bravo

Hi @ChickenBenny,

Thank you for your valuable contribution. The Adobe Commerce Engineering team has conducted extensive testing to reproduce this issue but have been unable to observe the incorrect max price calculation behaviour described.

Testing Performed:

  • Bundle products with multiple child SKUs marked out-of-stock via MSI
  • Various bundle option types (drop-down, radio, checkbox, multi-select)
  • Non-default MSI stock assigned to website
  • "Display Out of Stock Products" set to both Yes and No
  • Frontend product page, category page, and GraphQL API

Technical Finding:

The addQuantityFilter() method is called from DefaultSelectionPriceListProvider::addMiniMaxPriceList(). However, addPriceFilter() is called first, which applies LIMIT 1 to the query (see Collection.php line 363). This means the plugin only ever receives a single row in the result set. With only one SKU ever returned, the implode() call produces a single value (e.g., 'SKU1'), which generates valid SQL: e.sku NOT IN('SKU1'). The bug would only manifest if multiple SKUs were in the $skusToExclude array, which cannot happen due to the pre-existing LIMIT 1.

Questions for Clarification:

  1. Could you share the specific SQL query you observed that contained the incorrectly formatted NOT IN('SKU1,SKU2') clause?
  2. What bundle configuration (option type, price type, number of options/children) produced this behaviour?
  3. Was there a specific entry point or code path beyond the standard product page load? We agree that the implode() usage is incorrect and should be fixed as a matter of code quality. However, we were unable to identify a scenario where this bug affects actual functionality. Any additional reproduction details would be helpful.

Thanks.

engcom-Bravo avatar Dec 09 '25 06:12 engcom-Bravo