Last active
June 26, 2023 17:38
-
-
Save macdonaldr93/9200736820858b86f1f869f569fd6783 to your computer and use it in GitHub Desktop.
OpenTheme - A facade to bridge themes and a standard Theme API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="collection-grid"> | |
{% for product in collection.products %} | |
<div | |
class="collection-grid__item" | |
<!-- We to know what product we're dealing with --> | |
data-product-id="{{ product.id }}" | |
<!-- A lot of API calls need the product handle, not ID --> | |
data-product-handle="{{ product.handle }}" | |
<!-- We don't always know which variant is selected within a grid item --> | |
data-variant-id="{{ product.selected_or_first_available_variant }}"> | |
</div> | |
{% endfor %} | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.OpenTheme = window.OpenTheme || {}; | |
// Facade between theme and a standard implementation | |
window.OpenTheme = { | |
selectors: { | |
productForm: '.product-details form[action="/cart"]', | |
productPrice: '.product-details .price', | |
}, | |
on(name, cb) { | |
switch (name) { | |
case 'variant:changed': { | |
document.addEventListener('archetype:variant:updated', cb); | |
} | |
case 'quickview:opened': { | |
document.addEventListener('archetype:quickview:opened', cb); | |
} | |
default: { | |
throw new Error(`${name} is invalid`); | |
} | |
} | |
}, | |
queryProductForm() { | |
return document.querySelector('.product-details form[action="/cart"]'); | |
}, | |
queryProductPrice() { | |
return document.querySelector('.product-details .price'); | |
}, | |
// There's no standard for tracking selected variants in quickviews | |
// Or on collection pages. | |
updateQuickviewVariant(id) { | |
const searchParams = new URLSearchParams(window.location.search); | |
searchParams.set('qv_variant_id', id); | |
window.location.search = searchParams.toString(); | |
}, | |
// There's no standard for tracking which selling plan is selected | |
updateSellingPlan(id) { | |
const searchParams = new URLSearchParams(window.location.search); | |
searchParams.set('selling_plan', id); | |
window.location.search = searchParams.toString(); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is very early. I'll keep updating it as we discuss. The main areas are:
variant:changed
: how does this event work in a grid? in a quickview?selling_plan:changed
quickview:opened
quickview:closed
data-product-id
data-product-handle
data-variant-id
variant
selling_plan
modal_variant
: I'm not sure about this one, but we need some way of knowing that a variant is selected in some other context