-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
URL pre-selection of configurable product swatches with associated product images throws JavaScript error #18017
Comments
Hi @leoquijano. Thank you for your report.
Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:
where @leoquijano do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?
|
Yes, I was able to reproduce it on vanilla Magento instance. See screenshots below:
I'm using production mode in a local instance. If your environment moves too fast to reproduce the issue, switching to developer mode might help. Also, remember you need to reload the product page after adding the # option, to force the browser to request the page contents again. Also note that if you add a second pre-selected option, the JavaScript error thrown by the first one will prevent the second one from being selected. That example is not presented here but it's easily be reproduced by adding a second swatch option and pre-selecting both options (the first one and the new one). |
Hi @leoquijano , thank you for your report. |
Thanks @engcom-backlog-nazar. I'm wondering, why is the issue marked as a feature request? It's actually a bug. |
@leoquijano Actually magento not have functional to change swates from links, in any version if you click on swatches url not changes, so i think it's a feature. however. |
@leoquijano Ok. seems like this is bug, the console must not have error |
@engcom-backlog-nazar, it seems they had the same discussion in #12517, about whether it was a feature or not. However, if you go back in time to #2746, it seems this functionality was present in M1 and that it has been present in M2 for some years now. An issue was fixed there so I guess it's already part of the feature set. If it's unofficial, then it might be a good idea to make it official, since this is a very useful feature. You can connect other parts of the website with the product view page and have the options already pre-selected. Hopefully the fix I suggest above will help. |
emulateClick worked perfect for me @leoquijano thanks |
Hi @niravkrish. Thank you for working on this issue.
|
Hi @leoquijano. Thank you for your report.
The fix will be available with the upcoming 2.3.2 release. |
Hi @magento-engcom-team and @niravkrish, will this be available in an upcoming 2.2.x release as well? I see PR #19627, but I don't see an upcoming 2.2.x release announced. |
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
Steps to reproduce
Expected result
Actual result
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_init
method.[Line 465]
- Call to$widget._EmulateSelected($widget._getSelectedAttributes())
.[Line 1243]
- Theclick
event is triggered with the attribute code, via$.proxy
.[Line 619]
- Theclick
event is handled by_EventListener()
, and the_OnClick()
method is called.Note:
_OnClick
is called with two arguments:$(this)
, and$widget
.emulateClick
event is also available, and will call_OnClick
with a third parameter, the event name (emulateClick
).[Line 725]
- The_loadMedia
method is called byOnClick
, to load the simple product image. Since theclick
event was used, and notemulateClick
, the event name will be undefined.[Line 674]
- TheupdateBaseImage
method is called, with an undefinedeventName
argument.[Line 1156]
- TheprocessUpdateBaseImage
is called, using an undefinedgallery
object. Note that if the event name is undefined (as in this case), theupdateBaseImage
method won't wait until the gallery image is loaded to process the image.[Line 1187]
- TheprocessUpdateBaseImage
method will try to callgallery.updateData
with the updated images, triggering the JavaScript error.emulateClick
event was created to avoid this issue (see commit 97390b3), but it's not being used for this use case.Probable fix:
In the
_EmulateSelected
method described above (line 1243), use theemulateClick
event instead ofclick
:The text was updated successfully, but these errors were encountered: