From f7a065fb53b0d2297d496665dc3c2c063df2e792 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 28 Oct 2015 14:27:02 +0000 Subject: [PATCH] Button initial state --- assets/js/frontend/add-to-cart-variation.js | 35 +++++++++++-------- .../js/frontend/add-to-cart-variation.min.js | 2 +- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/assets/js/frontend/add-to-cart-variation.js b/assets/js/frontend/add-to-cart-variation.js index 0a248a601ad..2ca21d09696 100644 --- a/assets/js/frontend/add-to-cart-variation.js +++ b/assets/js/frontend/add-to-cart-variation.js @@ -6,20 +6,32 @@ ;(function ( $, window, document, undefined ) { $.fn.wc_variation_form = function() { - var $form = this, - $single_variation = $form.find( '.single_variation' ), - $product = $form.closest('.product'), - $product_id = parseInt( $form.data( 'product_id' ), 10 ), - $product_variations = $form.data( 'product_variations' ), - $use_ajax = $product_variations === false, - $xhr = false, - $reset_variations = $form.find( '.reset_variations' ); + var $form = this, + $single_variation = $form.find( '.single_variation' ), + $product = $form.closest('.product'), + $product_id = parseInt( $form.data( 'product_id' ), 10 ), + $product_variations = $form.data( 'product_variations' ), + $use_ajax = $product_variations === false, + $xhr = false, + $reset_variations = $form.find( '.reset_variations' ), + template = wp.template( 'variation-template' ), + button_template = wp.template( 'variation-add-to-cart-template' ), + unavailable_template = wp.template( 'unavailable-variation-template' ), + $single_variation_wrap = $form.find( '.single_variation_wrap' ), + $variations_button = $form.find( '.variations_button' ); // Unbind any existing events $form.unbind( 'check_variations update_variation_values found_variation' ); $form.find( '.reset_variations' ).unbind( 'click' ); $form.find( '.variations select' ).unbind( 'change focusin' ); + // Output button intial state + $variations_button.html( button_template( { + variation_id: 0, + min_qty: '', + max_qty: '' + } ) ); + // Bind new events to form $form @@ -145,14 +157,9 @@ // Show single variation details (price, stock, image) .on( 'found_variation', function( event, variation ) { - var template = wp.template( 'variation-template' ), - button_template = wp.template( 'variation-add-to-cart-template' ), - unavailable_template = wp.template( 'unavailable-variation-template' ), - $single_variation_wrap = $form.find( '.single_variation_wrap' ), - $sku = $product.find( '.product_meta' ).find( '.sku' ), + var $sku = $product.find( '.product_meta' ).find( '.sku' ), $weight = $product.find( '.product_weight' ), $dimensions = $product.find( '.product_dimensions' ), - $variations_button = $form.find( '.variations_button' ), purchasable = true; // Display SKU diff --git a/assets/js/frontend/add-to-cart-variation.min.js b/assets/js/frontend/add-to-cart-variation.min.js index 978ca6c2802..0aeae58967c 100644 --- a/assets/js/frontend/add-to-cart-variation.min.js +++ b/assets/js/frontend/add-to-cart-variation.min.js @@ -1,4 +1,4 @@ /*! * Variations Plugin */ -!function(a,b,c,d){a.fn.wc_variation_form=function(){var c=this,d=c.find(".single_variation"),f=c.closest(".product"),g=parseInt(c.data("product_id"),10),h=c.data("product_variations"),i=h===!1,j=!1,k=c.find(".reset_variations");return c.unbind("check_variations update_variation_values found_variation"),c.find(".reset_variations").unbind("click"),c.find(".variations select").unbind("change focusin"),c.on("click",".reset_variations",function(){return c.find(".variations select").val("").change(),c.trigger("reset_data"),!1}).on("hide_variation",function(){return c.find(".single_add_to_cart_button").attr("disabled","disabled").attr("title",wc_add_to_cart_variation_params.i18n_make_a_selection_text),!1}).on("show_variation",function(a,b,d){return d?c.find(".single_add_to_cart_button").removeAttr("disabled").removeAttr("title"):c.find(".single_add_to_cart_button").attr("disabled","disabled").attr("title",wc_add_to_cart_variation_params.i18n_unavailable_text),!1}).on("reload_product_variations",function(){h=c.data("product_variations"),i=h===!1}).on("reset_data",function(){a(".sku").wc_reset_content(),a(".product_weight").wc_reset_content(),a(".product_dimensions").wc_reset_content(),c.trigger("reset_image"),d.slideUp(200).trigger("hide_variation")}).on("reset_image",function(){c.wc_variations_image_update(!1)}).on("change",".variations select",function(){if(c.find('input[name="variation_id"], input.variation_id').val("").change(),c.find(".wc-no-matching-variations").remove(),i){j&&j.abort();var b=!0,d=!1,e={};c.find(".variations select").each(function(){var c=a(this).data("attribute_name")||a(this).attr("name");0===a(this).val().length?b=!1:d=!0,e[c]=a(this).val()}),b?(e.product_id=g,j=a.ajax({url:wc_cart_fragments_params.wc_ajax_url.toString().replace("%%endpoint%%","get_variation"),type:"POST",data:e,success:function(a){a?c.trigger("found_variation",[a]):(c.trigger("reset_data"),c.find(".single_variation").after('

'+wc_add_to_cart_variation_params.i18n_no_matching_variations_text+"

"),c.find(".wc-no-matching-variations").slideDown(200))}})):c.trigger("reset_data"),d?"hidden"===k.css("visibility")&&k.css("visibility","visible").hide().fadeIn():k.css("visibility","hidden")}else c.trigger("woocommerce_variation_select_change"),c.trigger("check_variations",["",!1]),a(this).blur();a(".product.has-default-attributes > .images").fadeTo(200,1),c.trigger("woocommerce_variation_has_changed")}).on("focusin touchstart",".variations select",function(){a(this).find("option:selected").attr("selected","selected"),i||(c.trigger("woocommerce_variation_select_focusin"),c.trigger("check_variations",[a(this).data("attribute_name")||a(this).attr("name"),!0]))}).on("found_variation",function(a,b){var e=wp.template("variation-template"),g=wp.template("variation-add-to-cart-template"),h=wp.template("unavailable-variation-template"),i=c.find(".single_variation_wrap"),j=f.find(".product_meta").find(".sku"),k=f.find(".product_weight"),l=f.find(".product_dimensions"),m=c.find(".variations_button"),n=!0;b.sku?j.wc_set_content(b.sku):j.wc_reset_content(),b.weight?k.wc_set_content(b.weight):k.wc_reset_content(),b.dimensions?l.wc_set_content(b.dimensions):l.wc_reset_content(),c.wc_variations_image_update(b),b.variation_is_visible?(d.html(e({price:b.price_html,availability:b.availability_html,description:b.variation_description})),m.html(g({variation_id:b.variation_id,min_qty:b.min_qty,max_qty:b.max_qty}))):(d.html(h),m.html(g({variation_id:0,min_qty:"",max_qty:""}))),"yes"===b.is_sold_individually?(i.find(".quantity input.qty").val("1"),i.find(".quantity").hide()):i.find(".quantity").show(),b.is_purchasable&&b.is_in_stock&&b.variation_is_visible||(n=!1),d.slideDown(200).trigger("show_variation",[b,n])}).on("check_variations",function(c,f,g){if(!i){var j=!0,k=!1,l={},m=a(this),n=m.find(".reset_variations");m.find(".variations select").each(function(){var b=a(this).data("attribute_name")||a(this).attr("name");0===a(this).val().length?j=!1:k=!0,f&&b===f?(j=!1,l[b]=""):l[b]=a(this).val()});var o=e.find_matching_variations(h,l);if(j){var p=o.shift();p?(m.find('input[name="variation_id"], input.variation_id').val(p.variation_id).change(),m.trigger("found_variation",[p])):(m.find(".variations select").val(""),g||m.trigger("reset_data"),b.alert(wc_add_to_cart_variation_params.i18n_no_matching_variations_text))}else m.trigger("update_variation_values",[o]),g||m.trigger("reset_data"),f||d.slideUp(200).trigger("hide_variation");k?"hidden"===n.css("visibility")&&n.css("visibility","visible").hide().fadeIn():n.css("visibility","hidden")}}).on("update_variation_values",function(b,d){i||(c.find(".variations select").each(function(b,c){var e,f=a(c);f.data("attribute_options")||f.data("attribute_options",f.find("option:gt(0)").get()),f.find("option:gt(0)").remove(),f.append(f.data("attribute_options")),f.find("option:gt(0)").removeClass("attached"),f.find("option:gt(0)").removeClass("enabled"),f.find("option:gt(0)").removeAttr("disabled"),e="undefined"!=typeof f.data("attribute_name")?f.data("attribute_name"):f.attr("name");for(var g in d)if("undefined"!=typeof d[g]){var h=d[g].attributes;for(var i in h)if(h.hasOwnProperty(i)){var j=h[i];if(i===e){var k="";d[g].variation_is_active&&(k="enabled"),j?(j=a("
").html(j).text(),j=j.replace(/'/g,"\\'"),j=j.replace(/"/g,'\\"'),f.find('option[value="'+j+'"]').addClass("attached "+k)):f.find("option:gt(0)").addClass("attached "+k)}}}f.find("option:gt(0):not(.attached)").remove(),f.find("option:gt(0):not(.enabled)").attr("disabled","disabled")}),c.trigger("woocommerce_update_variation_values"))}),c.trigger("wc_variation_form"),c};var e={find_matching_variations:function(a,b){for(var c=[],d=0;d1?(d.wc_set_variation_attr("src",a.image_src),d.wc_set_variation_attr("title",a.image_title),d.wc_set_variation_attr("alt",a.image_title),e.wc_set_variation_attr("href",a.image_link),e.wc_set_variation_attr("title",a.image_caption)):(d.wc_reset_variation_attr("src"),d.wc_reset_variation_attr("title"),d.wc_reset_variation_attr("alt"),e.wc_reset_variation_attr("href"),e.wc_reset_variation_attr("title"))},a(function(){"undefined"!=typeof wc_add_to_cart_variation_params&&a(".variations_form").each(function(){a(this).wc_variation_form().find(".variations select:eq(0)").change()})})}(jQuery,window,document); \ No newline at end of file +!function(a,b,c,d){a.fn.wc_variation_form=function(){var c=this,d=c.find(".single_variation"),f=c.closest(".product"),g=parseInt(c.data("product_id"),10),h=c.data("product_variations"),i=h===!1,j=!1,k=c.find(".reset_variations"),l=wp.template("variation-template"),m=wp.template("variation-add-to-cart-template"),n=wp.template("unavailable-variation-template"),o=c.find(".single_variation_wrap"),p=c.find(".variations_button");return c.unbind("check_variations update_variation_values found_variation"),c.find(".reset_variations").unbind("click"),c.find(".variations select").unbind("change focusin"),p.html(m({variation_id:0,min_qty:"",max_qty:""})),c.on("click",".reset_variations",function(){return c.find(".variations select").val("").change(),c.trigger("reset_data"),!1}).on("hide_variation",function(){return c.find(".single_add_to_cart_button").attr("disabled","disabled").attr("title",wc_add_to_cart_variation_params.i18n_make_a_selection_text),!1}).on("show_variation",function(a,b,d){return d?c.find(".single_add_to_cart_button").removeAttr("disabled").removeAttr("title"):c.find(".single_add_to_cart_button").attr("disabled","disabled").attr("title",wc_add_to_cart_variation_params.i18n_unavailable_text),!1}).on("reload_product_variations",function(){h=c.data("product_variations"),i=h===!1}).on("reset_data",function(){a(".sku").wc_reset_content(),a(".product_weight").wc_reset_content(),a(".product_dimensions").wc_reset_content(),c.trigger("reset_image"),d.slideUp(200).trigger("hide_variation")}).on("reset_image",function(){c.wc_variations_image_update(!1)}).on("change",".variations select",function(){if(c.find('input[name="variation_id"], input.variation_id').val("").change(),c.find(".wc-no-matching-variations").remove(),i){j&&j.abort();var b=!0,d=!1,e={};c.find(".variations select").each(function(){var c=a(this).data("attribute_name")||a(this).attr("name");0===a(this).val().length?b=!1:d=!0,e[c]=a(this).val()}),b?(e.product_id=g,j=a.ajax({url:wc_cart_fragments_params.wc_ajax_url.toString().replace("%%endpoint%%","get_variation"),type:"POST",data:e,success:function(a){a?c.trigger("found_variation",[a]):(c.trigger("reset_data"),c.find(".single_variation").after('

'+wc_add_to_cart_variation_params.i18n_no_matching_variations_text+"

"),c.find(".wc-no-matching-variations").slideDown(200))}})):c.trigger("reset_data"),d?"hidden"===k.css("visibility")&&k.css("visibility","visible").hide().fadeIn():k.css("visibility","hidden")}else c.trigger("woocommerce_variation_select_change"),c.trigger("check_variations",["",!1]),a(this).blur();a(".product.has-default-attributes > .images").fadeTo(200,1),c.trigger("woocommerce_variation_has_changed")}).on("focusin touchstart",".variations select",function(){a(this).find("option:selected").attr("selected","selected"),i||(c.trigger("woocommerce_variation_select_focusin"),c.trigger("check_variations",[a(this).data("attribute_name")||a(this).attr("name"),!0]))}).on("found_variation",function(a,b){var e=f.find(".product_meta").find(".sku"),g=f.find(".product_weight"),h=f.find(".product_dimensions"),i=!0;b.sku?e.wc_set_content(b.sku):e.wc_reset_content(),b.weight?g.wc_set_content(b.weight):g.wc_reset_content(),b.dimensions?h.wc_set_content(b.dimensions):h.wc_reset_content(),c.wc_variations_image_update(b),b.variation_is_visible?(d.html(l({price:b.price_html,availability:b.availability_html,description:b.variation_description})),p.html(m({variation_id:b.variation_id,min_qty:b.min_qty,max_qty:b.max_qty}))):(d.html(n),p.html(m({variation_id:0,min_qty:"",max_qty:""}))),"yes"===b.is_sold_individually?(o.find(".quantity input.qty").val("1"),o.find(".quantity").hide()):o.find(".quantity").show(),b.is_purchasable&&b.is_in_stock&&b.variation_is_visible||(i=!1),d.slideDown(200).trigger("show_variation",[b,i])}).on("check_variations",function(c,f,g){if(!i){var j=!0,k=!1,l={},m=a(this),n=m.find(".reset_variations");m.find(".variations select").each(function(){var b=a(this).data("attribute_name")||a(this).attr("name");0===a(this).val().length?j=!1:k=!0,f&&b===f?(j=!1,l[b]=""):l[b]=a(this).val()});var o=e.find_matching_variations(h,l);if(j){var p=o.shift();p?(m.find('input[name="variation_id"], input.variation_id').val(p.variation_id).change(),m.trigger("found_variation",[p])):(m.find(".variations select").val(""),g||m.trigger("reset_data"),b.alert(wc_add_to_cart_variation_params.i18n_no_matching_variations_text))}else m.trigger("update_variation_values",[o]),g||m.trigger("reset_data"),f||d.slideUp(200).trigger("hide_variation");k?"hidden"===n.css("visibility")&&n.css("visibility","visible").hide().fadeIn():n.css("visibility","hidden")}}).on("update_variation_values",function(b,d){i||(c.find(".variations select").each(function(b,c){var e,f=a(c);f.data("attribute_options")||f.data("attribute_options",f.find("option:gt(0)").get()),f.find("option:gt(0)").remove(),f.append(f.data("attribute_options")),f.find("option:gt(0)").removeClass("attached"),f.find("option:gt(0)").removeClass("enabled"),f.find("option:gt(0)").removeAttr("disabled"),e="undefined"!=typeof f.data("attribute_name")?f.data("attribute_name"):f.attr("name");for(var g in d)if("undefined"!=typeof d[g]){var h=d[g].attributes;for(var i in h)if(h.hasOwnProperty(i)){var j=h[i];if(i===e){var k="";d[g].variation_is_active&&(k="enabled"),j?(j=a("
").html(j).text(),j=j.replace(/'/g,"\\'"),j=j.replace(/"/g,'\\"'),f.find('option[value="'+j+'"]').addClass("attached "+k)):f.find("option:gt(0)").addClass("attached "+k)}}}f.find("option:gt(0):not(.attached)").remove(),f.find("option:gt(0):not(.enabled)").attr("disabled","disabled")}),c.trigger("woocommerce_update_variation_values"))}),c.trigger("wc_variation_form"),c};var e={find_matching_variations:function(a,b){for(var c=[],d=0;d1?(d.wc_set_variation_attr("src",a.image_src),d.wc_set_variation_attr("title",a.image_title),d.wc_set_variation_attr("alt",a.image_title),e.wc_set_variation_attr("href",a.image_link),e.wc_set_variation_attr("title",a.image_caption)):(d.wc_reset_variation_attr("src"),d.wc_reset_variation_attr("title"),d.wc_reset_variation_attr("alt"),e.wc_reset_variation_attr("href"),e.wc_reset_variation_attr("title"))},a(function(){"undefined"!=typeof wc_add_to_cart_variation_params&&a(".variations_form").each(function(){a(this).wc_variation_form().find(".variations select:eq(0)").change()})})}(jQuery,window,document); \ No newline at end of file