-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Closed
Labels
Component: SwatchesFixed in 2.3.xThe issue has been fixed in 2.3 release lineThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 releaseThe issue has been reproduced on latest 2.3 release
Description
Summary
When pre-selecting a configurable product swatch that has an image, Magento will try to load that image in the product gallery before it is initialized, throwing a JavaScript error.
This issue was already reported in #12517, but that bug is closed. I think that the fix I propose below is a better one, since it uses an event name that seems to be created for a similar purpose.
Preconditions
- Magento v2.2.5.
- PHP v7.1.20-1.
- Ubuntu 16.04.1
- Apache 2.4.18
- MySQL 5.7.23
Steps to reproduce
- Set up a default Magento instance, and create a configurable product with at least one option.
- Configure that option to use swatches (text or visual).
- Create at least one simple product associated to that option.
- Upload image A as default for the configurable product.
- Upload a different image B as default for the simple product.
- Make sure that Magento automatically loads the simple product image for the PDP gallery when the option is selected.
- Open the configurable product page, with the option pre-selected. For example: https://magento.app/sample-product.html#89=1245
- The Swatch Renderer will pre-select that swatch. Since Magento is configured to load its image (image B) when the swatch is selected, this image will be loaded.
- However, since we haven't loaded the gallery yet, the swatch renderer will throw an error when trying to load the image.
Expected result
- The swatch is selected and its associated simple product image is loaded in the gallery.
Actual result
- The swatch is not selected and the JavaScript console shows an error.
Investigation
The problem seems to be located in Module_Swatches::view/frontend/web/js/swatch-renderer.js. The following sequence of method calls is performed when pre-selecting a simple product:
[Line 304]- Call tothis._renderControls()- from the_initmethod.[Line 465]- Call to$widget._EmulateSelected($widget._getSelectedAttributes()).[Line 1243]- Theclickevent is triggered with the attribute code, via$.proxy.[Line 619]- Theclickevent is handled by_EventListener(), and the_OnClick()method is called.
Note:- The
_OnClickis called with two arguments:$(this), and$widget. - The
emulateClickevent is also available, and will call_OnClickwith a third parameter, the event name (emulateClick).
- The
[Line 725]- The_loadMediamethod is called byOnClick, to load the simple product image. Since theclickevent was used, and notemulateClick, the event name will be undefined.[Line 674]- TheupdateBaseImagemethod is called, with an undefinedeventNameargument.[Line 1156]- TheprocessUpdateBaseImageis called, using an undefinedgalleryobject. Note that if the event name is undefined (as in this case), theupdateBaseImagemethod won't wait until the gallery image is loaded to process the image.[Line 1187]- TheprocessUpdateBaseImagemethod will try to callgallery.updateDatawith the updated images, triggering the JavaScript error.
- This error not only means that the image won't be loaded, but also that the swatch option won't be pre-selected, as expected.
- It seems that the
emulateClickevent was created to avoid this issue (see commit 97390b3), but it's not being used for this use case.
Probable fix:
-
In the
_EmulateSelectedmethod described above (line 1243), use theemulateClickevent instead ofclick:_EmulateSelected: function (selectedAttributes) { $.each(selectedAttributes, $.proxy(function (attributeCode, optionId) { this.element.find('.' + this.options.classes.attributeClass + '[attribute-code="' + attributeCode + '"] [option-id="' + optionId + '"]').trigger('emulateClick'); }, this)); },
dani97, thilinaaligent, adamraya, steffen-wirth, 0x15f and 1 more
Metadata
Metadata
Assignees
Labels
Component: SwatchesFixed in 2.3.xThe issue has been fixed in 2.3 release lineThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 releaseThe issue has been reproduced on latest 2.3 release