WooCommerce – Variable Product – Please choose product options…
Situation:
When you are using some custom template for single product (WooCommerce) and variation selection isn’t taking effect on click because your are using “do_action” with “woocommerce_before_add_to_cart_form” and “woocommerce_after_variations_form” maybe inside a shortcode? If yes, then WooCommerce is leaving your form as orphan and will not entertain your elements on trigger. Simply set variation id manually with a click.
jQuery(document).ready(function($){
function getPropertyCount(obj) {
var count = 0,
key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
count++;
}
}
return count;
}
$('.single-product').on('click', '.variations_form.cart .single_add_to_cart_button', function(event){
event.preventDefault();
if($('input[type="hidden"][name="variation_id"]').val()=='0'){
var variations = $('.variations_form.cart').data('product_variations');
$.each(variations, function(i, v){
var attribs = getPropertyCount(v.attributes);
var attrib_count = 0;
$.each(v.attributes, function(names, values){
names = names.replace('attribute_', '');
if($('.variations_form.cart table.variations #'+names).val()==values){
attrib_count++;
}
});
//document.title = attribs+' - '+attrib_count;
if(attribs==attrib_count){
$('input[type="hidden"][name="variation_id"]').val(v.variation_id);
$('.variations_form.cart').submit();
}
});
}
});
});
Last updated: October 29, 2017