#3435 Optimise GetBundleProductStockStatus #3436
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description (*)
Optimises Magento\InventoryBundleProduct\Model\GetBundleProductStockStatus (https://github.com/magento/inventory/blob/develop/InventoryBundleProduct/Model/GetBundleProductStockStatus.php) to be better optimised for the scenario where there are many child products, and the parent product is in stock.
Currently getAreSalableSelections gets the stock status for every SKU within an option in bulk:
This is where the inefficiency lies. Despite areProductsSalableForRequestedQty accepting an array of $skuRequests, it actually gets the stock status of each SKU syncronously. You can see that in https://github.com/magento/inventory/blob/develop/InventorySales/Model/AreProductsSalableForRequestedQty.php, where it actually just calls
isProductSalableForRequestedQtyInterface->execute
within a loop. Each call toisProductSalableForRequestedQtyInterface
performs database queries, and this is a moderately expensive function.This refactor changes the logic to only call
isProductSalableForRequestedQtyInterface->execute
the minimum possible number of times required to get the correct result.Fixed Issues (if relevant)
Manual testing scenarios (*)
Test
GetBundleProductStockStatus
to ensure the same behaviour as before.Note, that a site I maintain has been running this code in production (albeit on a Magento 2.4.4 installation) for some time, with no observed bugs.
Questions or comments
This could be seen as slightly academic as
GetBundleProductStockStatus
is not directly used in Magento core since 913287a#diff-b232ada4f1ab0a260b18a11a35d11d3337265f7bbb5eaba96dde557b4c2ced47. However, this optimisation will be useful for anyone usingGetBundleProductStockStatus
in extensions.Contribution checklist (*)