2012-07-17 14:09:18 +00:00
|
|
|
jQuery(document).ready(function($) {
|
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
/**
|
|
|
|
* Variations form handling
|
|
|
|
*/
|
|
|
|
$('form.variations_form')
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// On clicking the reset variation button
|
|
|
|
.on( 'click', '.reset_variations', function( event ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
$(this).closest('form.variations_form').find('.variations select').val('').change();
|
2012-10-08 12:54:31 +00:00
|
|
|
|
|
|
|
var $sku = $(this).closest('.product').find('.sku');
|
|
|
|
|
|
|
|
if ( $sku.attr( 'data-o_sku' ) )
|
|
|
|
$sku.text( $sku.attr( 'data-o_sku' ) );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
return false;
|
|
|
|
} )
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// Upon changing an option
|
|
|
|
.on( 'change', '.variations select', function( event ) {
|
2012-10-05 18:02:02 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
$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 ] );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
} )
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// 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');
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
$variation_form.find('.variations select').each( function() {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-22 06:51:19 +00:00
|
|
|
if ( $(this).val().length == 0 ) {
|
|
|
|
all_set = false;
|
|
|
|
} else {
|
|
|
|
any_set = true;
|
|
|
|
}
|
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
if ( exclude && $(this).attr('name') == exclude ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-25 15:00:40 +00:00
|
|
|
var product_id = parseInt( $variation_form.attr( 'data-product_id' ) );
|
2012-10-12 13:21:21 +00:00
|
|
|
var all_variations = window[ "product_variations" ][ product_id ];
|
2012-08-25 09:11:23 +00:00
|
|
|
|
|
|
|
// Fallback
|
|
|
|
if ( ! all_variations )
|
|
|
|
all_variations = window[ "product_variations" ];
|
|
|
|
|
|
|
|
var matching_variations = find_matching_variations( all_variations, current_settings );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
if ( all_set ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
var variation = matching_variations.pop();
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
if ( variation ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// Found - set ID
|
|
|
|
$variation_form
|
|
|
|
.find('input[name=variation_id]')
|
|
|
|
.val( variation.variation_id )
|
|
|
|
.change();
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
$variation_form.trigger( 'found_variation', [ variation ] );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Nothing found - reset fields
|
|
|
|
$variation_form.find('.variations select').val('');
|
|
|
|
|
|
|
|
if ( ! focus )
|
|
|
|
$variation_form.trigger( 'reset_image' );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$variation_form.trigger( 'update_variation_values', [ matching_variations ] );
|
|
|
|
|
|
|
|
if ( ! focus )
|
|
|
|
$variation_form.trigger( 'reset_image' );
|
|
|
|
|
2012-08-22 06:51:19 +00:00
|
|
|
if ( ! exclude ) {
|
|
|
|
$variation_form.find('.single_variation_wrap').slideUp('200');
|
|
|
|
}
|
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
if ( any_set ) {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
if ( $reset_variations.css('visibility') == 'hidden' )
|
|
|
|
$reset_variations.css('visibility','visible').hide().fadeIn();
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
} else {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
$reset_variations.css('visibility','hidden');
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
} )
|
|
|
|
|
|
|
|
// 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
|
2012-09-25 12:22:22 +00:00
|
|
|
.attr( 'href', o_href )
|
|
|
|
.attr( 'title', o_title );
|
2012-08-21 15:41:18 +00:00
|
|
|
}
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
} )
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// Disable option fields that are unavaiable for current set of attributes
|
|
|
|
.on( 'update_variation_values', function( event, variations ) {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
$variation_form = $(this).closest('form.variations_form');
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// Loop through selects and disable/enable options based on selections
|
|
|
|
$variation_form.find('.variations select').each(function( index, el ){
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
current_attr_select = $(el);
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// Disable all
|
|
|
|
current_attr_select.find('option:gt(0)').attr('disabled', 'disabled');
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// Get name
|
|
|
|
var current_attr_name = current_attr_select.attr('name');
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// Loop through variations
|
|
|
|
for ( num in variations ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
var attributes = variations[ num ].attributes;
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
for ( attr_name in attributes ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
var attr_val = attributes[ attr_name ];
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
if ( attr_name == current_attr_name ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
if ( attr_val ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// Decode entities
|
|
|
|
attr_val = $("<div/>").html( attr_val ).text();
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// Add slashes
|
|
|
|
attr_val = attr_val.replace(/'/g, "\\'");
|
|
|
|
attr_val = attr_val.replace(/"/g, "\\\"");
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// Compare the meercat
|
|
|
|
current_attr_select.find('option[value="' + attr_val + '"]').removeAttr('disabled');
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
} else {
|
|
|
|
current_attr_select.find('option').removeAttr('disabled');
|
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
});
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
// Custom event for when variations have been updated
|
2012-08-25 15:00:40 +00:00
|
|
|
$variation_form.trigger('woocommerce_update_variation_values');
|
2012-08-21 15:41:18 +00:00
|
|
|
|
|
|
|
} )
|
|
|
|
|
|
|
|
// Show single variation details (price, stock, image)
|
|
|
|
.on( 'found_variation', function( event, variation ) {
|
|
|
|
var $variation_form = $(this);
|
2012-08-24 20:46:32 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
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)' );
|
2012-08-24 20:46:32 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
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');
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
var variation_image = variation.image_src;
|
|
|
|
var variation_link = variation.image_link;
|
|
|
|
var variation_title = variation.image_title;
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
$variation_form.find('.variations_button').show();
|
|
|
|
$variation_form.find('.single_variation').html( variation.price_html + variation.availability_html );
|
|
|
|
|
2012-08-24 20:46:32 +00:00
|
|
|
if ( ! o_src ) {
|
|
|
|
o_src = ( ! $product_img.attr('src') ) ? '' : $product_img.attr('src');
|
|
|
|
$product_img.attr('data-o_src', o_src );
|
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2012-08-24 20:46:32 +00:00
|
|
|
if ( ! o_href ) {
|
|
|
|
o_href = ( ! $product_link.attr('href') ) ? '' : $product_link.attr('href');
|
|
|
|
$product_link.attr('data-o_href', o_href );
|
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2012-08-24 20:46:32 +00:00
|
|
|
if ( ! o_title ) {
|
|
|
|
o_title = ( ! $product_img.attr('title') ) ? '' : $product_img.attr('title');
|
|
|
|
$product_img.attr('data-o_title', o_title );
|
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
|
|
|
if ( variation_image && variation_image.length > 1 ) {
|
|
|
|
$product_img
|
2012-08-24 20:46:32 +00:00
|
|
|
.attr( 'src', variation_image )
|
|
|
|
.attr( 'alt', variation_title )
|
|
|
|
.attr( 'title', variation_title );
|
2012-08-21 15:41:18 +00:00
|
|
|
$product_link
|
2012-09-25 12:22:22 +00:00
|
|
|
.attr( 'href', variation_link )
|
|
|
|
.attr( 'title', variation_title );
|
2012-08-21 15:41:18 +00:00
|
|
|
} else {
|
|
|
|
$product_img
|
2012-08-24 20:46:32 +00:00
|
|
|
.attr( 'src', o_src )
|
|
|
|
.attr( 'alt', o_title )
|
|
|
|
.attr( 'title', o_title );
|
2012-08-21 15:41:18 +00:00
|
|
|
$product_link
|
2012-09-25 12:22:22 +00:00
|
|
|
.attr( 'href', o_href )
|
|
|
|
.attr( 'title', o_title );
|
2012-08-21 15:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var $single_variation_wrap = $variation_form.find('.single_variation_wrap');
|
2012-10-08 12:54:31 +00:00
|
|
|
|
|
|
|
var $sku = $product.find('.product_meta').find('.sku');
|
|
|
|
|
|
|
|
if ( ! $sku.attr( 'data-o_sku' ) )
|
|
|
|
$sku.attr( 'data-o_sku', $sku.text() );
|
|
|
|
|
|
|
|
if ( variation.sku ) {
|
|
|
|
$sku.text( variation.sku );
|
|
|
|
} else {
|
|
|
|
$sku.text( $sku.attr( 'data-o_sku' ) );
|
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
|
|
|
$single_variation_wrap.find('.quantity').show();
|
2012-08-25 10:02:02 +00:00
|
|
|
|
|
|
|
if ( ! variation.is_in_stock && ! variation.backorders_allowed ) {
|
|
|
|
$variation_form.find('.variations_button').hide();
|
|
|
|
}
|
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
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();
|
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-25 08:51:20 +00:00
|
|
|
$single_variation_wrap.slideDown('200').trigger( 'show_variation', [ variation ] );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
} );
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2012-08-21 15:41:18 +00:00
|
|
|
/**
|
|
|
|
* Initial states and loading
|
|
|
|
*/
|
2012-08-22 15:36:08 +00:00
|
|
|
$('form.variations_form .variations select').change();
|
2012-08-21 15:41:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2012-08-23 06:11:30 +00:00
|
|
|
for ( attr_name in attrs1 ) {
|
|
|
|
var val1 = attrs1[ attr_name ];
|
|
|
|
var val2 = attrs2[ attr_name ];
|
2012-08-21 15:41:18 +00:00
|
|
|
if ( val1 !== undefined && val2 !== undefined && val1.length != 0 && val2.length != 0 && val1 != val2 ) {
|
|
|
|
match = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return match;
|
|
|
|
}
|
2012-07-17 14:09:18 +00:00
|
|
|
|
|
|
|
});
|