Skip to content

Commit

Permalink
Fixed bug when reading selected option values. Removed unused script …
Browse files Browse the repository at this point in the history
…tag containing variant json. (#3520)
  • Loading branch information
lhoffbeck authored and dan-menard committed Jul 3, 2024
1 parent afbbaf9 commit 4331aed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
20 changes: 8 additions & 12 deletions assets/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -1059,8 +1059,7 @@ class VariantSelects extends HTMLElement {
connectedCallback() {
this.addEventListener('change', (event) => {
const target = this.getInputForEventTarget(event.target);
this.currentVariant = this.getVariantData(target.id);
this.updateSelectedSwatchValue(event);
this.updateSelectionMetadata(event);

publish(PUB_SUB_EVENTS.optionValueSelectionChange, {
data: {
Expand All @@ -1072,10 +1071,15 @@ class VariantSelects extends HTMLElement {
});
}

updateSelectedSwatchValue({ target }) {
updateSelectionMetadata({ target }) {
const { value, tagName } = target;

if (tagName === 'SELECT' && target.selectedOptions.length) {
Array.from(target.options)
.find((option) => option.getAttribute('selected'))
.removeAttribute('selected');
target.selectedOptions[0].setAttribute('selected', 'selected');

const swatchValue = target.selectedOptions[0].dataset.optionSwatchValue;
const selectedDropdownSwatchValue = target
.closest('.product-form__input')
Expand Down Expand Up @@ -1103,16 +1107,8 @@ class VariantSelects extends HTMLElement {
return target.tagName === 'SELECT' ? target.selectedOptions[0] : target;
}

getVariantData(inputId) {
return JSON.parse(this.getVariantDataElement(inputId).textContent);
}

getVariantDataElement(inputId) {
return this.querySelector(`script[type="application/json"][data-resource="${inputId}"]`);
}

get selectedOptionValues() {
return Array.from(this.querySelectorAll('select, fieldset input:checked')).map(
return Array.from(this.querySelectorAll('select option[selected], fieldset input:checked')).map(
({ dataset }) => dataset.optionValueId
);
}
Expand Down
4 changes: 0 additions & 4 deletions snippets/product-variant-options.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,4 @@
{%- endif %}
</option>
{%- endif -%}

<script type="application/json" data-resource="{{ input_id }}">
{{ value.variant | json }}
</script>
{%- endfor -%}

0 comments on commit 4331aed

Please sign in to comment.