1 line
6.3 KiB
JavaScript
1 line
6.3 KiB
JavaScript
(function(e,t,n,r){e.fn.wc_variation_form=function(){e.fn.wc_variation_form.find_matching_variations=function(t,n){var r=[];for(var i=0;i<t.length;i++){var s=t[i];var o=s.variation_id;if(e.fn.wc_variation_form.variations_match(s.attributes,n)){r.push(s)}}return r};e.fn.wc_variation_form.variations_match=function(e,t){var n=true;for(var i in e){if(e.hasOwnProperty(i)){var s=e[i];var o=t[i];if(s!==r&&o!==r&&s.length!==0&&o.length!==0&&s!==o){n=false}}}return n};this.unbind("check_variations update_variation_values found_variation");this.find(".reset_variations").unbind("click");this.find(".variations select").unbind("change focusin");$form=this.on("click",".reset_variations",function(t){e(this).closest(".variations_form").find(".variations select").val("").change();var n=e(this).closest(".product").find(".sku"),r=e(this).closest(".product").find(".product_weight"),i=e(this).closest(".product").find(".product_dimensions");if(n.attr("data-o_sku"))n.text(n.attr("data-o_sku"));if(r.attr("data-o_weight"))r.text(r.attr("data-o_weight"));if(i.attr("data-o_dimensions"))i.text(i.attr("data-o_dimensions"));return false}).on("change",".variations select",function(t){$variation_form=e(this).closest(".variations_form");$variation_form.find("input[name=variation_id]").val("").change();$variation_form.trigger("woocommerce_variation_select_change").trigger("check_variations",["",false]);e(this).blur();if(e().uniform&&e.isFunction(e.uniform.update)){e.uniform.update()}}).on("focusin touchstart",".variations select",function(t){$variation_form=e(this).closest(".variations_form");$variation_form.trigger("woocommerce_variation_select_focusin").trigger("check_variations",[e(this).attr("name"),true])}).on("check_variations",function(n,r,i){var s=true,o=false,u=false,a={},f=e(this),l=f.find(".reset_variations");f.find(".variations select").each(function(){if(e(this).val().length===0){s=false}else{o=true}if(r&&e(this).attr("name")===r){s=false;a[e(this).attr("name")]=""}else{value=e(this).val();a[e(this).attr("name")]=value}});var c=parseInt(f.data("product_id")),h=f.data("product_variations");if(!h)h=t.product_variations.product_id;if(!h)h=t.product_variations;if(!h)h=t["product_variations_"+c];var p=e.fn.wc_variation_form.find_matching_variations(h,a);if(s){var d=p.shift();if(d){f.find("input[name=variation_id]").val(d.variation_id).change();f.trigger("found_variation",[d])}else{f.find(".variations select").val("");if(!i)f.trigger("reset_image");alert(wc_add_to_cart_variation_params.i18n_no_matching_variations_text)}}else{f.trigger("update_variation_values",[p]);if(!i)f.trigger("reset_image");if(!r){f.find(".single_variation_wrap").slideUp(200)}}if(o){if(l.css("visibility")==="hidden")l.css("visibility","visible").hide().fadeIn()}else{l.css("visibility","hidden")}}).on("reset_image",function(t){var n=e(this).closest(".product"),i=n.find("div.images img:eq(0)"),s=n.find("div.images a.zoom:eq(0)"),o=i.attr("data-o_src"),u=i.attr("data-o_title"),a=i.attr("data-o_alt"),f=s.attr("data-o_href");if(o!==r){i.attr("src",o)}if(f!==r){s.attr("href",f)}if(u!==r){i.attr("title",u);s.attr("title",u)}if(a!==r){i.attr("alt",a)}}).on("update_variation_values",function(t,n){$variation_form=e(this).closest(".variations_form");$variation_form.find(".variations select").each(function(t,r){current_attr_select=e(r);if(!current_attr_select.data("attribute_options"))current_attr_select.data("attribute_options",current_attr_select.find("option:gt(0)").get());current_attr_select.find("option:gt(0)").remove();current_attr_select.append(current_attr_select.data("attribute_options"));current_attr_select.find("option:gt(0)").removeClass("active");var i=current_attr_select.attr("name");for(var s in n){if(typeof n[s]!="undefined"){var o=n[s].attributes;for(var u in o){if(o.hasOwnProperty(u)){var a=o[u];if(u==i){if(a){a=e("<div/>").html(a).text();a=a.replace(/'/g,"\\'");a=a.replace(/"/g,'\\"');current_attr_select.find('option[value="'+a+'"]').addClass("active")}else{current_attr_select.find("option:gt(0)").addClass("active")}}}}}}current_attr_select.find("option:gt(0):not(.active)").remove()});$variation_form.trigger("woocommerce_update_variation_values")}).on("found_variation",function(t,n){var i=e(this),s=e(this).closest(".product"),o=s.find("div.images img:eq(0)"),u=s.find("div.images a.zoom:eq(0)"),a=o.attr("data-o_src"),f=o.attr("data-o_title"),l=o.attr("data-o_alt"),c=u.attr("data-o_href"),h=n.image_src,p=n.image_link,d=n.image_title,v=n.image_alt;i.find(".variations_button").show();i.find(".single_variation").html(n.price_html+n.availability_html);if(a===r){a=!o.attr("src")?"":o.attr("src");o.attr("data-o_src",a)}if(c===r){c=!u.attr("href")?"":u.attr("href");u.attr("data-o_href",c)}if(f===r){f=!o.attr("title")?"":o.attr("title");o.attr("data-o_title",f)}if(l===r){l=!o.attr("alt")?"":o.attr("alt");o.attr("data-o_alt",l)}if(h&&h.length>1){o.attr("src",h).attr("alt",v).attr("title",d);u.attr("href",p).attr("title",d)}else{o.attr("src",a).attr("alt",l).attr("title",f);u.attr("href",c).attr("title",f)}var m=i.find(".single_variation_wrap"),g=s.find(".product_meta").find(".sku"),y=s.find(".product_weight"),b=s.find(".product_dimensions");if(!g.attr("data-o_sku"))g.attr("data-o_sku",g.text());if(!y.attr("data-o_weight"))y.attr("data-o_weight",y.text());if(!b.attr("data-o_dimensions"))b.attr("data-o_dimensions",b.text());if(n.sku){g.text(n.sku)}else{g.text(g.attr("data-o_sku"))}if(n.weight){y.text(n.weight)}else{y.text(y.attr("data-o_weight"))}if(n.dimensions){b.text(n.dimensions)}else{b.text(b.attr("data-o_dimensions"))}m.find(".quantity").show();if(!n.is_in_stock&&!n.backorders_allowed){i.find(".variations_button").hide()}if(!n.variation_is_visible){i.find(".variations_button").hide();i.find(".single_variation").html("<p>"+wc_add_to_cart_variation_params.i18n_unavailable_text+"</p>")}if(n.min_qty)m.find("input[name=quantity]").attr("min",n.min_qty).val(n.min_qty);else m.find("input[name=quantity]").removeAttr("min");if(n.max_qty)m.find("input[name=quantity]").attr("max",n.max_qty);else m.find("input[name=quantity]").removeAttr("max");if(n.is_sold_individually==="yes"){m.find("input[name=quantity]").val("1");m.find(".quantity").hide()}m.slideDown(200).trigger("show_variation",[n])});$form.trigger("wc_variation_form");return $form};e(function(){if(typeof wc_add_to_cart_variation_params==="undefined")return false;e(".variations_form").wc_variation_form();e(".variations_form .variations select").change()})})(jQuery,window,document) |