javascript - How can I create "smarter" swatches for a shopify theme -


i adding swatches shopify site via tutorial , think great, hoping have them bit "smarter".

http://docs.shopify.com/manual/configuration/store-customization/add-color-swatches-to-your-products#demo

if have hat 4 variants -- sm/red, md/red, sm/blue, md/blue -- 4 buttons show on product page. 2 on top, 1 says "sm" , 1 says "md" below 2 buttons 2 buttons color, 1 red , 1 blue.

lets have in stock except sm/red. customer clicks button "sm" , sees there 2 color options below it. unfortunately, click red, , find out sold out "add cart" button changing "sold out".

i prefer if user clicked "sm" button, red swatch greyed out or got x, user received feedback on availability.

how hard make happen?

that's not difficult @ all. if have implemented colour swatches per shopify tutorial mentioned in question, need add code selectcallback function.

add product.liquid:

...  var selectcallback = function(variant, selector) {    ...    var selectedsize = jquery('.size.options li.selected span').text();    if (selectedsize.length > 0) {     var variants = selector.product.variants;     var varianttitles = [];     var i;      (i = 0; < variants.length; i++) {       varianttitles.push(variants[i].title);     }      jquery('.color.options li').each( function() {       var varianttitle = selectedsize + " / " + jquery('div', this).text();        // if varianttitle valid variant & not sold out, remove unavailable class       var variantindex = jquery.inarray(varianttitle, varianttitles);       if (variantindex != -1 && variants[variantindex].available == true) {         jquery('span', this).removeclass('unavailable');       }       // if not valid variant or sold out, add unavailable class       else {         jquery('span', this).addclass('unavailable');       }     });   } };  ... 

add swatches.liquid (within <style> tag):

.unavailable { opacity: 0.2; } 

then if have 2 sizes (sm, md) , 2 colours (red, blue), , "sm / red" sold out, this:

small/red sold out

i've put code in gist.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -