Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jonathanmoore/c0e0e503aa732bf1c05b7a7be4230c61 to your computer and use it in GitHub Desktop.
Save jonathanmoore/c0e0e503aa732bf1c05b7a7be4230c61 to your computer and use it in GitHub Desktop.
Linked options helper methods for Shopify. See this: http://docs.shopify.com/manual/configuration/store-customization/advanced-navigation/linked-product-options - Updated to work with sectioned themes (tested with District)
<script>
// (c) Copyright 2016 Caroline Schnapp. All Rights Reserved. Contact: [email protected]
// See https://docs.shopify.com/themes/customization/navigation/link-product-options-in-menus
// Modified by Jonathan Moore (Style Hatch) https://github.com/jonathanmoore
/*
Updated to work with sectioned themes
- Added required methods from the deprecated options_selection.js
- Triggers an initial variant change
- Hides sold out variants with only one option
*/
window.addEventListener('DOMContentLoaded', function() {
var Shopify = Shopify || {};
// Required functionality from depricated options_selection.js
Shopify.arrayIncludes = function(e, t) {
for (var n = 0; n < e.length; n++)
if (e[n] == t) return !0;
return !1
}, Shopify.uniq = function(e) {
for (var t = [], n = 0; n < e.length; n++) Shopify.arrayIncludes(t, e[n]) || t.push(e[n]);
return t
}
Shopify.optionsMap = {};
Shopify.updateOptionsInSelector = function(selectorIndex) {
switch (selectorIndex) {
case 0:
var key = 'root';
var selector = jQuery('.single-option-selector:eq(0)');
break;
case 1:
var key = jQuery('.single-option-selector:eq(0)').val();
var selector = jQuery('.single-option-selector:eq(1)');
break;
case 2:
var key = jQuery('.single-option-selector:eq(0)').val();
key += ' / ' + jQuery('.single-option-selector:eq(1)').val();
var selector = jQuery('.single-option-selector:eq(2)');
}
var initialValue = selector.val();
selector.empty();
var availableOptions = Shopify.optionsMap[key];
for (var i=0; i<availableOptions.length; i++) {
var option = availableOptions[i];
var newOption = jQuery('<option></option>').val(option).html(option);
selector.append(newOption);
}
jQuery('.swatch[data-option-index="' + selectorIndex + '"] .swatch-element').each(function() {
if (jQuery.inArray($(this).attr('data-value'), availableOptions) !== -1) {
$(this).removeClass('soldout').show().find(':radio').removeAttr('disabled','disabled').removeAttr('checked');
}
else {
$(this).addClass('soldout').hide().find(':radio').removeAttr('checked').attr('disabled','disabled');
}
});
if (jQuery.inArray(initialValue, availableOptions) !== -1) {
selector.val(initialValue);
}
selector.trigger('change');
};
Shopify.linkOptionSelectors = function(product) {
// Building our mapping object.
for (var i=0; i<product.variants.length; i++) {
var variant = product.variants[i];
if (variant.available) {
// Gathering values for the 1st drop-down.
Shopify.optionsMap['root'] = Shopify.optionsMap['root'] || [];
Shopify.optionsMap['root'].push(variant.option1);
Shopify.optionsMap['root'] = Shopify.uniq(Shopify.optionsMap['root']);
// Gathering values for the 2nd drop-down.
if (product.options.length > 1) {
var key = variant.option1;
Shopify.optionsMap[key] = Shopify.optionsMap[key] || [];
Shopify.optionsMap[key].push(variant.option2);
Shopify.optionsMap[key] = Shopify.uniq(Shopify.optionsMap[key]);
}
// Gathering values for the 3rd drop-down.
if (product.options.length === 3) {
var key = variant.option1 + ' / ' + variant.option2;
Shopify.optionsMap[key] = Shopify.optionsMap[key] || [];
Shopify.optionsMap[key].push(variant.option3);
Shopify.optionsMap[key] = Shopify.uniq(Shopify.optionsMap[key]);
}
}
}
// Update options right away.
Shopify.updateOptionsInSelector(0);
if (product.options.length > 1) Shopify.updateOptionsInSelector(1);
if (product.options.length === 3) Shopify.updateOptionsInSelector(2);
// When there is an update in the first dropdown.
jQuery(".single-option-selector:eq(0)").change(function() {
Shopify.updateOptionsInSelector(1);
if (product.options.length === 3) Shopify.updateOptionsInSelector(2);
return true;
});
// When there is an update in the second dropdown.
jQuery(".single-option-selector:eq(1)").change(function() {
if (product.options.length === 3) Shopify.updateOptionsInSelector(2);
return true;
});
};
{% if product.available and product.options.size > 1 %}
var $addToCartForm = $('form[action="/cart/add"]');
if (window.MutationObserver && $addToCartForm.length) {
if (typeof observer === 'object' && typeof observer.disconnect === 'function') {
observer.disconnect();
}
var config = { childList: true, subtree: true };
var observer = new MutationObserver(function() {
Shopify.linkOptionSelectors({{ product | json }});
observer.disconnect();
});
observer.observe($addToCartForm[0], config);
}
{% endif %}
var selector = jQuery('.single-option-selector:eq(0)');
selector.trigger('change');
{% if product.options.size == 1 %}
{% for variant in product.variants %}
{% unless variant.available %}
jQuery('.single-option-selector option').filter(function() { return jQuery(this).text().trim() === {{ variant.title | json }}; }).remove();
{% endunless %}
{% endfor %}
jQuery('.single-option-selector').trigger('change');
{% endif %}
});
</script>
@impactcolor
Copy link

@PhilipAV to the question the answer is yes. I'm using 3 variations on a responsive theme by out of the sandbox and it works.
This solution isn't meant to be an out of the box solution. The author stated that it was made for the District theme. You will have to break it down to see where the issue is arising from. There can be many things that are causing it not to work.

@impactcolor
Copy link

@PhilipAV one way to debug it would be to add some console.logs to the code to see what's being initialized. In example if Shopify.updateOptionsInSelector = function(selectorIndex) { console.log('updateOptions is initizlized'); ....
Then you'll be able to pin point where the issue is arising from. If I remember correctly on the theme I was using it wasn't picking up the options.map but I can't really remember what it was.

@PhilipAV
Copy link

@impactcolor Thanks for this, I'll give them a try over the weekend.

@pigmug
Copy link

pigmug commented Nov 26, 2020

My shopify site is a bilingual site (using Langify). I've included your code in my site. The English version works OK. However, the other language (Chinese) works as if there is no script included. Would like to know how to handle bilingual case. Thank you.

@xlighting2017
Copy link

@jonathanmoore, Thanks for your code, I have 1 potential issue report here:

if the customer is opening a non-default variant directly (i.e. xxx.com/products/product-name?variant=123456),
there is a small chance that your code may run before the actual product is loaded,

so when this happens,
the "product" is wrong, and the "second option" will show a blank, and the window.url will be "?variant="

here is a setup to reproduce it:
add 2 product variants, with "name option" = a and b,
setup "color option" = red and green, but only "a" have "red", and "b" have "green",
set a+red as the default product, and open the URL of b+green;
refresh the browser mulitple times, and you will see sometimes, it shows b + blank/no green ;

I have an ugly fix, that change

addEventListener('DOMContentLoaded'

to

addEventListener('load'

but I'm thinking if you have a better fix?

Thanks a lot!

@abssimon
Copy link

abssimon commented Jan 21, 2021

I use the Debut theme and get the error "Uncaught ReferenceError: $ is not defined" in line 110.
var $addToCartForm = $('form[action="/cart/add"]');
Same as @akenger. It was the first issue, but never solved, I think

@cyb3rd00d
Copy link

Hi -

Thank you in advance, I got this working. However is there a way to modify the code to unhide sold out items? I ask as our store allows backordering of out of stock.

Thank you

@sanjay-makwana-avidbrio

@jonathanmoore i didnt understand that you code was only for District, excuse me))
But you code is working on Brooklyn theme! but only for drop-down type(

in this picture says only about "non-sectioned versions of Brooklyn. I have section version.

Shopify support even didn't want do anything. They just bla-bla-bla but they can not help.
I think if your code is working for brooklyn theme "drop-down" type probably you may a little bit change and it will be work with "button" type.
One more time sorry)

i have same issue on debut theme.

@sanjay-makwana-avidbrio

I use the Debut theme and get the error "Uncaught ReferenceError: $ is not defined" in line 110.
var $addToCartForm = $('form[action="/cart/add"]');
Same as @akenger. It was the first issue, but never solved, I think

your theme not using http://jquery.com/ add into your theme.liquid

@sanjay-makwana-avidbrio
Copy link

sanjay-makwana-avidbrio commented Mar 31, 2021

Hello @jonathanmoore

does any one working this solution with swatches variants.

I have setup product swatches using the tutorial: https://help.shopify.com/themes/customization/products/features/add-color-swatches

I am using debut theme therefore to make it work I have tweaked the code slightly as per suggestions

I have tried linking product options as per tutorial: https://help.shopify.com/themes/customization/products/variants/link-product-options-in-menus

I have also tried the modified code for sectioned themes without any luck: https://gist.github.com/jonathanmoore/c0e0e503aa732bf1c05b7a7be4230c61

The swatches work fine if the product variants with select input. So if an out of stock variation is selected the add to bag button changes to unavailable.

i dont want to show the unavailable variants.

@srnitish
Copy link

Hey Guys,

I have figured out the solution for all the themes that you guys have on Shopify.

If you are facing the below issue-

  1. If you click on the First Variant, then the options of the remaining variant showing up.
  2. And If you click on the Second Variant then also the options for the Previous Variant showing on the frontend.

This will basically create confusion to select desired variant option for the users.

Kindly drop me an email at [email protected] for the solution.

@sanjay-makwana-avidbrio

@srnitish can post here ?

@andrey-243
Copy link

Hi all I am working on this website (https://kokorikko.com) I did everything specified and still have no result :/ do you have any idea ?
PS: I am using Motion Theme

@faisalanmol
Copy link

dear Jonathan Moore,

I am working with paid prestige theme and want to use hide unavailable variants, I follow all settings but not able to get a solution would you please guide me to sort out this issue,

thanks

@sughramehdi
Copy link

Works for Turbo Portland Theme !

@mijankarim
Copy link

@sughramehdi It's not working for hiding sold-out variants with only one option in the Turbo Portland theme. Can you please tell me which code you used? did you do any changes to the code?

@sciascia
Copy link

Anyone know how to do this with the Shopify 2.0 Dawn theme?

@Johneltonsbrain
Copy link

How do I completely reverse the script? I deleted the created snippet but my sold out / unavailable products are still hidden.

@mbeezee
Copy link

mbeezee commented Nov 3, 2021

Seconding @sciascia - has anyone figured this out for the Dawn theme?

@kyleboehlen
Copy link

kyleboehlen commented Dec 22, 2021

We updated the gist to work with other themes by allowing you to add the select class to a variable, as well as choose whether sold out variants are hidden or not.

https://gist.github.com/kyleboehlen/fd4ce2f4d24a16b74b1e4a30c914f32d

@Johneltonsbrain
Copy link

Has anyone found a solution for the following issue?

When having 3 dropdown menus for 3 variant options, I go ahead and change one option and then it says "unavailable" even though that variant-combination exists. Then I go ahead and change the variant option and it goes to "available". Then I switch back to the old variant options which showed "unavailable" which now shows as "available".

@samoleary
Copy link

samoleary commented Jun 26, 2022

I use the Debut theme and get the error "Uncaught ReferenceError: $ is not defined" in line 110. Same as @akenger. It was the first issue, but never solved, I think

2 possible solutions:

  1. You're trying to use jQuery before it's loaded. In which case you need to move your code to somewhere after it's loaded.
  2. Sometimes themes re-use the $ selector, so you need to change your code to explicitly use jQuery instead (i.e. replace $('.selector') with jQuery('.selector')

Hope that helps.

@invintsLLP
Copy link

Has anyone been successful with getting this to work on the dawn theme?

@torian257x
Copy link

Has anyone found a solution for the following issue?

When having 3 dropdown menus for 3 variant options, I go ahead and change one option and then it says "unavailable" even though that variant-combination exists. Then I go ahead and change the variant option and it goes to "available". Then I switch back to the old variant options which showed "unavailable" which now shows as "available".

the issue is that the script triggers jquery events, and the jquery events are NOT listened to by vanilla javascript events. As a result, the cart does not update correctly

change this line

https://gist.github.com/jonathanmoore/c0e0e503aa732bf1c05b7a7be4230c61#file-gistfile1-html-L63

and this line

https://gist.github.com/jonathanmoore/c0e0e503aa732bf1c05b7a7be4230c61#file-gistfile1-html-L125

to fire this instead
selector[0].dispatchEvent(new Event('change'));

after a quick test, seems to work fine

@ramizibrahimovic
Copy link

@PhilipAV to the question the answer is yes. I'm using 3 variations on a responsive theme by out of the sandbox and it works. This solution isn't meant to be an out of the box solution. The author stated that it was made for the District theme. You will have to break it down to see where the issue is arising from. There can be many things that are causing it not to work.

Hi Omar, hope you are doing well!

I am working on a clients Flex theme and saw your comment. I am working through the linked options snippets and console.logging each and every step.

However I was unable to find it.

Since you solved that issue with your out of the sandbox themes would you please tell me if you by any chance solved it on flex?

I would love to learn from you and solve this issue.

wishing you a lovely day, Ramiz

@ramizibrahimovic
Copy link

Hello,
Thanks for the script.
For those of you who are having issues with the default variant not loading and requiring the user to select a variant before the script fires:
This is what worked for me on the Shopify Debut Theme.
Find the following code:
Shopify.linkOptionSelectors({{ product | json }});
It is located in the following if statement

{% if product.available and product.options.size > 1 %}
...
Shopify.linkOptionSelectors({{ product | json }});
...
{% endif %}

Simply move it directly outside of the if statement:

{% if product.available and product.options.size > 1 %}
...
// Line removed from here
...
{% endif %}

// Line added here
Shopify.linkOptionSelectors({{ product | json }});

Keep working on Debut ... Thanks

Hi William,

I tried this and it worked on Flex theme (out of the sandbox) with 2 variant dropdowns so far that:

  • if you change the first variant dropdown, the second dropdown only has variant options that are available.

However if you select one of the 2. variant options the selected option is not the same as the one that is displayed afterwards. It just changes after you select it.

also it does not work with default value of the 1. variant selection. only if you change the first variant.

Can somebody help me please on that?

It seems such an easy task but became a nightmare for me here :/

Thanks!
Ramiz

@roarsweden
Copy link

Anyone here got this to work with Prestige theme with 7.X.X? I would love to get it to work since i need this badly ;)

@jonathanmoore
Copy link
Author

@roarsweden This is pretty outdated at this point. With the big shifts with Online Store 2.0 and how themes are structured now I doubt this would work with any theme created or updated in the last two years.

@markbecklund
Copy link

@jonathanmoore any pointers as to a solution that WOULD work with current themes?

@JulianAwake
Copy link

Hey is there anything that works with DAWN 15? :) I would be very thankful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment