Skip to content

Instantly share code, notes, and snippets.

@Jerska
Created October 13, 2017 15:28
Show Gist options
  • Save Jerska/ac8a468ef43142663d0e0f0f12ef069a to your computer and use it in GitHub Desktop.
Save Jerska/ac8a468ef43142663d0e0f0f12ef069a to your computer and use it in GitHub Desktop.
Differences algolia
diff --git a/public/shopify/assets/javascripts/algolia_autocomplete.js.liquid b/public/shopify/assets/javascripts/algolia_autocomplete.js.liquid
index 0130d68..be51693 100644
--- a/public/shopify/assets/javascripts/algolia_autocomplete.js.liquid
+++ b/public/shopify/assets/javascripts/algolia_autocomplete.js.liquid
@@ -19,7 +19,7 @@
return algolia.render(params.templates.empty, props);
},
suggestion: function displaySuggestion (hit) {
- return algolia.render(params.templates.hit, hit);
+ return algolia.render(params.templates.hit, Object.assign({ _distinct: params.distinct }, hit));
}
};
@@ -30,7 +30,6 @@
return algolia.render(params.templates.footer, {
query: props.query,
nbHits: content.nbHits
-
});
}
@@ -39,6 +38,7 @@
return {
name: section,
source: $.fn.autocomplete.sources.hits(params.index, {
+ distinct: !!params.distinct,
hitsPerPage: params.hitsPerPage,
highlightPreTag: '<span class="aa-highlight">',
highlightPostTag: '</span>'
@@ -124,6 +124,7 @@
poweredBy: algolia.config.powered_by,
products: {
enabled: algolia.config.index_products,
+ distinct: algolia.config.show_products,
hitsPerPage: algolia.config.products_autocomplete_hits_per_page,
index: index('products'),
templates: {
diff --git a/public/shopify/assets/javascripts/algolia_helpers.js.liquid b/public/shopify/assets/javascripts/algolia_helpers.js.liquid
index 1b63d28..ff2765f 100644
--- a/public/shopify/assets/javascripts/algolia_helpers.js.liquid
+++ b/public/shopify/assets/javascripts/algolia_helpers.js.liquid
@@ -9,7 +9,17 @@
return algolia.formatMoney(+value * 100);
};
+ function formattedPriceWithComparison (price, compare_at_price, price_ratio) {
+ var comparing = +compare_at_price && +compare_at_price > +price,
+ discount_ratio = 1.0 - price_ratio,
+ res = '<b>' + formatPrice(price) + '</b>';
+ if (comparing) {
+ res += ' <span class="ais-hit--price-striked"><span>' + formatPrice(compare_at_price) + '</span></span> ';
+ res += ' <span class="ais-hit--price-discount" style="font-weight: ' + (Math.floor(discount_ratio * 10) * 100) + ';">-' + Math.floor(discount_ratio * 100) + '%</span>';
+ }
+ return res;
+ }
var escapeHtml = function escapeHtml (unsafe) {
return unsafe
@@ -31,20 +41,33 @@
formattedPriceWithoutDecimals: function formattedPriceWithoutDecimals (text, render) {
return formatPrice(render(text)).replace(/\.\d+$/, '');
},
- formattedPriceWithComparison: function formattedPriceWithComparison () {
- var comparing = +this.compare_at_price && +this.compare_at_price > +this.price,
- discount_ratio = 1.0 - this.price_ratio,
- res = '<b>' + formatPrice(this.price) + '</b>';
- if (comparing) {
- res += ' <span class="ais-hit--price-striked"><span>' + formatPrice(this.compare_at_price) + '</span></span> ';
- res += ' <span class="ais-hit--price-discount" style="font-weight: ' + (Math.floor(discount_ratio * 10) * 100) + ';">-' + Math.floor(discount_ratio * 100) + '%</span>';
+ formattedPriceWithComparison: function _formattedPriceWithComparison () {
+ return formattedPriceWithComparison(this.price, this.compare_at_price, this.price_ratio);
+ },
+ autocompletePrice: function autocompletePrice (text, render) {
+ if (this._distinct) {
+ var min = this.variants_min_price;
+ var max = this.variants_max_price;
+ if (min !== max) {
+ return formatPrice(min) + ' - ' + formatPrice(max);
+ }
+ }
+ return formatPrice(this.price);
+ return formattedPriceWithComparison(this.price, this.compare_at_price, this.price_ratio);
+ },
+ instantsearchPrice: function instantsearchPrice (text, render) {
+ if (this._distinct) {
+ var min = this.variants_min_price;
+ var max = this.variants_max_price;
+ if (min !== max) {
+ return '<b>' + formatPrice(min) + ' - ' + formatPrice(max) + '</b>';
+ }
}
-
- return res;
+ return formattedPriceWithComparison(this.price, this.compare_at_price, this.price_ratio);
},
fullTitle: function fullTitle () {
var res = this.title;
- if (this.variant_title && this.variant_title !== 'Default Title' && this.variant_title !== 'Default') {
+ if (!this._distinct && this.variant_title && this.variant_title !== 'Default Title' && this.variant_title !== 'Default') {
res += ' (' + this.variant_title + ')';
}
@@ -52,7 +75,7 @@
},
fullHTMLTitle: function fullHTMLTitle () {
var res = this._highlightResult.title.value;
- if (this.variant_title && this.variant_title !== 'Default Title' && this.variant_title !== 'Default') {
+ if (!this._distinct && this.variant_title && this.variant_title !== 'Default Title' && this.variant_title !== 'Default') {
res += ' <span class="algolia-variant">(' + this._highlightResult.variant_title.value + ')</span>';
}
diff --git a/public/shopify/assets/javascripts/algolia_instant_search.js.liquid b/public/shopify/assets/javascripts/algolia_instant_search.js.liquid
index 8719464..ca707ea 100644
--- a/public/shopify/assets/javascripts/algolia_instant_search.js.liquid
+++ b/public/shopify/assets/javascripts/algolia_instant_search.js.liquid
@@ -11,6 +11,7 @@
var instant = algolia.instantsearch = {
colors: algolia.config.colors,
+ distinct: !!algolia.config.show_products,
facets: {
hidden: algolia.hiddenFacets,
shown: algolia.shownFacets,
@@ -100,13 +101,14 @@
}
});
- // Highlight tags
+ // Query parameters
instant.search.addWidget({
init: function (opts) {
var helper = opts.helper;
var page = helper.getPage();
helper.setQueryParameter('highlightPreTag', '<span class="ais-highlight">');
helper.setQueryParameter('highlightPostTag', '</span>');
+ helper.setQueryParameter('distinct', instant.distinct);
helper.setPage(page);
}
});
@@ -239,6 +241,7 @@
},
transformData: {
item: function (product) {
+ product._distinct = instant.distinct;
product.can_order = (
product.inventory_management !== 'shopify' ||
product.inventory_policy === 'continue' ||
diff --git a/public/shopify/snippets/algolia_autocomplete_product.hogan.liquid b/public/shopify/snippets/algolia_autocomplete_product.hogan.liquid
index e47b131..8ed5ca5 100644
--- a/public/shopify/snippets/algolia_autocomplete_product.hogan.liquid
+++ b/public/shopify/snippets/algolia_autocomplete_product.hogan.liquid
@@ -13,7 +13,7 @@
[[/ vendor ]]
</p>
<p class="aa-product-price">
- [[# helpers.formattedPrice ]][[ price ]][[/ helpers.formattedPrice ]]
+ [[# helpers.autocompletePrice ]][[/ helpers.autocompletePrice ]]
</p>
</div>
</div>
diff --git a/public/shopify/snippets/algolia_instant_search_product.hogan.liquid b/public/shopify/snippets/algolia_instant_search_product.hogan.liquid
index beb1bce..fc7fb7c 100644
--- a/public/shopify/snippets/algolia_instant_search_product.hogan.liquid
+++ b/public/shopify/snippets/algolia_instant_search_product.hogan.liquid
@@ -11,7 +11,7 @@
[[& _highlightResult.product_type.value ]]
[[# vendor ]] by [[& _highlightResult.vendor.value ]][[/ vendor ]]
</p>
- <p class="ais-hit--price">[[# helpers.formattedPriceWithComparison ]][[/ helpers.formattedPriceWithComparison ]]</p>
+ <p class="ais-hit--price">[[# helpers.instantsearchPrice ]][[/ helpers.instantsearchPrice ]]</p>
<!-- Extra info examples - Remove the display: none to show them -->
<p class="ais-hit--info" style="display: none">
[[# sku ]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment