magento2
magento2 copied to clipboard
check salable quantity for swatches are crossed out issue #33799
Description (*)
Swatches are not crossed out if salable quantity is 0 and Product's stock status as "Is in stock".
Related Pull Requests
- I added "Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku" dependency injection to check salable qty at line No :102 in configurable product's helper data file.Function name (getOptions).In this file its checked only out of stock only.
- please find below code changes at line no:101 $salableqty = $this->getSalableQuantityDataBySku->execute($product->getSku()); if ($product->isSalable() && (int)$salableqty[0]['qty']) { ... }
Fixed Issues (if relevant)
- Fixes magento/magento2#33799
Manual testing scenarios (*)
- Make a configurable product
- Place an order for configurable product with one of the options so salable quantity is 0 after the order is placed.
- After this order, in backend salable qty is 0 for respective option's product
- But in frontend, the swatch will crossed out.
Hi @Kannakiraj123. Thank you for your contribution Here are some useful tips how you can test your changes using Magento test environment. Add the comment under your pull request to deploy test or vanilla Magento instance:
@magento give me test instance- deploy test instance based on PR changes@magento give me 2.4-develop instance- deploy vanilla Magento instance
:exclamation: 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:
Database CompareFunctional Tests CEFunctional Tests EE,Functional Tests B2BIntegration TestsMagento Health IndexSample Data Tests CESample Data Tests EESample Data Tests B2BStatic TestsUnit TestsWebAPI TestsSemantic Version Checker
You can find more information about the builds here
:information_source: Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review.
For more details, please, review the Magento Contributor Guide documentation.
:warning: According to the Magento Contribution requirements, all Pull Requests 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 Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.
:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel
:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel
@magento give me test instance
Hi @Kannakiraj123. Thank you for your request. I'm working on Magento instance for you.
@magento give me 2.4.develop_confiugrable_bugfix instance
Hi @Kannakiraj123. Thank you for your request. I'm working on Magento instance for you.
Hi @Kannakiraj123, here is your Magento Instance: https://d0f5dd9e20401135906cd842a7346d35.instances.magento-community.engineering Admin access: https://d0f5dd9e20401135906cd842a7346d35.instances.magento-community.engineering/admin_6602 Login: 4683223d Password: bc55f0cabc51
Hi @Kannakiraj123, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.
@magento run Unit Tests
The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.
@magento run all tests
The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.
Just one comment from my site. Tested it on my site. Cache must be cleaned before the swatch is crossed. Therefore not the solution as requested. Just one hint for it: it did work on Magento 2.3.5 Regards, Chequille
Good Morning Everybody,
my tests are showing more things which are wrong in 2.4.3 release.
In magento 2.3.5 my configurable product with three options which do have all a salable quantity of 0 looks like the screenshot below. Swatches are not shown anymore and of course add to cart button is not shown:

In 2.4.3 release, unfortunanetly with the patch from above, it is still showing the swatches (all crossed now) and the add to cart button as well.

Again, please have a look at version 2.3.5 where everything does work as it should. We need to have this in 2.4.3 as well, as it is mandatory for going online with a shop.
Best regards, Chequille
@magento give me test instance
Hi @Kannakiraj123. Thank you for your request. I'm working on Magento instance for you.
Hi @Kannakiraj123, here is your Magento Instance: https://d0f5dd9e20401135906cd842a7346d35.instances.magento-community.engineering Admin access: https://d0f5dd9e20401135906cd842a7346d35.instances.magento-community.engineering/admin_a152 Login: 6a38b2ef Password: c26ee7639738
Sorry to ask, should the above code solve the problem? If yes, not on my site. All swatches are again not crossed and ADD to CART button is still shown if all swatch products have salable qty 0
hi @chequille ,I checked in my local env.Sorry but its not working test instance.Have to check.
@magento give me 2.3.5 instance
Hi @Kannakiraj123. Thank you for your request. I'm working on Magento instance for you.
Hi @Kannakiraj123, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.
@magento give me 2.3.5-p2 instance
Hi @Kannakiraj123, unfortunately there is no ability to deploy 2.3.5-p2 Magento instance. Please make sure you specify the correct Magento version/branch.
@magento give me 2.3.5 instance
Hi @Kannakiraj123. Thank you for your request. I'm working on Magento instance for you.
Hi @Kannakiraj123, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.
Hi @Kannakiraj123 it is really strange why this stock issues are not working anymore in 2.4.x. I had as well a look to the code in 2.3.5 but I am not that deep in Magento2 to find the reason why it is working there.
Nevertheless, your idea with the observer is working for simple products with the code you made. I made some changes in it to get it working as well for configurables. Of course I do not know if it is good code as of the rules. I made as well a change in the Helper Data.php.
Changes/additions in the observer:
if ($product->getTypeId() == 'configurable' )
{
// if product is configurable go over all child products to see if product is still salable
$Childs = $product->getTypeInstance()->getUsedProducts($product);
foreach ( $Childs as $child) {
$stockItemConfig = $this->stockItemRepository->get($child->getId());
$stockItemData = $this->getStockItemData->execute($child->getSku(), $stockItemConfig->getStockId());
$minQty = $stockItemConfig->getMinQty();
$productQtyInStock += $stockItemData['quantity']
+ $this->getReservationsQuantity->execute($child->getSku(), $stockItemConfig->getStockId())
- $minQty;
}
// If all option products do not have available stock set parent product to false
if( $productQtyInStock <=0 )
$observer->getEvent()->getSalable()->setIsSalable(false);
}
And change in Data.php
if ($this->canDisplayShowOutOfStockStatus()) {
if ($product->isSalable()) {
// removed ['salable'] from this line: $options['salable'][$productAttributeId][$attributeValue][] = $productId;
$options[$productAttributeId][$attributeValue][] = $productId;
}
//removed this line: $options[$productAttributeId][$attributeValue][] = $productId;
} else {
if ($product->isSalable()) {
$options[$productAttributeId][$attributeValue][] = $productId;
}
}
This would be now a workaround for the problem. Of course the better way would be to find the reason why it was OK in 2.3.5 and not OK in 2.4.x And of course bundle products must be included as well. ANd as well cache has to be looked at.
Regards, Chequille
hi @chequille ,Thanks for coding updation.yes we have to check 2.3.5.Still am checking which difference make issue 2.4.x.
@magento give me test instance
Hi @Kannakiraj123. Thank you for your request. I'm working on Magento instance for you.