Skip to content
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

🐛 Fix option value dropdowns and simplify payload #3520

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 -%}
Loading