2015-07-10 05:13:30 +00:00
|
|
|
/*global wc_add_to_cart_variation_params */
|
2015-07-10 10:37:05 +00:00
|
|
|
/*global wc_cart_fragments_params */
|
2013-02-07 10:12:08 +00:00
|
|
|
/*!
|
|
|
|
* Variations Plugin
|
|
|
|
*/
|
|
|
|
;(function ( $, window, document, undefined ) {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
$.fn.wc_variation_form = function() {
|
|
|
|
var $form = this;
|
|
|
|
var $product = $form.closest('.product');
|
2015-07-10 10:37:05 +00:00
|
|
|
var $product_id = parseInt( $form.data( 'product_id' ), 10 );
|
2015-07-09 14:56:20 +00:00
|
|
|
var $product_variations = $form.data( 'product_variations' );
|
|
|
|
var $use_ajax = $product_variations === false;
|
|
|
|
var $xhr = false;
|
|
|
|
var $reset_variations = $form.find( '.reset_variations' );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Unbind any existing events
|
|
|
|
$form.unbind( 'check_variations update_variation_values found_variation' );
|
|
|
|
$form.find( '.reset_variations' ).unbind( 'click' );
|
|
|
|
$form.find( '.variations select' ).unbind( 'change focusin' );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Bind new events to form
|
|
|
|
$form
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// On clicking the reset variation button
|
2015-07-10 10:37:05 +00:00
|
|
|
.on( 'click', '.reset_variations', function() {
|
2015-07-09 14:56:20 +00:00
|
|
|
$form.find( '.variations select' ).val( '' ).change();
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'reset_data' );
|
2015-07-09 14:56:20 +00:00
|
|
|
return false;
|
|
|
|
} )
|
|
|
|
|
2015-07-28 12:34:04 +00:00
|
|
|
// Reload product variations data
|
|
|
|
.on( 'reload_product_variations', function() {
|
|
|
|
$product_variations = $form.data( 'product_variations' );
|
|
|
|
$use_ajax = $product_variations === false;
|
|
|
|
} )
|
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Reset product data
|
2015-07-10 10:37:05 +00:00
|
|
|
.on( 'reset_data', function() {
|
2015-07-09 14:56:20 +00:00
|
|
|
var to_reset = {
|
|
|
|
'.sku': 'o_sku',
|
|
|
|
'.product_weight': 'o_weight',
|
|
|
|
'.product_dimensions': 'o_dimensions'
|
|
|
|
};
|
|
|
|
$.each( to_reset, function( selector, data_attribute ) {
|
|
|
|
var $el = $product.find( selector );
|
|
|
|
if ( $el.attr( 'data-' + data_attribute ) ) {
|
|
|
|
$el.text( $el.attr( 'data-' + data_attribute ) );
|
2014-02-22 04:08:08 +00:00
|
|
|
}
|
2015-07-09 14:56:20 +00:00
|
|
|
});
|
2015-07-31 11:04:55 +00:00
|
|
|
$form.wc_variations_description_update( '' );
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'reset_image' );
|
|
|
|
$form.find( '.single_variation_wrap' ).slideUp( 200 ).trigger( 'hide_variation' );
|
2015-07-09 14:56:20 +00:00
|
|
|
} )
|
|
|
|
|
|
|
|
// Reset product image
|
2015-07-10 10:37:05 +00:00
|
|
|
.on( 'reset_image', function() {
|
2015-07-09 14:56:20 +00:00
|
|
|
var $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_title' ),
|
|
|
|
o_href = $product_link.attr( 'data-o_href' );
|
|
|
|
|
|
|
|
if ( o_src !== undefined ) {
|
|
|
|
$product_img.attr( 'src', o_src );
|
2014-02-22 04:08:08 +00:00
|
|
|
}
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( o_href !== undefined ) {
|
|
|
|
$product_link.attr( 'href', o_href );
|
2014-02-22 04:08:08 +00:00
|
|
|
}
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( o_title !== undefined ) {
|
|
|
|
$product_img.attr( 'title', o_title );
|
|
|
|
$product_link.attr( 'title', o_title );
|
2015-05-14 17:56:26 +00:00
|
|
|
}
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( o_alt !== undefined ) {
|
|
|
|
$product_img.attr( 'alt', o_alt );
|
|
|
|
}
|
|
|
|
} )
|
2015-05-14 17:56:26 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// On changing an attribute
|
2015-07-10 10:37:05 +00:00
|
|
|
.on( 'change', '.variations select', function() {
|
2015-07-09 14:56:20 +00:00
|
|
|
$form.find( 'input[name="variation_id"], input.variation_id' ).val( '' ).change();
|
|
|
|
$form.find( '.wc-no-matching-variations' ).remove();
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( $use_ajax ) {
|
|
|
|
if ( $xhr ) {
|
|
|
|
$xhr.abort();
|
2015-07-10 05:13:30 +00:00
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
var all_attributes_chosen = true;
|
|
|
|
var some_attributes_chosen = false;
|
|
|
|
var data = {};
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
$form.find( '.variations select' ).each( function() {
|
|
|
|
var attribute_name = $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( $( this ).val().length === 0 ) {
|
|
|
|
all_attributes_chosen = false;
|
|
|
|
} else {
|
|
|
|
some_attributes_chosen = true;
|
|
|
|
}
|
2014-10-20 08:59:01 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
data[ attribute_name ] = $( this ).val();
|
|
|
|
});
|
2015-07-10 05:13:30 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( all_attributes_chosen ) {
|
|
|
|
// Get a matchihng variation via ajax
|
|
|
|
data.product_id = $product_id;
|
|
|
|
|
|
|
|
$xhr = $.ajax( {
|
2015-07-27 16:55:37 +00:00
|
|
|
url: wc_cart_fragments_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_variation' ),
|
2015-07-09 14:56:20 +00:00
|
|
|
type: 'POST',
|
|
|
|
data: data,
|
|
|
|
success: function( variation ) {
|
|
|
|
if ( variation ) {
|
|
|
|
$form.find( 'input[name="variation_id"], input.variation_id' )
|
|
|
|
.val( variation.variation_id )
|
|
|
|
.change();
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'found_variation', [ variation ] );
|
2015-07-09 14:56:20 +00:00
|
|
|
} else {
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'reset_data' );
|
2015-07-09 14:56:20 +00:00
|
|
|
$form.find( '.single_variation_wrap' ).after( '<p class="wc-no-matching-variations woocommerce-info">' + wc_add_to_cart_variation_params.i18n_no_matching_variations_text + '</p>' );
|
|
|
|
$form.find( '.wc-no-matching-variations' ).slideDown( 200 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
} else {
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'reset_data' );
|
2015-05-14 17:56:26 +00:00
|
|
|
}
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( some_attributes_chosen ) {
|
|
|
|
if ( $reset_variations.css( 'visibility' ) === 'hidden' ) {
|
|
|
|
$reset_variations.css( 'visibility', 'visible' ).hide().fadeIn();
|
|
|
|
}
|
2015-05-14 17:56:26 +00:00
|
|
|
} else {
|
2015-07-09 14:56:20 +00:00
|
|
|
$reset_variations.css( 'visibility', 'hidden' );
|
2015-05-14 17:56:26 +00:00
|
|
|
}
|
2015-07-09 14:56:20 +00:00
|
|
|
} else {
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'woocommerce_variation_select_change' );
|
|
|
|
$form.trigger( 'check_variations', [ '', false ] );
|
2014-03-17 05:19:58 +00:00
|
|
|
$( this ).blur();
|
2015-07-09 14:56:20 +00:00
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-10-02 01:45:11 +00:00
|
|
|
// added to get around variation image flicker issue
|
2015-10-02 08:26:53 +00:00
|
|
|
$( '.product.has-default-attributes > .images' ).fadeTo( 200, 1 );
|
2015-10-27 19:24:32 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Custom event for when variation selection has been changed
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'woocommerce_variation_has_changed' );
|
2015-07-09 14:56:20 +00:00
|
|
|
} )
|
2014-12-10 21:43:26 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Upon gaining focus
|
2015-07-10 10:37:05 +00:00
|
|
|
.on( 'focusin touchstart', '.variations select', function() {
|
2015-10-27 19:59:26 +00:00
|
|
|
$( this ).find( 'option:selected' ).attr( 'selected', 'selected' );
|
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( ! $use_ajax ) {
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'woocommerce_variation_select_focusin' );
|
|
|
|
$form.trigger( 'check_variations', [ $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' ), true ] );
|
2015-07-09 14:56:20 +00:00
|
|
|
}
|
|
|
|
} )
|
|
|
|
|
|
|
|
// Show single variation details (price, stock, image)
|
|
|
|
.on( 'found_variation', function( event, variation ) {
|
|
|
|
var $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_caption = variation.image_caption,
|
2015-07-10 10:37:05 +00:00
|
|
|
variation_title = variation.image_title;
|
2015-07-09 14:56:20 +00:00
|
|
|
|
|
|
|
$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 );
|
|
|
|
}
|
2012-08-22 06:51:19 +00:00
|
|
|
|
2015-07-09 14:56:20 +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-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +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-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( o_alt === undefined ) {
|
|
|
|
o_alt = ( ! $product_img.attr( 'alt' ) ) ? '' : $product_img.attr( 'alt' );
|
|
|
|
$product_img.attr( 'data-o_alt', o_alt );
|
|
|
|
}
|
2014-10-20 08:59:01 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
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 )
|
|
|
|
.attr( 'title', variation_caption );
|
|
|
|
} 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-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
var $single_variation_wrap = $form.find( '.single_variation_wrap' ),
|
|
|
|
$sku = $product.find( '.product_meta' ).find( '.sku' ),
|
|
|
|
$weight = $product.find( '.product_weight' ),
|
|
|
|
$dimensions = $product.find( '.product_dimensions' );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( ! $sku.attr( 'data-o_sku' ) ) {
|
|
|
|
$sku.attr( 'data-o_sku', $sku.text() );
|
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( ! $weight.attr( 'data-o_weight' ) ) {
|
|
|
|
$weight.attr( 'data-o_weight', $weight.text() );
|
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( ! $dimensions.attr( 'data-o_dimensions' ) ) {
|
|
|
|
$dimensions.attr( 'data-o_dimensions', $dimensions.text() );
|
|
|
|
}
|
2014-10-20 08:59:01 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( variation.sku ) {
|
|
|
|
$sku.text( variation.sku );
|
|
|
|
} else {
|
|
|
|
$sku.text( $sku.attr( 'data-o_sku' ) );
|
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( variation.weight ) {
|
|
|
|
$weight.text( variation.weight );
|
|
|
|
} else {
|
|
|
|
$weight.text( $weight.attr( 'data-o_weight' ) );
|
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( variation.dimensions ) {
|
|
|
|
$dimensions.text( variation.dimensions );
|
|
|
|
} else {
|
|
|
|
$dimensions.text( $dimensions.attr( 'data-o_dimensions' ) );
|
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-31 11:04:55 +00:00
|
|
|
var hide_qty = false;
|
|
|
|
var hide_qty_button = false;
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( ! variation.is_purchasable || ! variation.is_in_stock || ! variation.variation_is_visible ) {
|
2015-07-31 11:04:55 +00:00
|
|
|
hide_qty_button = true;
|
2015-07-09 14:56:20 +00:00
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( ! variation.variation_is_visible ) {
|
|
|
|
$form.find( '.single_variation' ).html( '<p>' + wc_add_to_cart_variation_params.i18n_unavailable_text + '</p>' );
|
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( variation.min_qty !== '' ) {
|
|
|
|
$single_variation_wrap.find( '.quantity input.qty' ).attr( 'min', variation.min_qty ).val( variation.min_qty );
|
|
|
|
} else {
|
|
|
|
$single_variation_wrap.find( '.quantity input.qty' ).removeAttr( 'min' );
|
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( variation.max_qty !== '' ) {
|
|
|
|
$single_variation_wrap.find( '.quantity input.qty' ).attr( 'max', variation.max_qty );
|
|
|
|
} else {
|
|
|
|
$single_variation_wrap.find( '.quantity input.qty' ).removeAttr( 'max' );
|
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( variation.is_sold_individually === 'yes' ) {
|
|
|
|
$single_variation_wrap.find( '.quantity input.qty' ).val( '1' );
|
2015-07-31 11:04:55 +00:00
|
|
|
hide_qty = true;
|
2015-07-09 14:56:20 +00:00
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2015-07-31 11:04:55 +00:00
|
|
|
// Show/hide qty container
|
|
|
|
if ( hide_qty ) {
|
|
|
|
$single_variation_wrap.find( '.quantity' ).hide();
|
|
|
|
} else {
|
|
|
|
// No need to hide it when hiding its container
|
|
|
|
if ( ! hide_qty_button ) {
|
|
|
|
$single_variation_wrap.find( '.quantity' ).show();
|
|
|
|
}
|
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-31 11:04:55 +00:00
|
|
|
// Show/hide qty & button container
|
|
|
|
if ( hide_qty_button ) {
|
|
|
|
if ( $single_variation_wrap.is( ':visible' ) ) {
|
|
|
|
$form.find( '.variations_button' ).slideUp( 200 );
|
|
|
|
} else {
|
|
|
|
$form.find( '.variations_button' ).hide();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( $single_variation_wrap.is( ':visible' ) ) {
|
|
|
|
$form.find( '.variations_button' ).slideDown( 200 );
|
|
|
|
} else {
|
|
|
|
$form.find( '.variations_button' ).show();
|
|
|
|
}
|
2015-07-09 14:56:20 +00:00
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-31 11:04:55 +00:00
|
|
|
// Refresh variation description
|
|
|
|
$form.wc_variations_description_update( variation.variation_description );
|
|
|
|
|
2015-07-14 09:28:49 +00:00
|
|
|
$single_variation_wrap.slideDown( 200 ).trigger( 'show_variation', [ variation ] );
|
2015-07-09 14:56:20 +00:00
|
|
|
})
|
2014-10-20 08:59:01 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Check variations
|
|
|
|
.on( 'check_variations', function( event, exclude, focus ) {
|
|
|
|
if ( $use_ajax ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-20 08:59:01 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
var all_attributes_chosen = true,
|
|
|
|
some_attributes_chosen = false,
|
|
|
|
current_settings = {},
|
|
|
|
$form = $( this ),
|
|
|
|
$reset_variations = $form.find( '.reset_variations' );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
$form.find( '.variations select' ).each( function() {
|
|
|
|
var attribute_name = $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( $( this ).val().length === 0 ) {
|
|
|
|
all_attributes_chosen = false;
|
2014-03-17 05:19:58 +00:00
|
|
|
} else {
|
2015-07-09 14:56:20 +00:00
|
|
|
some_attributes_chosen = true;
|
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( exclude && attribute_name === exclude ) {
|
|
|
|
all_attributes_chosen = false;
|
|
|
|
current_settings[ attribute_name ] = '';
|
|
|
|
} else {
|
|
|
|
// Add to settings array
|
|
|
|
current_settings[ attribute_name ] = $( this ).val();
|
2014-03-17 05:19:58 +00:00
|
|
|
}
|
2015-07-09 14:56:20 +00:00
|
|
|
});
|
2012-08-22 06:51:19 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
var matching_variations = wc_variation_form_matcher.find_matching_variations( $product_variations, current_settings );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( all_attributes_chosen ) {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
var variation = matching_variations.shift();
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( variation ) {
|
|
|
|
$form.find( 'input[name="variation_id"], input.variation_id' )
|
|
|
|
.val( variation.variation_id )
|
|
|
|
.change();
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'found_variation', [ variation ] );
|
2014-03-17 05:19:58 +00:00
|
|
|
} else {
|
2015-07-09 14:56:20 +00:00
|
|
|
// Nothing found - reset fields
|
|
|
|
$form.find( '.variations select' ).val( '' );
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2015-07-10 05:13:30 +00:00
|
|
|
if ( ! focus ) {
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'reset_data' );
|
2015-07-10 05:13:30 +00:00
|
|
|
}
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-10 10:37:05 +00:00
|
|
|
window.alert( wc_add_to_cart_variation_params.i18n_no_matching_variations_text );
|
2014-03-17 05:19:58 +00:00
|
|
|
}
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
} else {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'update_variation_values', [ matching_variations ] );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( ! focus ) {
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'reset_data' );
|
2014-03-17 05:19:58 +00:00
|
|
|
}
|
2014-02-22 04:08:08 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( ! exclude ) {
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.find( '.single_variation_wrap' ).slideUp( 200 ).trigger( 'hide_variation' );
|
2014-03-17 05:19:58 +00:00
|
|
|
}
|
2015-07-09 14:56:20 +00:00
|
|
|
}
|
|
|
|
if ( some_attributes_chosen ) {
|
|
|
|
if ( $reset_variations.css( 'visibility' ) === 'hidden' ) {
|
|
|
|
$reset_variations.css( 'visibility', 'visible' ).hide().fadeIn();
|
2014-03-17 05:19:58 +00:00
|
|
|
}
|
2015-07-09 14:56:20 +00:00
|
|
|
} else {
|
|
|
|
$reset_variations.css( 'visibility', 'hidden' );
|
|
|
|
}
|
|
|
|
} )
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Disable option fields that are unavaiable for current set of attributes
|
|
|
|
.on( 'update_variation_values', function( event, variations ) {
|
|
|
|
if ( $use_ajax ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Loop through selects and disable/enable options based on selections
|
|
|
|
$form.find( '.variations select' ).each( function( index, el ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-10 10:37:05 +00:00
|
|
|
var current_attr_name, current_attr_select = $( el );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +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
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
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( 'attached' );
|
|
|
|
current_attr_select.find( 'option:gt(0)' ).removeClass( 'enabled' );
|
|
|
|
current_attr_select.find( 'option:gt(0)' ).removeAttr( 'disabled' );
|
2014-09-26 16:22:43 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Get name from data-attribute_name, or from input name if it doesn't exist
|
2015-07-10 10:37:05 +00:00
|
|
|
if ( typeof( current_attr_select.data( 'attribute_name' ) ) !== 'undefined' ) {
|
2015-07-09 14:56:20 +00:00
|
|
|
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
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Loop through variations
|
|
|
|
for ( var num in variations ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-10 10:37:05 +00:00
|
|
|
if ( typeof( variations[ num ] ) !== 'undefined' ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
var attributes = variations[ num ].attributes;
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
for ( var attr_name in attributes ) {
|
|
|
|
if ( attributes.hasOwnProperty( attr_name ) ) {
|
|
|
|
var attr_val = attributes[ attr_name ];
|
2013-02-01 11:49:29 +00:00
|
|
|
|
2015-07-10 10:37:05 +00:00
|
|
|
if ( attr_name === current_attr_name ) {
|
2013-02-01 11:49:29 +00:00
|
|
|
|
2015-07-10 10:37:05 +00:00
|
|
|
var variation_active = '';
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-10 10:37:05 +00:00
|
|
|
if ( variations[ num ].variation_is_active ) {
|
2015-07-09 14:56:20 +00:00
|
|
|
variation_active = 'enabled';
|
2015-07-10 10:37:05 +00:00
|
|
|
}
|
2014-09-26 16:22:43 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( attr_val ) {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Decode entities
|
|
|
|
attr_val = $( '<div/>' ).html( attr_val ).text();
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Add slashes
|
2015-07-10 10:37:05 +00:00
|
|
|
attr_val = attr_val.replace( /'/g, '\\\'' );
|
|
|
|
attr_val = attr_val.replace( /"/g, '\\\"' );
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Compare the meerkat
|
|
|
|
current_attr_select.find( 'option[value="' + attr_val + '"]' ).addClass( 'attached ' + variation_active );
|
2013-02-01 11:49:29 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
} else {
|
2012-08-08 10:53:36 +00:00
|
|
|
|
2015-07-09 14:56:20 +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-03-17 05:19:58 +00:00
|
|
|
}
|
2014-02-22 04:08:08 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Detach unattached
|
|
|
|
current_attr_select.find( 'option:gt(0):not(.attached)' ).remove();
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Grey out disabled
|
|
|
|
current_attr_select.find( 'option:gt(0):not(.enabled)' ).attr( 'disabled', 'disabled' );
|
2012-08-21 15:41:18 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
});
|
2013-10-10 15:34:44 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
// Custom event for when variations have been updated
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'woocommerce_update_variation_values' );
|
2015-07-09 14:56:20 +00:00
|
|
|
});
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2015-07-14 09:28:49 +00:00
|
|
|
$form.trigger( 'wc_variation_form' );
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
return $form;
|
|
|
|
};
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
/**
|
|
|
|
* Matches inline variation objects to chosen attributes
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
var wc_variation_form_matcher = {
|
|
|
|
find_matching_variations: function( product_variations, settings ) {
|
|
|
|
var matching = [];
|
|
|
|
for ( var i = 0; i < product_variations.length; i++ ) {
|
|
|
|
var variation = product_variations[i];
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( wc_variation_form_matcher.variations_match( variation.attributes, settings ) ) {
|
|
|
|
matching.push( variation );
|
2015-07-10 05:13:30 +00:00
|
|
|
}
|
2015-07-09 14:56:20 +00:00
|
|
|
}
|
|
|
|
return matching;
|
|
|
|
},
|
|
|
|
variations_match: function( attrs1, attrs2 ) {
|
|
|
|
var match = true;
|
|
|
|
for ( var attr_name in attrs1 ) {
|
|
|
|
if ( attrs1.hasOwnProperty( attr_name ) ) {
|
|
|
|
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;
|
|
|
|
}
|
2014-03-17 05:19:58 +00:00
|
|
|
}
|
2015-07-09 14:56:20 +00:00
|
|
|
}
|
|
|
|
return match;
|
|
|
|
}
|
2014-02-22 04:08:08 +00:00
|
|
|
};
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2015-07-31 11:04:55 +00:00
|
|
|
/**
|
|
|
|
* Performs animated variation description refreshes
|
|
|
|
*/
|
|
|
|
$.fn.wc_variations_description_update = function( variation_description ) {
|
|
|
|
var $form = this;
|
|
|
|
var $variations_description = $form.find( '.woocommerce-variation-description' );
|
|
|
|
|
|
|
|
if ( $variations_description.length === 0 ) {
|
|
|
|
if ( variation_description ) {
|
|
|
|
// add transparent border to allow correct height measurement when children have top/bottom margins
|
|
|
|
$form.find( '.single_variation_wrap' ).prepend( $( '<div class="woocommerce-variation-description" style="border:1px solid transparent;">' + variation_description + '</div>' ).hide() );
|
|
|
|
$form.find( '.woocommerce-variation-description' ).slideDown( 200 );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var load_height = $variations_description.outerHeight( true );
|
|
|
|
var new_height = 0;
|
|
|
|
var animate_height = false;
|
|
|
|
|
|
|
|
// lock height
|
|
|
|
$variations_description.css( 'height', load_height );
|
|
|
|
// replace html
|
|
|
|
$variations_description.html( variation_description );
|
|
|
|
// measure height
|
|
|
|
$variations_description.css( 'height', 'auto' );
|
|
|
|
|
|
|
|
new_height = $variations_description.outerHeight( true );
|
|
|
|
|
|
|
|
if ( Math.abs( new_height - load_height ) > 1 ) {
|
|
|
|
animate_height = true;
|
|
|
|
// lock height
|
|
|
|
$variations_description.css( 'height', load_height );
|
|
|
|
}
|
|
|
|
|
|
|
|
// animate height
|
|
|
|
if ( animate_height ) {
|
|
|
|
$variations_description.animate( { 'height' : new_height }, { duration: 200, queue: false, always: function() {
|
|
|
|
$variations_description.css( { 'height' : 'auto' } );
|
|
|
|
} } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-22 04:08:08 +00:00
|
|
|
$( function() {
|
2015-07-09 14:56:20 +00:00
|
|
|
if ( typeof wc_add_to_cart_variation_params !== 'undefined' ) {
|
2015-07-09 15:21:17 +00:00
|
|
|
$( '.variations_form' ).each( function() {
|
|
|
|
$( this ).wc_variation_form().find('.variations select:eq(0)').change();
|
|
|
|
});
|
2015-07-10 05:13:30 +00:00
|
|
|
}
|
2014-02-22 04:08:08 +00:00
|
|
|
});
|
2013-02-07 10:12:08 +00:00
|
|
|
|
2015-07-14 09:28:49 +00:00
|
|
|
})( jQuery, window, document );
|