-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Fix swatch-renderer.js #39924
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
base: 2.4-develop
Are you sure you want to change the base?
Fix swatch-renderer.js #39924
Conversation
Fix for "Uncaught TypeError: salableProducts is undefined" when no variant is available
Hi @fisch42. Thank you for your contribution!
Allowed build names are:
You can find more information about the builds here For more details, review the Code Contributions documentation. |
@magento create issue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request adds a guard to prevent salableProducts
from being accessed when it’s undefined, avoiding the TypeError in swatch-renderer.js.
- Added a check for
salableProducts
before usinghasOwnProperty
- Included a length check to skip disabling when no variants are available
@@ -516,7 +516,7 @@ define([ | |||
swatchOptions.each(function (key, value) { | |||
let optionId = $(value).data('option-id'); | |||
|
|||
if (!salableProducts.hasOwnProperty(optionId)) { | |||
if (salableProducts != undefined && salableProducts.length > 0 && !salableProducts.hasOwnProperty(optionId)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The added salableProducts.length > 0
check assumes salableProducts
is an array and will skip disabling options when it's an object (empty or not). Instead, guard only against undefined and then check properties, e.g.: if (salableProducts && !salableProducts.hasOwnProperty(optionId)) { … }
.
if (salableProducts != undefined && salableProducts.length > 0 && !salableProducts.hasOwnProperty(optionId)) { | |
if (salableProducts && !salableProducts.hasOwnProperty(optionId)) { |
Copilot uses AI. Check for mistakes.
Fix for "Uncaught TypeError: salableProducts is undefined" when no variant is available
Description (*)
This code fixes an error, when a configurable product has no available variant.
This is the JS error in the browser console:
Related Pull Requests
Fixed Issues (if relevant)
Manual testing scenarios (*)
Questions or comments
Contribution checklist (*)
Resolved issues: