This commit is contained in:
Mike Jolley 2012-08-08 11:53:36 +01:00
parent 56e8689c5d
commit 6cd409b7a4
2 changed files with 58 additions and 48 deletions

View File

@ -5,108 +5,113 @@ jQuery(document).ready(function($) {
$('.variations select').val('').change();
return false;
}).css('visibility','hidden');
//check if two arrays of attributes match
function variations_match(attrs1, attrs2) {
function variations_match(attrs1, attrs2) {
var match = true;
for (name in attrs1) {
var val1 = attrs1[name];
var val2 = attrs2[name];
if ( val1 !== undefined && val2 !== undefined && val1.length != 0 && val2.length != 0 && val1 != val2 ) {
match = false;
}
}
return match;
}
//search for matching variations for given set of attributes
function find_matching_variations(settings) {
var matching = [];
for (var i = 0; i < product_variations.length; i++) {
var variation = product_variations[i];
var variation_id = variation.variation_id;
if(variations_match(variation.attributes, settings)) {
matching.push(variation);
}
}
return matching;
}
//disable option fields that are unavaiable for current set of attributes
function update_variation_values(variations) {
// Loop through selects and disable/enable options based on selections
$('.variations select').each(function( index, el ){
current_attr_select = $(el);
// Disable all
current_attr_select.find('option:gt(0)').attr('disabled', 'disabled');
// Get name
var current_attr_name = current_attr_select.attr('name');
// Loop through variations
for(num in variations) {
var attributes = variations[num].attributes;
for(attr_name in attributes) {
var attr_val = attributes[attr_name];
if(attr_name == current_attr_name) {
if (attr_val) {
// Decode entities
attr_val = $("<div/>").html( attr_val ).text();
// Add slashes
attr_val = attr_val.replace(/'/g, "\\'");
attr_val = attr_val.replace(/"/g, "\\\"");
// Compare the meercat
current_attr_select.find('option[value="'+attr_val+'"]').removeAttr('disabled');
} else {
current_attr_select.find('option').removeAttr('disabled');
}
}
}
}
});
// Custom event for when variations have been updated
$(document).trigger('woocommerce_update_variation_values');
}
//show single variation details (price, stock, image)
function show_variation(variation) {
var img = $('div.images img:eq(0)');
var link = $('div.images a.zoom:eq(0)');
var o_src = $(img).attr('data-o_src');
var o_href = $(link).attr('data-o_href');
var o_title = $(img).attr('data-o_title');
var variation_image = variation.image_src;
var variation_link = variation.image_link;
var variation_title = variation.image_title;
$('.variations_button').show();
$('.single_variation').html( variation.price_html + variation.availability_html );
if (!o_src) {
if ( ! o_src ) {
$(img).attr('data-o_src', $(img).attr('src'));
}
if (!o_href) {
if ( ! o_href ) {
$(link).attr('data-o_href', $(link).attr('href'));
}
if (variation_image && variation_image.length > 1) {
if ( ! o_title ) {
$(img).attr('data-o_title', $(img).attr('title'));
}
if (variation_image && variation_image.length > 1) {
$(img).attr('src', variation_image);
$(link).attr('href', variation_link);
$(img).attr('alt', variation_title);
@ -114,28 +119,30 @@ jQuery(document).ready(function($) {
} else {
$(img).attr('src', o_src);
$(link).attr('href', o_href);
$(img).attr('alt', o_title);
$(img).attr('title', o_title);
}
if (variation.sku) {
$('.product_meta').find('.sku').text( variation.sku );
} else {
$('.product_meta').find('.sku').text('');
}
$('.single_variation_wrap').find('.quantity').show();
if (variation.min_qty) {
$('.single_variation_wrap').find('input[name=quantity]').attr('data-min', variation.min_qty).val(variation.min_qty);
} else {
$('.single_variation_wrap').find('input[name=quantity]').removeAttr('data-min');
}
if ( variation.max_qty ) {
$('.single_variation_wrap').find('input[name=quantity]').attr('data-max', variation.max_qty);
} else {
$('.single_variation_wrap').find('input[name=quantity]').removeAttr('data-max');
}
if ( variation.is_sold_individually == 'yes' ) {
$('.single_variation_wrap').find('input[name=quantity]').val('1');
$('.single_variation_wrap').find('.quantity').hide();
@ -143,28 +150,28 @@ jQuery(document).ready(function($) {
$('.single_variation_wrap').slideDown('200').trigger('variationWrapShown').trigger('show_variation'); // deprecated variationWrapShown
}
//when one of attributes is changed - check everything to show only valid options
function check_variations( exclude, focus ) {
var all_set = true;
var any_set = false;
var showing_variation = false;
var current_settings = {};
$('.variations select').each(function(){
if ( exclude && $(this).attr('name') == exclude ) {
all_set = false;
current_settings[$(this).attr('name')] = '';
} else {
if ($(this).val().length == 0) {
all_set = false;
} else {
any_set = true;
}
// Encode entities
value = $(this).val()
.replace(/&/g, '&amp;')
@ -176,11 +183,11 @@ jQuery(document).ready(function($) {
// Add to settings array
current_settings[$(this).attr('name')] = value;
}
});
var matching_variations = find_matching_variations(current_settings);
if (all_set) {
var variation = matching_variations.pop();
if (variation) {
@ -195,29 +202,32 @@ jQuery(document).ready(function($) {
update_variation_values(matching_variations);
if ( ! focus ) reset_variation_image();
}
if (any_set) {
if ($('.reset_variations').css('visibility') == 'hidden') $('.reset_variations').css('visibility','visible').hide().fadeIn();
} else {
$('.reset_variations').css('visibility','hidden');
}
}
function reset_variation_image() {
// Reset image
var img = $('div.images img:eq(0)');
var link = $('div.images a.zoom:eq(0)');
var o_src = $(img).attr('data-o_src');
var o_href = $(link).attr('data-o_href');
if ( o_src && o_href ) {
var o_title = $(img).attr('data-o_title');
if ( o_src && o_href && o_title ) {
$(img).attr('src', o_src);
$(link).attr('href', o_href);
$(img).attr('alt', o_title);
$(img).attr('title', o_title);
}
}
$('.variations select').change(function(){
$('form input[name=variation_id]').val('').change();
$('.single_variation_wrap').hide();
$('.single_variation').text('');
@ -226,9 +236,9 @@ jQuery(document).ready(function($) {
if( $().uniform && $.isFunction($.uniform.update) ) {
$.uniform.update();
}
}).bind( 'focusin', function() {
check_variations( $(this).attr('name'), true );
}).change();

View File

@ -1 +1 @@
jQuery(document).ready(function(e){function t(e,t){var n=!0;for(name in e){var r=e[name],i=t[name];r!==undefined&&i!==undefined&&r.length!=0&&i.length!=0&&r!=i&&(n=!1)}return n}function n(e){var n=[];for(var r=0;r<product_variations.length;r++){var i=product_variations[r],s=i.variation_id;t(i.attributes,e)&&n.push(i)}return n}function r(t){e(".variations select").each(function(n,r){current_attr_select=e(r);current_attr_select.find("option:gt(0)").attr("disabled","disabled");var i=current_attr_select.attr("name");for(num in t){var s=t[num].attributes;for(attr_name in s){var o=s[attr_name];if(attr_name==i)if(o){o=e("<div/>").html(o).text();o=o.replace(/'/g,"\\'");o=o.replace(/"/g,'\\"');current_attr_select.find('option[value="'+o+'"]').removeAttr("disabled")}else current_attr_select.find("option").removeAttr("disabled")}}});e(document).trigger("woocommerce_update_variation_values")}function i(t){var n=e("div.images img:eq(0)"),r=e("div.images a.zoom:eq(0)"),i=e(n).attr("data-o_src"),s=e(r).attr("data-o_href"),o=t.image_src,u=t.image_link;e(".variations_button").show();e(".single_variation").html(t.price_html+t.availability_html);i||e(n).attr("data-o_src",e(n).attr("src"));s||e(r).attr("data-o_href",e(r).attr("href"));if(o&&o.length>1){e(n).attr("src",o);e(r).attr("href",u)}else{e(n).attr("src",i);e(r).attr("href",s)}t.sku?e(".product_meta").find(".sku").text(t.sku):e(".product_meta").find(".sku").text("");e(".single_variation_wrap").find(".quantity").show();t.min_qty?e(".single_variation_wrap").find("input[name=quantity]").attr("data-min",t.min_qty).val(t.min_qty):e(".single_variation_wrap").find("input[name=quantity]").removeAttr("data-min");t.max_qty?e(".single_variation_wrap").find("input[name=quantity]").attr("data-max",t.max_qty):e(".single_variation_wrap").find("input[name=quantity]").removeAttr("data-max");if(t.is_sold_individually=="yes"){e(".single_variation_wrap").find("input[name=quantity]").val("1");e(".single_variation_wrap").find(".quantity").hide()}e(".single_variation_wrap").slideDown("200").trigger("variationWrapShown").trigger("show_variation")}function s(t,s){var u=!0,a=!1,f=!1,l={};e(".variations select").each(function(){if(t&&e(this).attr("name")==t){u=!1;l[e(this).attr("name")]=""}else{e(this).val().length==0?u=!1:a=!0;value=e(this).val().replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#039;").replace(/</g,"&lt;").replace(/>/g,"&gt;");l[e(this).attr("name")]=value}});var c=n(l);if(u){var h=c.pop();if(h){e("form input[name=variation_id]").val(h.variation_id).change();i(h)}else{e(".variations select").val("");s||o()}}else{r(c);s||o()}a?e(".reset_variations").css("visibility")=="hidden"&&e(".reset_variations").css("visibility","visible").hide().fadeIn():e(".reset_variations").css("visibility","hidden")}function o(){var t=e("div.images img:eq(0)"),n=e("div.images a.zoom:eq(0)"),r=e(t).attr("data-o_src"),i=e(n).attr("data-o_href");if(r&&i){e(t).attr("src",r);e(n).attr("href",i)}}e(".reset_variations").click(function(){e(".variations select").val("").change();return!1}).css("visibility","hidden");e(".variations select").change(function(){e("form input[name=variation_id]").val("").change();e(".single_variation_wrap").hide();e(".single_variation").text("");s("",!1);e(this).blur();e().uniform&&e.isFunction(e.uniform.update)&&e.uniform.update()}).bind("focusin",function(){s(e(this).attr("name"),!0)}).change()});
jQuery(document).ready(function(e){function t(e,t){var n=!0;for(name in e){var r=e[name],i=t[name];r!==undefined&&i!==undefined&&r.length!=0&&i.length!=0&&r!=i&&(n=!1)}return n}function n(e){var n=[];for(var r=0;r<product_variations.length;r++){var i=product_variations[r],s=i.variation_id;t(i.attributes,e)&&n.push(i)}return n}function r(t){e(".variations select").each(function(n,r){current_attr_select=e(r);current_attr_select.find("option:gt(0)").attr("disabled","disabled");var i=current_attr_select.attr("name");for(num in t){var s=t[num].attributes;for(attr_name in s){var o=s[attr_name];if(attr_name==i)if(o){o=e("<div/>").html(o).text();o=o.replace(/'/g,"\\'");o=o.replace(/"/g,'\\"');current_attr_select.find('option[value="'+o+'"]').removeAttr("disabled")}else current_attr_select.find("option").removeAttr("disabled")}}});e(document).trigger("woocommerce_update_variation_values")}function i(t){var n=e("div.images img:eq(0)"),r=e("div.images a.zoom:eq(0)"),i=e(n).attr("data-o_src"),s=e(r).attr("data-o_href"),o=e(n).attr("data-o_title"),u=t.image_src,a=t.image_link,f=t.image_title;e(".variations_button").show();e(".single_variation").html(t.price_html+t.availability_html);i||e(n).attr("data-o_src",e(n).attr("src"));s||e(r).attr("data-o_href",e(r).attr("href"));o||e(n).attr("data-o_title",e(n).attr("title"));if(u&&u.length>1){e(n).attr("src",u);e(r).attr("href",a);e(n).attr("alt",f);e(n).attr("title",f)}else{e(n).attr("src",i);e(r).attr("href",s);e(n).attr("alt",o);e(n).attr("title",o)}t.sku?e(".product_meta").find(".sku").text(t.sku):e(".product_meta").find(".sku").text("");e(".single_variation_wrap").find(".quantity").show();t.min_qty?e(".single_variation_wrap").find("input[name=quantity]").attr("data-min",t.min_qty).val(t.min_qty):e(".single_variation_wrap").find("input[name=quantity]").removeAttr("data-min");t.max_qty?e(".single_variation_wrap").find("input[name=quantity]").attr("data-max",t.max_qty):e(".single_variation_wrap").find("input[name=quantity]").removeAttr("data-max");if(t.is_sold_individually=="yes"){e(".single_variation_wrap").find("input[name=quantity]").val("1");e(".single_variation_wrap").find(".quantity").hide()}e(".single_variation_wrap").slideDown("200").trigger("variationWrapShown").trigger("show_variation")}function s(t,s){var u=!0,a=!1,f=!1,l={};e(".variations select").each(function(){if(t&&e(this).attr("name")==t){u=!1;l[e(this).attr("name")]=""}else{e(this).val().length==0?u=!1:a=!0;value=e(this).val().replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#039;").replace(/</g,"&lt;").replace(/>/g,"&gt;");l[e(this).attr("name")]=value}});var c=n(l);if(u){var h=c.pop();if(h){e("form input[name=variation_id]").val(h.variation_id).change();i(h)}else{e(".variations select").val("");s||o()}}else{r(c);s||o()}a?e(".reset_variations").css("visibility")=="hidden"&&e(".reset_variations").css("visibility","visible").hide().fadeIn():e(".reset_variations").css("visibility","hidden")}function o(){var t=e("div.images img:eq(0)"),n=e("div.images a.zoom:eq(0)"),r=e(t).attr("data-o_src"),i=e(n).attr("data-o_href"),s=e(t).attr("data-o_title");if(r&&i&&s){e(t).attr("src",r);e(n).attr("href",i);e(t).attr("alt",s);e(t).attr("title",s)}}e(".reset_variations").click(function(){e(".variations select").val("").change();return!1}).css("visibility","hidden");e(".variations select").change(function(){e("form input[name=variation_id]").val("").change();e(".single_variation_wrap").hide();e(".single_variation").text("");s("",!1);e(this).blur();e().uniform&&e.isFunction(e.uniform.update)&&e.uniform.update()}).bind("focusin",function(){s(e(this).attr("name"),!0)}).change()});