-
Notifications
You must be signed in to change notification settings - Fork 487
Moved product JS to external file (mostly) #220
Changes from 4 commits
ad8fc86
9038e9c
e4c7de0
198efed
1109dc0
0403085
8c33dfb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,12 @@ timber.cacheSelectors = function () { | |
|
||
// Product Page | ||
$productImage: $('#productPhotoImg'), | ||
$thumbImages: $('#productThumbs').find('a.product-photo-thumb') | ||
$thumbImages: $('#productThumbs').find('a.product-photo-thumb'), | ||
$addToCart: $('#addToCart'), | ||
$productPrice: $('#productPrice'), | ||
$comparePrice: $('#comparePrice'), | ||
$quantityElements: $('.quantity-selector, label + .js-qty'), | ||
$addToCartText: $('#addToCartText') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should these be saved to timber.cache? Otherwise, since all are product related, they can also be saved to a single There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I like the idea of productCache or something. That way I could move the initializing JS back to the product template instead of theme.liquid. On it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do these need to be saved outside the productJs function? why not just declare them there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, they could be inside. Thought that went against our styleguide though. It would actually be my preference in this case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which part of the guide are you referring to? Vars should be declared at the top of a function, but it doesn't have to happen anywhere in particular other than that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I must be thinking of something else I read, not the guide. Apologies for the confusion. |
||
} | ||
}; | ||
|
||
|
@@ -101,6 +106,56 @@ timber.accessibleNav = function () { | |
} | ||
}; | ||
|
||
timber.productJs = function (options) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. knit-pick, but seems redundant to call something "js" inside a .js file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hah that's already irked me too. Thinking of a better name now. |
||
var money_format = options.money_format, | ||
variant = options.variant, | ||
selector = options.selector; | ||
|
||
if (variant) { | ||
|
||
// Update variant image, if one is set | ||
if (variant.featured_image) { | ||
var newImg = variant.featured_image, | ||
el = timber.cache.$productImage[0]; | ||
Shopify.Image.switchImage(newImg, el, timber.switchImage); | ||
} | ||
|
||
// Select a valid variant if available | ||
if (variant.available) { | ||
// Available, enable the submit button, change text, show quantity elements | ||
timber.cache.$addToCart.removeClass('disabled').prop('disabled', false); | ||
timber.cache.$addToCartText.text('{{ "products.product.add_to_cart" | t }}'); | ||
timber.cache.$quantityElements.show(); | ||
} else { | ||
// Sold out, disable the submit button, change text, hide quantity elements | ||
timber.cache.$addToCart.addClass('disabled').prop('disabled', true); | ||
timber.cache.$addToCartText.text('{{ "products.product.sold_out" | t }}'); | ||
timber.cache.$quantityElements.hide(); | ||
} | ||
|
||
// Regardless of stock, update the product price | ||
timber.cache.$productPrice.html( Shopify.formatMoney(variant.price, money_format) ); | ||
|
||
// Also update and show the product's compare price if necessary | ||
if ( variant.compare_at_price > variant.price ) { | ||
timber.cache.$comparePrice | ||
.html('{{ "products.product.compare_at" | t }}: ' + Shopify.formatMoney(variant.compare_at_price, money_format)) | ||
.show(); | ||
} else { | ||
timber.cache.$comparePrice.hide(); | ||
} | ||
|
||
} else { | ||
// The variant doesn't exist, disable submit button. | ||
// This may be an error or notice that a specific variant is not available. | ||
// To only show available variants, implement linked product options: | ||
// - http://docs.shopify.com/manual/configuration/store-customization/advanced-navigation/linked-product-options | ||
timber.cache.$addToCart.addClass('disabled').prop('disabled', true); | ||
timber.cache.$addToCartText.text('{{ "products.product.unavailable" | t }}'); | ||
timber.cache.$quantityElements.hide(); | ||
} | ||
}; | ||
|
||
timber.productImageSwitch = function () { | ||
if ( timber.cache.$thumbImages.length ) { | ||
// Switch the main image with one of the thumbnails | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,54 @@ | |
{% endcomment %} | ||
{{ 'shop.js' | asset_url | script_tag }} | ||
|
||
{% comment %} | ||
Product page specific JS | ||
{% endcomment %} | ||
{% if template contains 'product' %} | ||
{% comment %} | ||
To take advantage of a callback on the select dropdown, add option_selection.js | ||
and customize the JS below as you need. | ||
|
||
Currently, the code below does: | ||
- Hides your <select> tag from above | ||
- Breaks out the product variants into separate product options, if more than one exists | ||
- Generates a <select> tag for each product option | ||
|
||
Callback notes: | ||
- Keep the callback available to the global scope (window.selectCallback) so that advanced | ||
addons can override it. | ||
* E.g. multiple currencies http://docs.shopify.com/manual/configuration/store-customization/currencies-and-translations/currencies/how-to-toggle-between-two-currencies | ||
{% endcomment %} | ||
{{ 'option_selection.js' | shopify_asset_url | script_tag }} | ||
<script> | ||
var selectCallback = function(variant, selector) { | ||
timber.productJs({ | ||
money_format: "{{ shop.money_format }}", | ||
variant: variant, | ||
selector: selector | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice :) |
||
}); | ||
}; | ||
|
||
jQuery(function($) { | ||
new Shopify.OptionSelectors('productSelect', { | ||
product: {{ product | json }}, | ||
onVariantSelected: selectCallback, | ||
enableHistoryState: true | ||
}); | ||
|
||
// Add label if only one product option and it isn't 'Title'. Could be 'Size'. | ||
{% if product.options.size == 1 and product.options.first != 'Title' %} | ||
$('.selector-wrapper:eq(0)').prepend('<label>{{ product.options.first | escape }}</label>'); | ||
{% endif %} | ||
|
||
// Hide selectors if we only have 1 variant and its title contains 'Default'. | ||
{% if product.variants.size == 1 and product.variants.first.title contains 'Default' %} | ||
$('.selector-wrapper').hide(); | ||
{% endif %} | ||
}); | ||
</script> | ||
{% endif %} | ||
|
||
{% comment %} | ||
Ajaxify your cart with this plugin. Adding a product will reveal a drawer, modal, or confirmation button. Enabled by default in theme settings. | ||
|
||
|
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.
😧