Just wondering how to access the SKU's on a variable product's variations.
Ideally on the single product page, I want the SKU to change when the different variations are selected, like the price does.
Just wondering how to access the SKU's on a variable product's variations.
Ideally on the single product page, I want the SKU to change when the different variations are selected, like the price does.
I think this might be a bug.
In meta.php, the if statement includes "variable" so I guess it should be displaying...
if ( $product->is_type( array( 'simple', 'variable' ) )
Any idea of how to access an array of SKU's for a variable product?
Hey Sean,
This was a bug i raised to the WC developers via github.
It has now been fixed for ready for 1.7 ...
Do you want the fix to get it working on 1.6.5.2?
Many Thanks
Samuel
Yes, that would be great thanks Samuel.
I have a client quite eager to get this fixed before they go live.
Thanks,
Sean K.
Hi Samuel,
I would also be interested in the fix for this.
Thanks,
Jeff
Finally found this! I am having the same problem. Could you share the fix with us Samuel?
Thank you,
Alice
I need this fix please.
Why would they wait til 1.7?
Hi,
Sorry for such a late response.
here is the fix..
replace your add-to-cart-variation.js with the following code
jQuery(document).ready(function($) {
/**
* Variations form handling
*/
$('form.variations_form')
// On clicking the reset variation button
.on( 'click', '.reset_variations', function( event ) {
$(this).closest('form.variations_form').find('.variations select').val('').change();
return false;
} )
// Upon changing an option
.on( 'change', '.variations select', function( event ) {
$variation_form = $(this).closest('form.variations_form');
$variation_form.find('input[name=variation_id]').val('').change();
$variation_form
.trigger( 'woocommerce_variation_select_change' )
.trigger( 'check_variations', [ '', false ] );
$(this).blur();
if( $().uniform && $.isFunction( $.uniform.update ) ) {
$.uniform.update();
}
} )
// Upon gaining focus
.on( 'focusin', '.variations select', function( event ) {
$variation_form = $(this).closest('form.variations_form');
$variation_form
.trigger( 'woocommerce_variation_select_focusin' )
.trigger( 'check_variations', [ $(this).attr('name'), true ] );
} )
// Check variations
.on( 'check_variations', function( event, exclude, focus ) {
var all_set = true;
var any_set = false;
var showing_variation = false;
var current_settings = {};
var $variation_form = $(this);
var $reset_variations = $variation_form.find('.reset_variations');
$variation_form.find('.variations select').each( function() {
if ( $(this).val().length == 0 ) {
all_set = false;
} else {
any_set = true;
}
if ( exclude && $(this).attr('name') == exclude ) {
all_set = false;
current_settings[$(this).attr('name')] = '';
} else {
// Encode entities
value = $(this).val()
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
// Add to settings array
current_settings[ $(this).attr('name') ] = value;
}
});
var product_id = parseInt( $variation_form.attr( 'data-product_id' ) );
var all_variations = window[ "product_variations_" + product_id ];
// Fallback
if ( ! all_variations )
all_variations = window[ "product_variations" ];
var matching_variations = find_matching_variations( all_variations, current_settings );
if ( all_set ) {
var variation = matching_variations.pop();
if ( variation ) {
// Found - set ID
$variation_form
.find('input[name=variation_id]')
.val( variation.variation_id )
.change();
$variation_form.trigger( 'found_variation', [ variation ] );
} else {
// Nothing found - reset fields
$variation_form.find('.variations select').val('');
if ( ! focus )
$variation_form.trigger( 'reset_image' );
}
} else {
$variation_form.trigger( 'update_variation_values', [ matching_variations ] );
if ( ! focus )
$variation_form.trigger( 'reset_image' );
if ( ! exclude ) {
$variation_form.find('.single_variation_wrap').slideUp('200');
}
}
if ( any_set ) {
if ( $reset_variations.css('visibility') == 'hidden' )
$reset_variations.css('visibility','visible').hide().fadeIn();
} else {
$reset_variations.css('visibility','hidden');
}
} )
// Reset product image
.on( 'reset_image', function( event ) {
var $product = $(this).closest( '.product' );
var $product_img = $product.find( 'div.images img:eq(0)' );
var $product_link = $product.find( 'div.images a.zoom:eq(0)' );
var o_src = $product_img.attr('data-o_src');
var o_title = $product_img.attr('data-o_title');
var o_href = $product_link.attr('data-o_href');
if ( o_src && o_href && o_title ) {
$product_img
.attr( 'src', o_src )
.attr( 'alt', o_title )
.attr( 'title', o_title );
$product_link
.attr( 'href', o_href );
}
} )
// Disable option fields that are unavaiable for current set of attributes
.on( 'update_variation_values', function( event, variations ) {
$variation_form = $(this).closest('form.variations_form');
// Loop through selects and disable/enable options based on selections
$variation_form.find('.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
$variation_form.trigger('woocommerce_update_variation_values');
} )
// Show single variation details (price, stock, image)
.on( 'found_variation', function( event, variation ) {
var $variation_form = $(this);
var $product = $(this).closest( '.product' );
var $product_img = $product.find( 'div.images img:eq(0)' );
var $product_link = $product.find( 'div.images a.zoom:eq(0)' );
var o_src = $product_img.attr('data-o_src');
var o_title = $product_img.attr('data-o_title');
var o_href = $product_link.attr('data-o_href');
var variation_image = variation.image_src;
var variation_link = variation.image_link;
var variation_title = variation.image_title;
$variation_form.find('.variations_button').show();
$variation_form.find('.single_variation').html( variation.price_html + variation.availability_html );
if ( ! o_src ) {
o_src = ( ! $product_img.attr('src') ) ? '' : $product_img.attr('src');
$product_img.attr('data-o_src', o_src );
}
if ( ! o_href ) {
o_href = ( ! $product_link.attr('href') ) ? '' : $product_link.attr('href');
$product_link.attr('data-o_href', o_href );
}
if ( ! o_title ) {
o_title = ( ! $product_img.attr('title') ) ? '' : $product_img.attr('title');
$product_img.attr('data-o_title', o_title );
}
if ( variation_image && variation_image.length > 1 ) {
$product_img
.attr( 'src', variation_image )
.attr( 'alt', variation_title )
.attr( 'title', variation_title );
$product_link
.attr( 'href', variation_link );
} else {
$product_img
.attr( 'src', o_src )
.attr( 'alt', o_title )
.attr( 'title', o_title );
$product_link
.attr( 'href', o_href );
}
var $single_variation_wrap = $variation_form.find('.single_variation_wrap');
if ( variation.sku )
$product.find('.product_meta').find('.sku').text( variation.sku );
else
$product.find('.product_meta').find('.sku').text('');
$single_variation_wrap.find('.quantity').show();
if ( ! variation.is_in_stock && ! variation.backorders_allowed ) {
$variation_form.find('.variations_button').hide();
}
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();
}
$single_variation_wrap.slideDown('200').trigger( 'show_variation', [ variation ] );
} );
/**
* Initial states and loading
*/
$('form.variations_form .variations select').change();
/**
* Helper functions for variations
*/
// Search for matching variations for given set of attributes
function find_matching_variations( product_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;
}
// Check if two arrays of attributes match
function variations_match( attrs1, attrs2 ) {
var match = true;
for ( attr_name in attrs1 ) {
var val1 = attrs1[ attr_name ];
var val2 = attrs2[ attr_name ];
if ( val1 !== undefined && val2 !== undefined && val1.length != 0 && val2.length != 0 && val1 != val2 ) {
match = false;
}
}
return match;
}
});
and also your add-to-cart-variation-min.js with the following:
jQuery(document).ready(function(e){function t(e,t){var r=[];for(var i=0;i<e.length;i++){var s=e[i],o=s.variation_id;n(s.attributes,t)&&r.push(s)}return r}function n(e,t){var n=!0;for(attr_name in e){var r=e[attr_name],i=t[attr_name];r!==undefined&&i!==undefined&&r.length!=0&&i.length!=0&&r!=i&&(n=!1)}return n}e("form.variations_form").on("click",".reset_variations",function(t){e(this).closest("form.variations_form").find(".variations select").val("").change();return!1}).on("change",".variations select",function(t){$variation_form=e(this).closest("form.variations_form");$variation_form.find("input[name=variation_id]").val("").change();$variation_form.trigger("woocommerce_variation_select_change").trigger("check_variations",["",!1]);e(this).blur();e().uniform&&e.isFunction(e.uniform.update)&&e.uniform.update()}).on("focusin",".variations select",function(t){$variation_form=e(this).closest("form.variations_form");$variation_form.trigger("woocommerce_variation_select_focusin").trigger("check_variations",[e(this).attr("name"),!0])}).on("check_variations",function(n,r,i){var s=!0,o=!1,u=!1,a={},f=e(this),l=f.find(".reset_variations");f.find(".variations select").each(function(){e(this).val().length==0?s=!1:o=!0;if(r&&e(this).attr("name")==r){s=!1;a[e(this).attr("name")]=""}else{value=e(this).val().replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">");a[e(this).attr("name")]=value}});var c=parseInt(f.attr("data-product_id")),h=window["product_variations_"+c];h||(h=window.product_variations);var p=t(h,a);if(s){var d=p.pop();if(d){f.find("input[name=variation_id]").val(d.variation_id).change();f.trigger("found_variation",[d])}else{f.find(".variations select").val("");i||f.trigger("reset_image")}}else{f.trigger("update_variation_values",[p]);i||f.trigger("reset_image");r||f.find(".single_variation_wrap").slideUp("200")}o?l.css("visibility")=="hidden"&&l.css("visibility","visible").hide().fadeIn():l.css("visibility","hidden")}).on("reset_image",function(t){var n=e(this).closest(".product"),r=n.find("div.images img:eq(0)"),i=n.find("div.images a.zoom:eq(0)"),s=r.attr("data-o_src"),o=r.attr("data-o_title"),u=i.attr("data-o_href");if(s&&u&&o){r.attr("src",s).attr("alt",o).attr("title",o);i.attr("href",u)}}).on("update_variation_values",function(t,n){$variation_form=e(this).closest("form.variations_form");$variation_form.find(".variations select").each(function(t,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 n){var s=n[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")}}});$variation_form.trigger("woocommerce_update_variation_values")}).on("found_variation",function(t,n){var r=e(this),i=e(this).closest(".product"),s=i.find("div.images img:eq(0)"),o=i.find("div.images a.zoom:eq(0)"),u=s.attr("data-o_src"),a=s.attr("data-o_title"),f=o.attr("data-o_href"),l=n.image_src,c=n.image_link,h=n.image_title;r.find(".variations_button").show();r.find(".single_variation").html(n.price_html+n.availability_html);if(!u){u=s.attr("src")?s.attr("src"):"";s.attr("data-o_src",u)}if(!f){f=o.attr("href")?o.attr("href"):"";o.attr("data-o_href",f)}if(!a){a=s.attr("title")?s.attr("title"):"";s.attr("data-o_title",a)}if(l&&l.length>1){s.attr("src",l).attr("alt",h).attr("title",h);o.attr("href",c)}else{s.attr("src",u).attr("alt",a).attr("title",a);o.attr("href",f)}var p=r.find(".single_variation_wrap");n.sku?i.find(".product_meta").find(".sku").text(n.sku):i.find(".product_meta").find(".sku").text("");p.find(".quantity").show();!n.is_in_stock&&!n.backorders_allowed&&r.find(".variations_button").hide();n.min_qty?p.find("input[name=quantity]").attr("data-min",n.min_qty).val(n.min_qty):p.find("input[name=quantity]").removeAttr("data-min");n.max_qty?p.find("input[name=quantity]").attr("data-max",n.max_qty):p.find("input[name=quantity]").removeAttr("data-max");if(n.is_sold_individually=="yes"){p.find("input[name=quantity]").val("1");p.find(".quantity").hide()}p.slideDown("200").trigger("show_variation",[n])});e("form.variations_form .variations select").change()});Dear samueljeden,
It's nice to see solution, but this code seems to have syntax error.
I have fix it (see below).
I'm using Woocommerce 1.6.6 and still sku for variable product isn't visible.
add-to-cart-variation-min.js (fixed)
jQuery(document).ready(function(e){function t(e,t){var r=[];for(var i=0;i<e.length;i++){var s=e[i],o=s.variation_id;n(s.attributes,t)&&r.push(s)}return r}function n(e,t){var n=!0;for(attr_name in e){var r=e[attr_name],i=t[attr_name];r!==undefined&&i!==undefined&&r.length!=0&&i.length!=0&&r!=i&&(n=!1)}return n}e("form.variations_form").on("click",".reset_variations",function(t){e(this).closest("form.variations_form").find(".variations select").val("").change();return!1}).on("change",".variations select",function(t){$variation_form=e(this).closest("form.variations_form");$variation_form.find("input[name=variation_id]").val("").change();$variation_form.trigger("woocommerce_variation_select_change").trigger("check_variations",["",!1]);e(this).blur();e().uniform&&e.isFunction(e.uniform.update)&&e.uniform.update()}).on("focusin",".variations select",function(t){$variation_form=e(this).closest("form.variations_form");$variation_form.trigger("woocommerce_variation_select_focusin").trigger("check_variations",[e(this).attr("name"),!0])}).on("check_variations",function(n,r,i){var s=!0,o=!1,u=!1,a={},f=e(this),l=f.find(".reset_variations");f.find(".variations select").each(function(){e(this).val().length==0?s=!1:o=!0;if(r&&e(this).attr("name")==r){s=!1;a[e(this).attr("name")]=""}else{value=e(this).val().replace(/&/g,"&").replace(/"/g,'"').replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">");a[e(this).attr("name")]=value}});var c=parseInt(f.attr("data-product_id")),h=window["product_variations_"+c];h||(h=window.product_variations);var p=t(h,a);if(s){var d=p.pop();if(d){f.find("input[name=variation_id]").val(d.variation_id).change();f.trigger("found_variation",[d])}else{f.find(".variations select").val("");i||f.trigger("reset_image")}}else{f.trigger("update_variation_values",[p]);i||f.trigger("reset_image");r||f.find(".single_variation_wrap").slideUp("200")}o?l.css("visibility")=="hidden"&&l.css("visibility","visible").hide().fadeIn():l.css("visibility","hidden")}).on("reset_image",function(t){var n=e(this).closest(".product"),r=n.find("div.images img:eq(0)"),i=n.find("div.images a.zoom:eq(0)"),s=r.attr("data-o_src"),o=r.attr("data-o_title"),u=i.attr("data-o_href");if(s&&u&&o){r.attr("src",s).attr("alt",o).attr("title",o);i.attr("href",u)}}).on("update_variation_values",function(t,n){$variation_form=e(this).closest("form.variations_form");$variation_form.find(".variations select").each(function(t,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 n){var s=n[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")}}});$variation_form.trigger("woocommerce_update_variation_values")}).on("found_variation",function(t,n){var r=e(this),i=e(this).closest(".product"),s=i.find("div.images img:eq(0)"),o=i.find("div.images a.zoom:eq(0)"),u=s.attr("data-o_src"),a=s.attr("data-o_title"),f=o.attr("data-o_href"),l=n.image_src,c=n.image_link,h=n.image_title;r.find(".variations_button").show();r.find(".single_variation").html(n.price_html+n.availability_html);if(!u){u=s.attr("src")?s.attr("src"):"";s.attr("data-o_src",u)}if(!f){f=o.attr("href")?o.attr("href"):"";o.attr("data-o_href",f)}if(!a){a=s.attr("title")?s.attr("title"):"";s.attr("data-o_title",a)}if(l&&l.length>1){s.attr("src",l).attr("alt",h).attr("title",h);o.attr("href",c)}else{s.attr("src",u).attr("alt",a).attr("title",a);o.attr("href",f)}var p=r.find(".single_variation_wrap");n.sku?i.find(".product_meta").find(".sku").text(n.sku):i.find(".product_meta").find(".sku").text("");p.find(".quantity").show();!n.is_in_stock&&!n.backorders_allowed&&r.find(".variations_button").hide();n.min_qty?p.find("input[name=quantity]").attr("data-min",n.min_qty).val(n.min_qty):p.find("input[name=quantity]").removeAttr("data-min");n.max_qty?p.find("input[name=quantity]").attr("data-max",n.max_qty):p.find("input[name=quantity]").removeAttr("data-max");if(n.is_sold_individually=="yes"){p.find("input[name=quantity]").val("1");p.find(".quantity").hide()}p.slideDown("200").trigger("show_variation",[n])});e("form.variations_form .variations select").change()});Thank you for the code but same with lorsabyan, the SKU is still not appearing when choosing the variables. I have left the main SKU blank as stated in the docs and added a different SKU for each variable but it is still blank on the front end. :(
My apologies for the syntax error.
You are required to have a parent sku for it to show.
Many Thanks
Hey Guys,
UPdate, just done a fresh fresh install of just WP 3.5, WC 1.6.6 and replaced the two files.
and it works, check here guys
http://fresh2.samueleden.org.uk/shop/this-is-a-test-product/
Awesome, works great! You're a life-safer! Been trying to find this fix for the past few days. Thank you for sharing and have a Happy Holidays!
kudos to samueljeden and lorsabyan !!
tried and it works!
Happy holidays!
Thanks for the fix guys - works great!
Would it be possible to automatically build a sku based on the variations currently selected? Using the parent sku as the base for the variation sku, could the description or slug of a variation be used to generate the complete variation sku?
Example: Parent sku on a product is ABC100
Variation 1 Option 1, Variation 2 Option 1 sku: ABC100-A-A
Variation 1 Option 2, Variation 2 Option 1 sku: ABC100-B-A
Variation 1 Option 1, Variation 2 Option 2 sku: ABC100-A-B
Variation 1 Option 2, Variation 2 Option 2 sku: ABC100-B-B
I realize you can manually enter the completed sku if the number of variations and options for each variation is low. The site I am working on has around 100 products, and a lot of the products have up to three variations available for configuring. Some of the variations have 8-12 different options. This means some products have >1000 possible sku's, and it would be a pain to manually enter each sku individually on every product. Also, this slows the page down a lot.
Here's a page from the site I'm working on. The sku's are not entered for each variation because it would be 1764 variations on this one product alone.
If this can be done automatically, it would save a lot of hassle/page load time.
Thanks guys!
Jeff
I am still learning how to do all this, pretty much "self taught" - I say that because I'm going to ask, what is probably a very simple question, but if someone would PLEASE just steer me in the right direction - that'd be so appreciated!
I'm using WP 3.5 and Woocommerce 1.6.6 (with Catalyst Theme and Dynamik) - Do I go to Appearance>Editor to insert the add-to-cart-variation.js and the add-to-cart-variation-min.js . . .?
I am just not sure where I am supposed to place this code, please help?
THANKS,
Allison
Hi Allison,
these need to replace the files in the template files for woocommerce... you would have to use a ftp client to gain access to the files, its not something that can be done through the wordpress admin unfortunately.
I have ftp access to the files, so do I go in and find the files, add-to-cart-variation.js and the add-to-cart-variation-min.js and then replace the code?
yes, 100% correct.
:)
Thanks so much samueljeden, but when I went in and did a search for those filenames, no dice.
I'm just about ready to just make an entry for each and every product and forget the variations - but would LOVE to make it work.
Hi Allison ,
they are in your ftp in the wp-content/plugins/woocommerce/assets/js/frontend
directory :)
Yep, I'm having the same problem. I updated the files. Here's a sample single product page:
Bracket
Probably just something simple I've missed.
Hi ryan,
It looks like your file has broken (your js file)
Please copy lorsa's file into both files
Dreamweaver's saying there is an error in add-to-cart-variation.js on line 73...
Lorsa's fixed it...
Just copy lorsa's min file into both the normal and min files... Should fix it
Well, I replaced both files. There has to be something wrong with how I set up the variations in the backend.
Expand your variables and screenshot please
If you want to take a look, just use:
user
wordpress
to login. I can't figure it out...
You must log in to post.