2013-02-07 10:12:08 +00:00
|
|
|
/*!
|
|
|
|
* Variations Plugin
|
|
|
|
*/
|
|
|
|
;(function ( $, window, document, undefined ) {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
$.fn.wc_variation_form = function () {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
$.fn.wc_variation_form.find_matching_variations = function( product_variations, settings ) {
|
|
|
|
var matching = [];
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
for ( var i = 0; i < product_variations.length; i++ ) {
|
2014-02-22 04:08:08 +00:00
|
|
|
var variation = product_variations[i];
|
|
|
|
var variation_id = variation.variation_id;
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2013-02-07 10:12:08 +00:00
|
|
|
if ( $.fn.wc_variation_form.variations_match( variation.attributes, settings ) ) {
|
2014-03-17 05:19:58 +00:00
|
|
|
matching.push( variation );
|
2014-02-22 04:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-17 05:19:58 +00:00
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
return matching;
|
|
|
|
};
|
|
|
|
|
|
|
|
$.fn.wc_variation_form.variations_match = function( attrs1, attrs2 ) {
|
|
|
|
var match = true;
|
|
|
|
|
|
|
|
for ( var attr_name in attrs1 ) {
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( attrs1.hasOwnProperty( attr_name ) ) {
|
|
|
|
var val1 = attrs1[ attr_name ];
|
|
|
|
var val2 = attrs2[ attr_name ];
|
2014-02-22 04:08:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( val1 !== undefined && val2 !== undefined && val1.length !== 0 && val2.length !== 0 && val1 !== val2 ) {
|
|
|
|
match = false;
|
|
|
|
}
|
2014-02-22 04:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-17 05:19:58 +00:00
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
return match;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Unbind any existing events
|
|
|
|
this.unbind( 'check_variations update_variation_values found_variation' );
|
|
|
|
this.find( '.reset_variations' ).unbind( 'click' );
|
|
|
|
this.find( '.variations select' ).unbind( 'change focusin' );
|
|
|
|
|
|
|
|
// Bind events
|
|
|
|
$form = this
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// On clicking the reset variation button
|
|
|
|
.on( 'click', '.reset_variations', function( event ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
$( this ).closest( '.variations_form' ).find( '.variations select' ).val( '' ).change();
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
var $sku = $( this ).closest( '.product' ).find( '.sku' ),
|
|
|
|
$weight = $( this ).closest( '.product' ).find( '.product_weight' ),
|
|
|
|
$dimensions = $( this ).closest( '.product' ).find( '.product_dimensions' );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( $sku.attr( 'data-o_sku' ) )
|
|
|
|
$sku.text( $sku.attr( 'data-o_sku' ) );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( $weight.attr( 'data-o_weight' ) )
|
|
|
|
$weight.text( $weight.attr( 'data-o_weight' ) );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( $dimensions.attr( 'data-o_dimensions' ) )
|
|
|
|
$dimensions.text( $dimensions.attr( 'data-o_dimensions' ) );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
return false;
|
|
|
|
} )
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// Upon changing an option
|
|
|
|
.on( 'change', '.variations select', function( event ) {
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
$variation_form = $( this ).closest( '.variations_form' );
|
2014-10-20 08:59:01 +00:00
|
|
|
|
|
|
|
if ( $variation_form.find( 'input.variation_id' ).length > 0 )
|
|
|
|
$variation_form.find( 'input.variation_id' ).val( '' ).change();
|
|
|
|
else
|
|
|
|
$variation_form.find( 'input[name=variation_id]' ).val( '' ).change();
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
$variation_form
|
|
|
|
.trigger( 'woocommerce_variation_select_change' )
|
|
|
|
.trigger( 'check_variations', [ '', false ] );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
$( this ).blur();
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if( $().uniform && $.isFunction( $.uniform.update ) ) {
|
|
|
|
$.uniform.update();
|
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
} )
|
2012-08-22 06:51:19 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// Upon gaining focus
|
|
|
|
.on( 'focusin touchstart', '.variations select', function( event ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
$variation_form = $( this ).closest( '.variations_form' );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-10-20 08:59:01 +00:00
|
|
|
// Get attribute name from data-attribute_name, or from input name if it doesn't exist
|
|
|
|
if ( typeof( $( this ).data( 'attribute_name' ) ) != 'undefined' )
|
|
|
|
attribute_name = $( this ).data( 'attribute_name' );
|
|
|
|
else
|
|
|
|
attribute_name = $( this ).attr( 'name' );
|
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
$variation_form
|
|
|
|
.trigger( 'woocommerce_variation_select_focusin' )
|
2014-10-20 08:59:01 +00:00
|
|
|
.trigger( 'check_variations', [ attribute_name, true ] );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
} )
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// Check variations
|
|
|
|
.on( 'check_variations', function( event, exclude, focus ) {
|
|
|
|
var all_set = true,
|
|
|
|
any_set = false,
|
|
|
|
showing_variation = false,
|
|
|
|
current_settings = {},
|
|
|
|
$variation_form = $( this ),
|
|
|
|
$reset_variations = $variation_form.find( '.reset_variations' );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
$variation_form.find( '.variations select' ).each( function() {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-10-20 08:59:01 +00:00
|
|
|
// Get attribute name from data-attribute_name, or from input name if it doesn't exist
|
|
|
|
if ( typeof( $( this ).data( 'attribute_name' ) ) != 'undefined' )
|
|
|
|
attribute_name = $( this ).data( 'attribute_name' );
|
|
|
|
else
|
|
|
|
attribute_name = $( this ).attr( 'name' );
|
|
|
|
|
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( $( this ).val().length === 0 ) {
|
|
|
|
all_set = false;
|
|
|
|
} else {
|
|
|
|
any_set = true;
|
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-10-20 08:59:01 +00:00
|
|
|
if ( exclude && attribute_name === exclude ) {
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
all_set = false;
|
2014-10-20 08:59:01 +00:00
|
|
|
current_settings[ attribute_name ] = '';
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
} else {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// Encode entities
|
|
|
|
value = $( this ).val();
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// Add to settings array
|
2014-10-20 08:59:01 +00:00
|
|
|
current_settings[ attribute_name ] = value;
|
2014-03-17 05:19:58 +00:00
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
});
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
var product_id = parseInt( $variation_form.data( 'product_id' ) ),
|
|
|
|
all_variations = $variation_form.data( 'product_variations' );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// Fallback to window property if not set - backwards compat
|
|
|
|
if ( ! all_variations )
|
|
|
|
all_variations = window.product_variations.product_id;
|
|
|
|
if ( ! all_variations )
|
|
|
|
all_variations = window.product_variations;
|
|
|
|
if ( ! all_variations )
|
|
|
|
all_variations = window['product_variations_' + product_id ];
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
var matching_variations = $.fn.wc_variation_form.find_matching_variations( all_variations, current_settings );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( all_set ) {
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
var variation = matching_variations.shift();
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( variation ) {
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// Found - set ID
|
2014-10-20 08:59:01 +00:00
|
|
|
|
|
|
|
// Get variation input by class, or by input name if class doesn't exist
|
|
|
|
if ( $variation_form.find( 'input.variation_id' ).length > 0 )
|
|
|
|
$variation_input = $variation_form.find( 'input.variation_id' );
|
|
|
|
else
|
|
|
|
$variation_input = $variation_form.find( 'input[name=variation_id]' );
|
|
|
|
|
|
|
|
// Set ID
|
|
|
|
$variation_input
|
2014-03-17 05:19:58 +00:00
|
|
|
.val( variation.variation_id )
|
|
|
|
.change();
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
$variation_form.trigger( 'found_variation', [ variation ] );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
} else {
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// Nothing found - reset fields
|
|
|
|
$variation_form.find( '.variations select' ).val( '' );
|
2012-08-22 06:51:19 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( ! focus )
|
|
|
|
$variation_form.trigger( 'reset_image' );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
alert( wc_add_to_cart_variation_params.i18n_no_matching_variations_text );
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
} else {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
$variation_form.trigger( 'update_variation_values', [ matching_variations ] );
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( ! focus )
|
|
|
|
$variation_form.trigger( 'reset_image' );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( ! exclude ) {
|
|
|
|
$variation_form.find( '.single_variation_wrap' ).slideUp( 200 );
|
2014-02-22 04:08:08 +00:00
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( any_set ) {
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( $reset_variations.css( 'visibility' ) === 'hidden' )
|
|
|
|
$reset_variations.css( 'visibility', 'visible' ).hide().fadeIn();
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
} else {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
$reset_variations.css( 'visibility', 'hidden' );
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
} )
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// Reset product image
|
|
|
|
.on( 'reset_image', function( event ) {
|
2014-02-22 04:08:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
var $product = $(this).closest( '.product' ),
|
|
|
|
$product_img = $product.find( 'div.images img:eq(0)' ),
|
|
|
|
$product_link = $product.find( 'div.images a.zoom:eq(0)' ),
|
|
|
|
o_src = $product_img.attr( 'data-o_src' ),
|
|
|
|
o_title = $product_img.attr( 'data-o_title' ),
|
|
|
|
o_alt = $product_img.attr( 'data-o_alt' ),
|
|
|
|
o_href = $product_link.attr( 'data-o_href' );
|
2014-02-22 04:08:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( o_src !== undefined ) {
|
|
|
|
$product_img
|
|
|
|
.attr( 'src', o_src );
|
|
|
|
}
|
2014-02-22 04:08:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( o_href !== undefined ) {
|
|
|
|
$product_link
|
|
|
|
.attr( 'href', o_href );
|
|
|
|
}
|
2014-02-22 04:08:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( o_title !== undefined ) {
|
|
|
|
$product_img
|
|
|
|
.attr( 'title', o_title );
|
|
|
|
$product_link
|
|
|
|
.attr( 'title', o_title );
|
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( o_alt !== undefined ) {
|
|
|
|
$product_img
|
|
|
|
.attr( 'alt', o_alt );
|
|
|
|
}
|
|
|
|
} )
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// Disable option fields that are unavaiable for current set of attributes
|
|
|
|
.on( 'update_variation_values', function( event, variations ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
$variation_form = $( this ).closest( '.variations_form' );
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +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
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
current_attr_select = $( el );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// Reset options
|
|
|
|
if ( ! current_attr_select.data( 'attribute_options' ) )
|
|
|
|
current_attr_select.data( 'attribute_options', current_attr_select.find( 'option:gt(0)' ).get() );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
current_attr_select.find( 'option:gt(0)' ).remove();
|
|
|
|
current_attr_select.append( current_attr_select.data( 'attribute_options' ) );
|
2014-09-26 16:22:43 +00:00
|
|
|
current_attr_select.find( 'option:gt(0)' ).removeClass( 'attached' );
|
|
|
|
|
|
|
|
current_attr_select.find( 'option:gt(0)' ).removeClass( 'enabled' );
|
|
|
|
current_attr_select.find( 'option:gt(0)' ).removeAttr( 'disabled' );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-10-20 08:59:01 +00:00
|
|
|
// Get name from data-attribute_name, or from input name if it doesn't exist
|
|
|
|
if ( typeof( current_attr_select.data( 'attribute_name' ) ) != 'undefined' )
|
|
|
|
current_attr_name = current_attr_select.data( 'attribute_name' );
|
|
|
|
else
|
|
|
|
current_attr_name = current_attr_select.attr( 'name' );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// Loop through variations
|
|
|
|
for ( var num in variations ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( typeof( variations[ num ] ) != 'undefined' ) {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
var attributes = variations[ num ].attributes;
|
2013-02-01 11:49:29 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
for ( var attr_name in attributes ) {
|
|
|
|
if ( attributes.hasOwnProperty( attr_name ) ) {
|
2014-02-22 04:08:08 +00:00
|
|
|
var attr_val = attributes[ attr_name ];
|
2013-02-01 11:49:29 +00:00
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
if ( attr_name == current_attr_name ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-09-26 16:22:43 +00:00
|
|
|
if ( variations[ num ].variation_is_active )
|
|
|
|
variation_active = 'enabled';
|
|
|
|
else
|
|
|
|
variation_active = '';
|
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
if ( attr_val ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
// Decode entities
|
|
|
|
attr_val = $( '<div/>' ).html( attr_val ).text();
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
// Add slashes
|
2014-03-17 05:19:58 +00:00
|
|
|
attr_val = attr_val.replace( /'/g, "\\'" );
|
|
|
|
attr_val = attr_val.replace( /"/g, "\\\"" );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
// Compare the meerkat
|
2014-09-26 16:22:43 +00:00
|
|
|
current_attr_select.find( 'option[value="' + attr_val + '"]' ).addClass( 'attached ' + variation_active );
|
2013-02-01 11:49:29 +00:00
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
} else {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-09-26 16:22:43 +00:00
|
|
|
current_attr_select.find( 'option:gt(0)' ).addClass( 'attached ' + variation_active );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 16:22:43 +00:00
|
|
|
// Detach unattached
|
|
|
|
current_attr_select.find( 'option:gt(0):not(.attached)' ).remove();
|
|
|
|
|
|
|
|
// Grey out disabled
|
|
|
|
current_attr_select.find( 'option:gt(0):not(.enabled)' ).attr( 'disabled', 'disabled' );
|
2014-02-22 04:08:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
});
|
2014-02-22 04:08:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
// 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 ),
|
|
|
|
$product = $( this ).closest( '.product' ),
|
|
|
|
$product_img = $product.find( 'div.images img:eq(0)' ),
|
|
|
|
$product_link = $product.find( 'div.images a.zoom:eq(0)' ),
|
|
|
|
o_src = $product_img.attr( 'data-o_src' ),
|
|
|
|
o_title = $product_img.attr( 'data-o_title' ),
|
|
|
|
o_alt = $product_img.attr( 'data-o_alt' ),
|
|
|
|
o_href = $product_link.attr( 'data-o_href' ),
|
|
|
|
variation_image = variation.image_src,
|
|
|
|
variation_link = variation.image_link,
|
|
|
|
variation_title = variation.image_title,
|
|
|
|
variation_alt = variation.image_alt;
|
|
|
|
|
|
|
|
$variation_form.find( '.variations_button' ).show();
|
|
|
|
$variation_form.find( '.single_variation' ).html( variation.price_html + variation.availability_html );
|
|
|
|
|
|
|
|
if ( o_src === undefined ) {
|
|
|
|
o_src = ( ! $product_img.attr( 'src' ) ) ? '' : $product_img.attr( 'src' );
|
|
|
|
$product_img.attr( 'data-o_src', o_src );
|
|
|
|
}
|
2014-02-22 04:08:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( o_href === undefined ) {
|
|
|
|
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
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( o_title === undefined ) {
|
|
|
|
o_title = ( ! $product_img.attr( 'title' ) ) ? '' : $product_img.attr( 'title' );
|
|
|
|
$product_img.attr( 'data-o_title', o_title );
|
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( o_alt === undefined ) {
|
|
|
|
o_alt = ( ! $product_img.attr( 'alt' ) ) ? '' : $product_img.attr( 'alt' );
|
|
|
|
$product_img.attr( 'data-o_alt', o_alt );
|
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( variation_image && variation_image.length > 1 ) {
|
|
|
|
$product_img
|
|
|
|
.attr( 'src', variation_image )
|
|
|
|
.attr( 'alt', variation_alt )
|
|
|
|
.attr( 'title', variation_title );
|
|
|
|
$product_link
|
|
|
|
.attr( 'href', variation_link )
|
|
|
|
.attr( 'title', variation_title );
|
|
|
|
} else {
|
|
|
|
$product_img
|
|
|
|
.attr( 'src', o_src )
|
|
|
|
.attr( 'alt', o_alt )
|
|
|
|
.attr( 'title', o_title );
|
|
|
|
$product_link
|
|
|
|
.attr( 'href', o_href )
|
|
|
|
.attr( 'title', o_title );
|
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
var $single_variation_wrap = $variation_form.find( '.single_variation_wrap' ),
|
|
|
|
$sku = $product.find( '.product_meta' ).find( '.sku' ),
|
|
|
|
$weight = $product.find( '.product_weight' ),
|
|
|
|
$dimensions = $product.find( '.product_dimensions' );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( ! $sku.attr( 'data-o_sku' ) )
|
|
|
|
$sku.attr( 'data-o_sku', $sku.text() );
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( ! $weight.attr( 'data-o_weight' ) )
|
|
|
|
$weight.attr( 'data-o_weight', $weight.text() );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( ! $dimensions.attr( 'data-o_dimensions' ) )
|
|
|
|
$dimensions.attr( 'data-o_dimensions', $dimensions.text() );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( variation.sku ) {
|
|
|
|
$sku.text( variation.sku );
|
|
|
|
} else {
|
|
|
|
$sku.text( $sku.attr( 'data-o_sku' ) );
|
|
|
|
}
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( variation.weight ) {
|
|
|
|
$weight.text( variation.weight );
|
|
|
|
} else {
|
|
|
|
$weight.text( $weight.attr( 'data-o_weight' ) );
|
|
|
|
}
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( variation.dimensions ) {
|
|
|
|
$dimensions.text( variation.dimensions );
|
|
|
|
} else {
|
|
|
|
$dimensions.text( $dimensions.attr( 'data-o_dimensions' ) );
|
|
|
|
}
|
2013-10-10 15:34:44 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
$single_variation_wrap.find( '.quantity' ).show();
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2014-03-17 12:12:10 +00:00
|
|
|
if ( ! variation.is_purchasable || ! variation.is_in_stock || ! variation.variation_is_visible ) {
|
2014-03-17 05:19:58 +00:00
|
|
|
$variation_form.find( '.variations_button' ).hide();
|
|
|
|
}
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( ! variation.variation_is_visible ) {
|
|
|
|
$variation_form.find( '.single_variation' ).html( '<p>' + wc_add_to_cart_variation_params.i18n_unavailable_text + '</p>' );
|
|
|
|
}
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( variation.min_qty )
|
2014-10-21 12:26:39 +00:00
|
|
|
$single_variation_wrap.find( '.quantity input.qty' ).attr( 'min', variation.min_qty ).val( variation.min_qty );
|
2014-03-17 05:19:58 +00:00
|
|
|
else
|
2014-10-21 12:26:39 +00:00
|
|
|
$single_variation_wrap.find( '.quantity input.qty' ).removeAttr( 'min' );
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2014-03-17 05:19:58 +00:00
|
|
|
if ( variation.max_qty )
|
2014-10-21 12:26:39 +00:00
|
|
|
$single_variation_wrap.find( '.quantity input.qty' ).attr( 'max', variation.max_qty );
|
2014-03-17 05:19:58 +00:00
|
|
|
else
|
2014-10-21 12:26:39 +00:00
|
|
|
$single_variation_wrap.find( '.quantity input.qty' ).removeAttr( 'max' );
|
2014-03-17 05:19:58 +00:00
|
|
|
|
|
|
|
if ( variation.is_sold_individually === 'yes' ) {
|
2014-10-21 12:26:39 +00:00
|
|
|
$single_variation_wrap.find( '.quantity input.qty' ).val( '1' );
|
2014-03-17 05:19:58 +00:00
|
|
|
$single_variation_wrap.find( '.quantity' ).hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
$single_variation_wrap.slideDown( 200 ).trigger( 'show_variation', [ variation ] );
|
|
|
|
|
|
|
|
});
|
2013-06-17 12:53:21 +00:00
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
$form.trigger( 'wc_variation_form' );
|
|
|
|
|
2013-06-17 12:53:21 +00:00
|
|
|
return $form;
|
2014-02-22 04:08:08 +00:00
|
|
|
};
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
$( function() {
|
2013-12-04 19:15:24 +00:00
|
|
|
|
|
|
|
// wc_add_to_cart_variation_params is required to continue, ensure the object exists
|
2014-02-22 04:08:08 +00:00
|
|
|
if ( typeof wc_add_to_cart_variation_params === 'undefined' )
|
2013-12-04 19:15:24 +00:00
|
|
|
return false;
|
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
$( '.variations_form' ).wc_variation_form();
|
|
|
|
$( '.variations_form .variations select' ).change();
|
|
|
|
});
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2013-12-04 19:15:24 +00:00
|
|
|
})( jQuery, window, document );
|