Ajax variation handling

If there are more variations than the threshold allows (set to 20
currently) this loads the matching variation via ajax instead of inline
in the HTML. #8477
This commit is contained in:
Mike Jolley 2015-07-09 15:56:20 +01:00
parent ac8f18a721
commit 65f19d5ca7
19 changed files with 556 additions and 502 deletions

File diff suppressed because one or more lines are too long

View File

@ -188,7 +188,6 @@ p.demo_store {
del {
opacity: 0.5;
display: block;
}
}
@ -204,7 +203,7 @@ p.demo_store {
color: red;
}
.woocommerce-product-rating{
.woocommerce-product-rating {
margin-bottom: 1.618em;
}
@ -405,6 +404,20 @@ p.demo_store {
}
}
.woocommerce-variation-description {
p {
margin-bottom: 1em;
}
}
.reset_variations {
visibility: hidden;
}
.wc-no-matching-variations {
display: none;
}
.button {
vertical-align: middle;
float: left;

View File

@ -3,500 +3,470 @@
*/
;(function ( $, window, document, undefined ) {
$.fn.wc_variation_form = function () {
$.fn.wc_variation_form.find_matching_variations = function( 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 ( $.fn.wc_variation_form.variations_match( variation.attributes, settings ) ) {
matching.push( variation );
}
}
return matching;
};
$.fn.wc_variation_form.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;
}
}
}
return match;
};
$.fn.wc_variation_form.get_variation_description_html = function( form, selected_index ) {
var all_variations = form.data( 'product_variations' ),
var_description_html;
if ( selected_index > 0 ) {
// minus an index to account for first non selected option
selected_index--;
if ( all_variations[ selected_index ].hasOwnProperty( 'variation_description' ) ) {
var_description_html = '<div class="woocommerce-variation-description woocommerce-variation-description-' +
all_variations[ selected_index ].variation_id + '">' + all_variations[ selected_index ].variation_description + '</div>';
return var_description_html;
}
}
return '';
};
$.fn.wc_variation_form = function() {
var $form = this;
var $product = $form.closest('.product');
var $product_id = parseInt( $form.data( 'product_id' ) );
var $product_variations = $form.data( 'product_variations' );
var $use_ajax = $product_variations === false;
var $xhr = false;
var $reset_variations = $form.find( '.reset_variations' );
// 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' );
$form.unbind( 'check_variations update_variation_values found_variation' );
$form.find( '.reset_variations' ).unbind( 'click' );
$form.find( '.variations select' ).unbind( 'change focusin' );
// Bind events
$form = this
// Bind new events to form
$form
// On clicking the reset variation button
.on( 'click', '.reset_variations', function( event ) {
// On clicking the reset variation button
.on( 'click', '.reset_variations', function( event ) {
$form.find( '.variations select' ).val( '' ).change();
$form.triggerHandler( 'reset_data' );
return false;
} )
$( this ).closest( '.variations_form' ).find( '.variations select' ).val( '' ).change();
// Reset product data
.on( 'reset_data', function( event ) {
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 ) );
}
});
$form.find( '.woocommerce-variation-description' ).remove();
$form.triggerHandler( 'reset_image' );
$form.find( '.single_variation_wrap' ).slideUp( 200 ).triggerHandler( 'hide_variation' );
} )
var $sku = $( this ).closest( '.product' ).find( '.sku' ),
$weight = $( this ).closest( '.product' ).find( '.product_weight' ),
$dimensions = $( this ).closest( '.product' ).find( '.product_dimensions' );
// Reset product image
.on( 'reset_image', function( event ) {
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 ( $sku.attr( 'data-o_sku' ) )
$sku.text( $sku.attr( 'data-o_sku' ) );
if ( o_src !== undefined ) {
$product_img.attr( 'src', o_src );
}
if ( o_href !== undefined ) {
$product_link.attr( 'href', o_href );
}
if ( o_title !== undefined ) {
$product_img.attr( 'title', o_title );
$product_link.attr( 'title', o_title );
}
if ( o_alt !== undefined ) {
$product_img.attr( 'alt', o_alt );
}
} )
if ( $weight.attr( 'data-o_weight' ) )
$weight.text( $weight.attr( 'data-o_weight' ) );
// On changing an attribute
.on( 'change', '.variations select', function( event ) {
$form.find( 'input[name="variation_id"], input.variation_id' ).val( '' ).change();
$form.find( '.wc-no-matching-variations' ).remove();
if ( $dimensions.attr( 'data-o_dimensions' ) )
$dimensions.text( $dimensions.attr( 'data-o_dimensions' ) );
return false;
} )
// Upon changing an option
.on( 'change', '.variations select', function( event ) {
var $variation_form = $( this ).closest( '.variations_form' ),
var_description = $.fn.wc_variation_form.get_variation_description_html( $variation_form, this.selectedIndex );
// remove variation description on change
$variation_form.find( '.variations' ).next( '.woocommerce-variation-description' ).remove();
// display variation description
if ( var_description ) {
$variation_form.find( '.variations' ).after( var_description );
if ( $use_ajax ) {
if ( $xhr ) {
$xhr.abort();
}
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();
}
$variation_form
.trigger( 'woocommerce_variation_select_change' )
.trigger( 'check_variations', [ '', false ] );
$( this ).blur();
if( $().uniform && $.isFunction( $.uniform.update ) ) {
$.uniform.update();
}
// Custom event for when variation selection has been changed
$variation_form.trigger( 'woocommerce_variation_has_changed' );
} )
// Upon gaining focus
.on( 'focusin touchstart', '.variations select', function( event ) {
$variation_form = $( this ).closest( '.variations_form' );
// 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' );
$variation_form
.trigger( 'woocommerce_variation_select_focusin' )
.trigger( 'check_variations', [ attribute_name, true ] );
} )
// 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' );
$variation_form.find( '.variations select' ).each( function() {
// 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' );
var all_attributes_chosen = true;
var some_attributes_chosen = false;
var data = {};
$form.find( '.variations select' ).each( function() {
var attribute_name = $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' );
if ( $( this ).val().length === 0 ) {
all_set = false;
all_attributes_chosen = false;
} else {
any_set = true;
}
if ( exclude && attribute_name === exclude ) {
all_set = false;
current_settings[ attribute_name ] = '';
} else {
// Encode entities
value = $( this ).val();
// Add to settings array
current_settings[ attribute_name ] = value;
some_attributes_chosen = true;
}
data[ attribute_name ] = $( this ).val();
});
var product_id = parseInt( $variation_form.data( 'product_id' ) ),
all_variations = $variation_form.data( 'product_variations' );
// 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 ];
var matching_variations = $.fn.wc_variation_form.find_matching_variations( all_variations, current_settings );
if ( all_set ) {
var variation = matching_variations.shift();
if ( variation ) {
// Found - set ID
// 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
.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' );
alert( wc_add_to_cart_variation_params.i18n_no_matching_variations_text );
}
if ( all_attributes_chosen ) {
// Get a matchihng variation via ajax
data.product_id = $product_id;
$xhr = $.ajax( {
url: wc_cart_fragments_params.wc_ajax_url + 'get_variation/',
type: 'POST',
data: data,
success: function( variation ) {
if ( variation ) {
$form.find( 'input[name="variation_id"], input.variation_id' )
.val( variation.variation_id )
.change();
$form.triggerHandler( 'found_variation', [ variation ] );
} else {
$form.triggerHandler( 'reset_data' );
$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 {
$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 ).trigger( 'hide_variation' );
}
$form.triggerHandler( 'reset_data' );
}
if ( any_set ) {
if ( $reset_variations.css( 'visibility' ) === 'hidden' )
if ( some_attributes_chosen ) {
if ( $reset_variations.css( 'visibility' ) === 'hidden' ) {
$reset_variations.css( 'visibility', 'visible' ).hide().fadeIn();
}
} else {
$reset_variations.css( 'visibility', 'hidden' );
$sku = $( this ).closest( '.product' ).find( '.sku' );
$sku.text( $sku.attr( 'data-o_sku' ) );
}
} else {
$form.triggerHandler( 'woocommerce_variation_select_change' )
$form.triggerHandler( 'check_variations', [ '', false ] );
$( this ).blur();
}
// Custom event for when variation selection has been changed
$form.triggerHandler( 'woocommerce_variation_has_changed' );
} )
// Upon gaining focus
.on( 'focusin touchstart', '.variations select', function( event ) {
if ( ! $use_ajax ) {
$form.triggerHandler( 'woocommerce_variation_select_focusin' )
$form.triggerHandler( 'check_variations', [ $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' ), true ] );
}
} )
// 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,
variation_title = variation.image_title,
variation_alt = variation.image_alt;
$form.find( '.variations_button' ).show();
$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 );
}
if ( o_href === undefined ) {
o_href = ( ! $product_link.attr( 'href' ) ) ? '' : $product_link.attr( 'href' );
$product_link.attr( 'data-o_href', o_href );
}
if ( o_title === undefined ) {
o_title = ( ! $product_img.attr( 'title' ) ) ? '' : $product_img.attr( 'title' );
$product_img.attr( 'data-o_title', o_title );
}
if ( o_alt === undefined ) {
o_alt = ( ! $product_img.attr( 'alt' ) ) ? '' : $product_img.attr( 'alt' );
$product_img.attr( 'data-o_alt', o_alt );
}
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 );
}
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' );
if ( ! $sku.attr( 'data-o_sku' ) ) {
$sku.attr( 'data-o_sku', $sku.text() );
}
if ( ! $weight.attr( 'data-o_weight' ) ) {
$weight.attr( 'data-o_weight', $weight.text() );
}
if ( ! $dimensions.attr( 'data-o_dimensions' ) ) {
$dimensions.attr( 'data-o_dimensions', $dimensions.text() );
}
if ( variation.sku ) {
$sku.text( variation.sku );
} else {
$sku.text( $sku.attr( 'data-o_sku' ) );
}
if ( variation.weight ) {
$weight.text( variation.weight );
} else {
$weight.text( $weight.attr( 'data-o_weight' ) );
}
if ( variation.dimensions ) {
$dimensions.text( variation.dimensions );
} else {
$dimensions.text( $dimensions.attr( 'data-o_dimensions' ) );
}
$single_variation_wrap.find( '.quantity' ).show();
if ( ! variation.is_purchasable || ! variation.is_in_stock || ! variation.variation_is_visible ) {
$form.find( '.variations_button' ).hide();
}
if ( ! variation.variation_is_visible ) {
$form.find( '.single_variation' ).html( '<p>' + wc_add_to_cart_variation_params.i18n_unavailable_text + '</p>' );
}
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' );
}
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' );
}
if ( variation.is_sold_individually === 'yes' ) {
$single_variation_wrap.find( '.quantity input.qty' ).val( '1' );
$single_variation_wrap.find( '.quantity' ).hide();
}
// display variation description
$form.find( '.woocommerce-variation-description' ).remove();
if ( variation.variation_description ) {
$form.find( '.single_variation_wrap' ).prepend( '<div class="woocommerce-variation-description">' + variation.variation_description + '</div>' );
}
$single_variation_wrap.slideDown( 200 ).triggerHandler( 'show_variation', [ variation ] );
})
// Check variations
.on( 'check_variations', function( event, exclude, focus ) {
if ( $use_ajax ) {
return;
}
var all_attributes_chosen = true,
some_attributes_chosen = false,
showing_variation = false,
current_settings = {},
$form = $( this ),
$reset_variations = $form.find( '.reset_variations' );
$form.find( '.variations select' ).each( function() {
var attribute_name = $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' );
if ( $( this ).val().length === 0 ) {
all_attributes_chosen = false;
} else {
some_attributes_chosen = true;
}
} )
if ( exclude && attribute_name === exclude ) {
all_attributes_chosen = false;
current_settings[ attribute_name ] = '';
} else {
// Add to settings array
current_settings[ attribute_name ] = $( this ).val();
}
});
// Reset product image
.on( 'reset_image', function( event ) {
var matching_variations = wc_variation_form_matcher.find_matching_variations( $product_variations, current_settings );
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_title' ),
o_href = $product_link.attr( 'data-o_href' );
if ( all_attributes_chosen ) {
if ( o_src !== undefined ) {
$product_img
.attr( 'src', o_src );
var variation = matching_variations.shift();
if ( variation ) {
$form.find( 'input[name="variation_id"], input.variation_id' )
.val( variation.variation_id )
.change();
$form.triggerHandler( 'found_variation', [ variation ] );
} else {
// Nothing found - reset fields
$form.find( '.variations select' ).val( '' );
if ( ! focus ) {
$form.triggerHandler( 'reset_data' );
}
alert( wc_add_to_cart_variation_params.i18n_no_matching_variations_text );
}
if ( o_href !== undefined ) {
$product_link
.attr( 'href', o_href );
} else {
$form.triggerHandler( 'update_variation_values', [ matching_variations ] );
if ( ! focus ) {
$form.triggerHandler( 'reset_data' );
}
if ( o_title !== undefined ) {
$product_img
.attr( 'title', o_title );
$product_link
.attr( 'title', o_title );
if ( ! exclude ) {
$form.find( '.single_variation_wrap' ).slideUp( 200 ).triggerHandler( 'hide_variation' );
}
}
if ( some_attributes_chosen ) {
if ( $reset_variations.css( 'visibility' ) === 'hidden' ) {
$reset_variations.css( 'visibility', 'visible' ).hide().fadeIn();
}
} else {
$reset_variations.css( 'visibility', 'hidden' );
}
} )
// 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 ) {
current_attr_select = $( el );
// Reset options
if ( ! current_attr_select.data( 'attribute_options' ) ) {
current_attr_select.data( 'attribute_options', current_attr_select.find( 'option:gt(0)' ).get() );
}
if ( o_alt !== undefined ) {
$product_img
.attr( 'alt', o_alt );
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' );
// 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' );
}
} )
// Disable option fields that are unavaiable for current set of attributes
.on( 'update_variation_values', function( event, variations ) {
// Loop through variations
for ( var num in variations ) {
$variation_form = $( this ).closest( '.variations_form' );
if ( typeof( variations[ num ] ) != 'undefined' ) {
// Loop through selects and disable/enable options based on selections
$variation_form.find( '.variations select' ).each( function( index, el ) {
var attributes = variations[ num ].attributes;
current_attr_select = $( el );
for ( var attr_name in attributes ) {
if ( attributes.hasOwnProperty( attr_name ) ) {
var attr_val = attributes[ attr_name ];
// Reset options
if ( ! current_attr_select.data( 'attribute_options' ) )
current_attr_select.data( 'attribute_options', current_attr_select.find( 'option:gt(0)' ).get() );
if ( attr_name == current_attr_name ) {
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' );
if ( variations[ num ].variation_is_active )
variation_active = 'enabled';
else
variation_active = '';
current_attr_select.find( 'option:gt(0)' ).removeClass( 'enabled' );
current_attr_select.find( 'option:gt(0)' ).removeAttr( 'disabled' );
if ( attr_val ) {
// 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' );
// Decode entities
attr_val = $( '<div/>' ).html( attr_val ).text();
// Loop through variations
for ( var num in variations ) {
// Add slashes
attr_val = attr_val.replace( /'/g, "\\'" );
attr_val = attr_val.replace( /"/g, "\\\"" );
if ( typeof( variations[ num ] ) != 'undefined' ) {
// Compare the meerkat
current_attr_select.find( 'option[value="' + attr_val + '"]' ).addClass( 'attached ' + variation_active );
var attributes = variations[ num ].attributes;
} else {
for ( var attr_name in attributes ) {
if ( attributes.hasOwnProperty( attr_name ) ) {
var attr_val = attributes[ attr_name ];
current_attr_select.find( 'option:gt(0)' ).addClass( 'attached ' + variation_active );
if ( attr_name == current_attr_name ) {
if ( variations[ num ].variation_is_active )
variation_active = 'enabled';
else
variation_active = '';
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 meerkat
current_attr_select.find( 'option[value="' + attr_val + '"]' ).addClass( 'attached ' + variation_active );
} else {
current_attr_select.find( 'option:gt(0)' ).addClass( 'attached ' + variation_active );
}
}
}
}
}
}
// 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' );
});
// 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_caption = variation.image_caption,
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 );
}
if ( o_href === undefined ) {
o_href = ( ! $product_link.attr( 'href' ) ) ? '' : $product_link.attr( 'href' );
$product_link.attr( 'data-o_href', o_href );
}
// Detach unattached
current_attr_select.find( 'option:gt(0):not(.attached)' ).remove();
if ( o_title === undefined ) {
o_title = ( ! $product_img.attr( 'title' ) ) ? '' : $product_img.attr( 'title' );
$product_img.attr( 'data-o_title', o_title );
}
if ( o_alt === undefined ) {
o_alt = ( ! $product_img.attr( 'alt' ) ) ? '' : $product_img.attr( 'alt' );
$product_img.attr( 'data-o_alt', o_alt );
}
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 );
}
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' );
if ( ! $sku.attr( 'data-o_sku' ) )
$sku.attr( 'data-o_sku', $sku.text() );
if ( ! $weight.attr( 'data-o_weight' ) )
$weight.attr( 'data-o_weight', $weight.text() );
if ( ! $dimensions.attr( 'data-o_dimensions' ) )
$dimensions.attr( 'data-o_dimensions', $dimensions.text() );
if ( variation.sku ) {
$sku.text( variation.sku );
} else {
$sku.text( $sku.attr( 'data-o_sku' ) );
}
if ( variation.weight ) {
$weight.text( variation.weight );
} else {
$weight.text( $weight.attr( 'data-o_weight' ) );
}
if ( variation.dimensions ) {
$dimensions.text( variation.dimensions );
} else {
$dimensions.text( $dimensions.attr( 'data-o_dimensions' ) );
}
$single_variation_wrap.find( '.quantity' ).show();
if ( ! variation.is_purchasable || ! variation.is_in_stock || ! variation.variation_is_visible ) {
$variation_form.find( '.variations_button' ).hide();
}
if ( ! variation.variation_is_visible ) {
$variation_form.find( '.single_variation' ).html( '<p>' + wc_add_to_cart_variation_params.i18n_unavailable_text + '</p>' );
}
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' );
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' );
if ( variation.is_sold_individually === 'yes' ) {
$single_variation_wrap.find( '.quantity input.qty' ).val( '1' );
$single_variation_wrap.find( '.quantity' ).hide();
}
$single_variation_wrap.slideDown( 200 ).trigger( 'show_variation', [ variation ] );
// Grey out disabled
current_attr_select.find( 'option:gt(0):not(.enabled)' ).attr( 'disabled', 'disabled' );
});
$form.trigger( 'wc_variation_form' );
// Custom event for when variations have been updated
$form.triggerHandler( 'woocommerce_update_variation_values' );
});
$form.triggerHandler( 'wc_variation_form' );
return $form;
};
/**
* 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];
var variation_id = variation.variation_id;
if ( wc_variation_form_matcher.variations_match( variation.attributes, settings ) ) {
matching.push( variation );
}
}
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;
}
}
}
return match;
}
};
$( function() {
// wc_add_to_cart_variation_params is required to continue, ensure the object exists
if ( typeof wc_add_to_cart_variation_params === 'undefined' )
return false;
$( '.variations_form' ).wc_variation_form();
$( '.variations_form .variations select' ).change();
if ( typeof wc_add_to_cart_variation_params !== 'undefined' ) {
$( '.variations_form' ).wc_variation_form().find('.variations select:eq(0)').change();
}
});
})( jQuery, window, document );
})( jQuery, window, document );

File diff suppressed because one or more lines are too long

View File

@ -28,7 +28,7 @@ jQuery( function( $ ) {
$( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
// Ajax action
$.post( wc_add_to_cart_params.wc_ajax_url + 'add_to_cart', data, function( response ) {
$.post( wc_add_to_cart_params.wc_ajax_url + 'add_to_cart/', data, function( response ) {
if ( ! response )
return;

View File

@ -1 +1 @@
jQuery(function(a){return"undefined"==typeof wc_add_to_cart_params?!1:void a(document).on("click",".add_to_cart_button",function(){var b=a(this);if(b.is(".product_type_simple")){if(!b.attr("data-product_id"))return!0;b.removeClass("added"),b.addClass("loading");var c={};return a.each(b.data(),function(a,b){c[a]=b}),a(document.body).trigger("adding_to_cart",[b,c]),a.post(wc_add_to_cart_params.wc_ajax_url+"add_to_cart",c,function(c){if(c){var d=window.location.toString();return d=d.replace("add-to-cart","added-to-cart"),c.error&&c.product_url?void(window.location=c.product_url):"yes"===wc_add_to_cart_params.cart_redirect_after_add?void(window.location=wc_add_to_cart_params.cart_url):(b.removeClass("loading"),fragments=c.fragments,cart_hash=c.cart_hash,fragments&&a.each(fragments,function(b){a(b).addClass("updating")}),a(".shop_table.cart, .updating, .cart_totals").fadeTo("400","0.6").block({message:null,overlayCSS:{opacity:.6}}),b.addClass("added"),wc_add_to_cart_params.is_cart||0!==b.parent().find(".added_to_cart").size()||b.after(' <a href="'+wc_add_to_cart_params.cart_url+'" class="added_to_cart wc-forward" title="'+wc_add_to_cart_params.i18n_view_cart+'">'+wc_add_to_cart_params.i18n_view_cart+"</a>"),fragments&&a.each(fragments,function(b,c){a(b).replaceWith(c)}),a(".widget_shopping_cart, .updating").stop(!0).css("opacity","1").unblock(),a(".shop_table.cart").load(d+" .shop_table.cart:eq(0) > *",function(){a(".shop_table.cart").stop(!0).css("opacity","1").unblock(),a(document.body).trigger("cart_page_refreshed")}),a(".cart_totals").load(d+" .cart_totals:eq(0) > *",function(){a(".cart_totals").stop(!0).css("opacity","1").unblock()}),a(document.body).trigger("added_to_cart",[fragments,cart_hash,b]),void 0)}}),!1}return!0})});
jQuery(function(a){return"undefined"==typeof wc_add_to_cart_params?!1:void a(document).on("click",".add_to_cart_button",function(){var b=a(this);if(b.is(".product_type_simple")){if(!b.attr("data-product_id"))return!0;b.removeClass("added"),b.addClass("loading");var c={};return a.each(b.data(),function(a,b){c[a]=b}),a(document.body).trigger("adding_to_cart",[b,c]),a.post(wc_add_to_cart_params.wc_ajax_url+"add_to_cart/",c,function(c){if(c){var d=window.location.toString();return d=d.replace("add-to-cart","added-to-cart"),c.error&&c.product_url?void(window.location=c.product_url):"yes"===wc_add_to_cart_params.cart_redirect_after_add?void(window.location=wc_add_to_cart_params.cart_url):(b.removeClass("loading"),fragments=c.fragments,cart_hash=c.cart_hash,fragments&&a.each(fragments,function(b){a(b).addClass("updating")}),a(".shop_table.cart, .updating, .cart_totals").fadeTo("400","0.6").block({message:null,overlayCSS:{opacity:.6}}),b.addClass("added"),wc_add_to_cart_params.is_cart||0!==b.parent().find(".added_to_cart").size()||b.after(' <a href="'+wc_add_to_cart_params.cart_url+'" class="added_to_cart wc-forward" title="'+wc_add_to_cart_params.i18n_view_cart+'">'+wc_add_to_cart_params.i18n_view_cart+"</a>"),fragments&&a.each(fragments,function(b,c){a(b).replaceWith(c)}),a(".widget_shopping_cart, .updating").stop(!0).css("opacity","1").unblock(),a(".shop_table.cart").load(d+" .shop_table.cart:eq(0) > *",function(){a(".shop_table.cart").stop(!0).css("opacity","1").unblock(),a(document.body).trigger("cart_page_refreshed")}),a(".cart_totals").load(d+" .cart_totals:eq(0) > *",function(){a(".cart_totals").stop(!0).css("opacity","1").unblock()}),a(document.body).trigger("added_to_cart",[fragments,cart_hash,b]),void 0)}}),!1}return!0})});

View File

@ -18,7 +18,7 @@ jQuery( function( $ ) {
}
var $fragment_refresh = {
url: wc_cart_fragments_params.wc_ajax_url + 'get_refreshed_fragments',
url: wc_cart_fragments_params.wc_ajax_url + 'get_refreshed_fragments/',
type: 'POST',
success: function( data ) {
if ( data && data.fragments ) {

View File

@ -1 +1 @@
jQuery(function(a){if("undefined"==typeof wc_cart_fragments_params)return!1;var b;try{b="sessionStorage"in window&&null!==window.sessionStorage,window.sessionStorage.setItem("wc","test"),window.sessionStorage.removeItem("wc")}catch(c){b=!1}var d={url:wc_cart_fragments_params.wc_ajax_url+"get_refreshed_fragments",type:"POST",success:function(c){c&&c.fragments&&(a.each(c.fragments,function(b,c){a(b).replaceWith(c)}),b&&(sessionStorage.setItem(wc_cart_fragments_params.fragment_name,JSON.stringify(c.fragments)),sessionStorage.setItem("wc_cart_hash",c.cart_hash)),a(document.body).trigger("wc_fragments_refreshed"))}};if(b){a(document.body).bind("added_to_cart",function(a,b,c){sessionStorage.setItem(wc_cart_fragments_params.fragment_name,JSON.stringify(b)),sessionStorage.setItem("wc_cart_hash",c)});try{var e=a.parseJSON(sessionStorage.getItem(wc_cart_fragments_params.fragment_name)),f=sessionStorage.getItem("wc_cart_hash"),g=a.cookie("woocommerce_cart_hash");if((null===f||void 0===f||""===f)&&(f=""),(null===g||void 0===g||""===g)&&(g=""),!e||!e["div.widget_shopping_cart_content"]||f!==g)throw"No fragment";a.each(e,function(b,c){a(b).replaceWith(c)}),a(document.body).trigger("wc_fragments_loaded")}catch(c){a.ajax(d)}}else a.ajax(d);a.cookie("woocommerce_items_in_cart")>0?a(".hide_cart_widget_if_empty").closest(".widget_shopping_cart").show():a(".hide_cart_widget_if_empty").closest(".widget_shopping_cart").hide(),a(document.body).bind("adding_to_cart",function(){a(".hide_cart_widget_if_empty").closest(".widget_shopping_cart").show()})});
jQuery(function(a){if("undefined"==typeof wc_cart_fragments_params)return!1;var b;try{b="sessionStorage"in window&&null!==window.sessionStorage,window.sessionStorage.setItem("wc","test"),window.sessionStorage.removeItem("wc")}catch(c){b=!1}var d={url:wc_cart_fragments_params.wc_ajax_url+"get_refreshed_fragments/",type:"POST",success:function(c){c&&c.fragments&&(a.each(c.fragments,function(b,c){a(b).replaceWith(c)}),b&&(sessionStorage.setItem(wc_cart_fragments_params.fragment_name,JSON.stringify(c.fragments)),sessionStorage.setItem("wc_cart_hash",c.cart_hash)),a(document.body).trigger("wc_fragments_refreshed"))}};if(b){a(document.body).bind("added_to_cart",function(a,b,c){sessionStorage.setItem(wc_cart_fragments_params.fragment_name,JSON.stringify(b)),sessionStorage.setItem("wc_cart_hash",c)});try{var e=a.parseJSON(sessionStorage.getItem(wc_cart_fragments_params.fragment_name)),f=sessionStorage.getItem("wc_cart_hash"),g=a.cookie("woocommerce_cart_hash");if((null===f||void 0===f||""===f)&&(f=""),(null===g||void 0===g||""===g)&&(g=""),!e||!e["div.widget_shopping_cart_content"]||f!==g)throw"No fragment";a.each(e,function(b,c){a(b).replaceWith(c)}),a(document.body).trigger("wc_fragments_loaded")}catch(c){a.ajax(d)}}else a.ajax(d);a.cookie("woocommerce_items_in_cart")>0?a(".hide_cart_widget_if_empty").closest(".widget_shopping_cart").show():a(".hide_cart_widget_if_empty").closest(".widget_shopping_cart").hide(),a(document.body).bind("adding_to_cart",function(){a(".hide_cart_widget_if_empty").closest(".widget_shopping_cart").show()})});

View File

@ -30,7 +30,7 @@ jQuery( function( $ ) {
shipping_method: shipping_methods
};
$.post( wc_cart_params.wc_ajax_url + 'update_shipping_method', data, function( response ) {
$.post( wc_cart_params.wc_ajax_url + 'update_shipping_method/', data, function( response ) {
$( 'div.cart_totals' ).replaceWith( response );
$( document.body ).trigger( 'updated_shipping_method' );
});

View File

@ -1 +1 @@
jQuery(function(a){return"undefined"==typeof wc_cart_params?!1:(a(document).on("click",".shipping-calculator-button",function(){return a(".shipping-calculator-form").slideToggle("slow"),!1}).on("change","select.shipping_method, input[name^=shipping_method]",function(){var b=[];a("select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]").each(function(){b[a(this).data("index")]=a(this).val()}),a("div.cart_totals").block({message:null,overlayCSS:{background:"#fff",opacity:.6}});var c={security:wc_cart_params.update_shipping_method_nonce,shipping_method:b};a.post(wc_cart_params.wc_ajax_url+"update_shipping_method",c,function(b){a("div.cart_totals").replaceWith(b),a(document.body).trigger("updated_shipping_method")})}),void a(".shipping-calculator-form").hide())});
jQuery(function(a){return"undefined"==typeof wc_cart_params?!1:(a(document).on("click",".shipping-calculator-button",function(){return a(".shipping-calculator-form").slideToggle("slow"),!1}).on("change","select.shipping_method, input[name^=shipping_method]",function(){var b=[];a("select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]").each(function(){b[a(this).data("index")]=a(this).val()}),a("div.cart_totals").block({message:null,overlayCSS:{background:"#fff",opacity:.6}});var c={security:wc_cart_params.update_shipping_method_nonce,shipping_method:b};a.post(wc_cart_params.wc_ajax_url+"update_shipping_method/",c,function(b){a("div.cart_totals").replaceWith(b),a(document.body).trigger("updated_shipping_method")})}),void a(".shipping-calculator-form").hide())});

View File

@ -241,7 +241,7 @@ jQuery( function( $ ) {
wc_checkout_form.xhr = $.ajax({
type: 'POST',
url: wc_checkout_params.wc_ajax_url + 'update_order_review',
url: wc_checkout_params.wc_ajax_url + 'update_order_review/',
data: data,
success: function( data ) {
// Always update the fragments
@ -408,7 +408,7 @@ jQuery( function( $ ) {
$.ajax({
type: 'POST',
url: wc_checkout_params.wc_ajax_url + 'apply_coupon',
url: wc_checkout_params.wc_ajax_url + 'apply_coupon/',
data: data,
success: function( code ) {
$( '.woocommerce-error, .woocommerce-message' ).remove();
@ -447,7 +447,7 @@ jQuery( function( $ ) {
$.ajax({
type: 'POST',
url: wc_checkout_params.wc_ajax_url + 'remove_coupon',
url: wc_checkout_params.wc_ajax_url + 'remove_coupon/',
data: data,
success: function( code ) {
$( '.woocommerce-error, .woocommerce-message' ).remove();

File diff suppressed because one or more lines are too long

View File

@ -30,7 +30,7 @@ jQuery( function( $ ) {
}
var $geolocate_customer = {
url: wc_geolocation_params.wc_ajax_url + 'get_customer_location',
url: wc_geolocation_params.wc_ajax_url + 'get_customer_location/',
type: 'GET',
success: function( response ) {
if ( response.success && response.data.hash && response.data.hash !== wc_geolocation_params.hash ) {

View File

@ -1 +1 @@
jQuery(function(a){var b=window.location.toString(),c=function(){wc_geolocation_params.hash&&a("a[href^='"+wc_geolocation_params.home_url+"']:not(a[href*='v=']), a[href^='/']:not(a[href*='v='])").each(function(){var b=a(this),c=b.attr("href");c.indexOf("?")>0?b.attr("href",c+"&v="+wc_geolocation_params.hash):b.attr("href",c+"?v="+wc_geolocation_params.hash)})},d=function(a){b=b.indexOf("?v=")>0||b.indexOf("&v=")>0?b.replace(/v=[^&]+/,"v="+a):b.indexOf("?")>0?b+"&v="+a:b+"?v="+a,window.location=b},e={url:wc_geolocation_params.wc_ajax_url+"get_customer_location",type:"GET",success:function(a){a.success&&a.data.hash&&a.data.hash!==wc_geolocation_params.hash&&d(a.data.hash)}};"1"!==wc_geolocation_params.is_checkout&&a.ajax(e),a(document.body).on("added_to_cart",function(){c()}),c()});
jQuery(function(a){var b=window.location.toString(),c=function(){wc_geolocation_params.hash&&a("a[href^='"+wc_geolocation_params.home_url+"']:not(a[href*='v=']), a[href^='/']:not(a[href*='v='])").each(function(){var b=a(this),c=b.attr("href");c.indexOf("?")>0?b.attr("href",c+"&v="+wc_geolocation_params.hash):b.attr("href",c+"?v="+wc_geolocation_params.hash)})},d=function(a){b=b.indexOf("?v=")>0||b.indexOf("&v=")>0?b.replace(/v=[^&]+/,"v="+a):b.indexOf("?")>0?b+"&v="+a:b+"?v="+a,window.location=b},e={url:wc_geolocation_params.wc_ajax_url+"get_customer_location/",type:"GET",success:function(a){a.success&&a.data.hash&&a.data.hash!==wc_geolocation_params.hash&&d(a.data.hash)}};"1"!==wc_geolocation_params.is_checkout&&a.ajax(e),a(document.body).on("added_to_cart",function(){c()}),c()});

View File

@ -84,6 +84,7 @@ class WC_AJAX {
'update_order_review' => true,
'add_to_cart' => true,
'checkout' => true,
'get_variation' => true,
'feature_product' => false,
'mark_order_status' => false,
'add_attribute' => false,
@ -433,6 +434,29 @@ class WC_AJAX {
die(0);
}
/**
* Get a matching variation based on posted attributes
*/
public static function get_variation() {
ob_start();
if ( empty( $_POST['product_id'] ) || ! ( $variable_product = wc_get_product( absint( $_POST['product_id'] ), array( 'product_type' => 'variable' ) ) ) ) {
die();
}
$variation_id = $variable_product->get_matching_variation( $_POST );
if ( $variation_id ) {
$variation = $variable_product->get_available_variation( $variation_id );
} else {
$variation = false;
}
wp_send_json( $variation );
die();
}
/**
* Feature a product from admin
*/

View File

@ -413,24 +413,62 @@ class WC_Product_Variable extends WC_Product {
* @return array
*/
public function get_variation_default_attributes() {
$default = isset( $this->default_attributes ) ? $this->default_attributes : '';
return apply_filters( 'woocommerce_product_default_attributes', (array) maybe_unserialize( $default ), $this );
}
/**
* Match a variation to a given set of attributes using a WP_Query
* @since 2.4.0
* @param $match_attributes
* @return int Variation ID which matched, 0 is no match was found
*/
public function get_matching_variation( $match_attributes = array() ) {
$query_args = array(
'post_parent' => $this->id,
'post_type' => 'product_variation',
'orderby' => 'menu_order',
'order' => 'ASC',
'fields' => 'ids',
'post_status' => 'publish',
'numberposts' => 1,
'meta_query' => array()
);
foreach ( $this->get_attributes() as $attribute ) {
if ( ! $attribute['is_variation'] ) {
continue;
}
$attribute_field_name = 'attribute_' . sanitize_title( $attribute['name'] );
if ( empty( $match_attributes[ $attribute_field_name ] ) ) {
return 0;
}
$query_args['meta_query'][] = array(
'key' => $attribute_field_name,
'value' => wc_clean( $match_attributes[ $attribute_field_name ] )
);
}
$matches = get_posts( $query_args );
if ( $matches && ! is_wp_error( $matches ) ) {
return current( $matches );
} else {
return 0;
}
}
/**
* Get an array of available variations for the current product.
*
* @access public
* @return array
*/
public function get_available_variations() {
$available_variations = array();
foreach ( $this->get_children() as $child_id ) {
$variation = $this->get_child( $child_id );
// Hide out of stock variations if 'Hide out of stock items from the catalog' is checked
@ -443,57 +481,66 @@ class WC_Product_Variable extends WC_Product {
continue;
}
$variation_attributes = $variation->get_variation_attributes();
$availability = $variation->get_availability();
$availability_html = empty( $availability['availability'] ) ? '' : '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . wp_kses_post( $availability['availability'] ) . '</p>';
$availability_html = apply_filters( 'woocommerce_stock_html', $availability_html, $availability['availability'], $variation );
if ( has_post_thumbnail( $variation->get_variation_id() ) ) {
$attachment_id = get_post_thumbnail_id( $variation->get_variation_id() );
$attachment = wp_get_attachment_image_src( $attachment_id, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image = $attachment ? current( $attachment ) : '';
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
$image_link = $attachment ? current( $attachment ) : '';
$image_title = get_the_title( $attachment_id );
$image_alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
} else {
$image = $image_link = $image_title = $image_alt = '';
}
$available_variations[] = apply_filters( 'woocommerce_available_variation', array(
'variation_id' => $child_id,
'variation_is_visible' => $variation->variation_is_visible(),
'variation_is_active' => $variation->variation_is_active(),
'is_purchasable' => $variation->is_purchasable(),
'display_price' => $variation->get_display_price(),
'display_regular_price' => $variation->get_display_price( $variation->get_regular_price() ),
'attributes' => $variation_attributes,
'image_src' => $image,
'image_link' => $image_link,
'image_title' => $image_title,
'image_alt' => $image_alt,
'price_html' => $variation->get_price() === "" || $this->get_variation_price( 'min' ) !== $this->get_variation_price( 'max' ) ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
'availability_html' => $availability_html,
'sku' => $variation->get_sku(),
'weight' => $variation->get_weight() . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) ),
'dimensions' => $variation->get_dimensions(),
'min_qty' => 1,
'max_qty' => $variation->backorders_allowed() ? '' : $variation->get_stock_quantity(),
'backorders_allowed' => $variation->backorders_allowed(),
'is_in_stock' => $variation->is_in_stock(),
'is_downloadable' => $variation->is_downloadable() ,
'is_virtual' => $variation->is_virtual(),
'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
'variation_description' => $variation->get_variation_description(),
), $this, $variation );
$available_variations[] = $this->get_available_variation( $variation );
}
return $available_variations;
}
/**
* Returns an array of date for a variation. Used in the add to cart form.
* @since 2.4.0
* @param $variation Variation product object or ID
* @return array
*/
public function get_available_variation( $variation ) {
if ( is_numeric( $variation ) ) {
$variation = $this->get_child( $variation );
}
if ( has_post_thumbnail( $variation->get_variation_id() ) ) {
$attachment_id = get_post_thumbnail_id( $variation->get_variation_id() );
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
$image = $attachment ? current( $attachment ) : '';
$image_link = $attachment ? current( $attachment ) : '';
$image_title = get_the_title( $attachment_id );
$image_alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
} else {
$image = $image_link = $image_title = $image_alt = '';
}
$availability = $variation->get_availability();
$availability_html = empty( $availability['availability'] ) ? '' : '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . wp_kses_post( $availability['availability'] ) . '</p>';
$availability_html = apply_filters( 'woocommerce_stock_html', $availability_html, $availability['availability'], $variation );
return apply_filters( 'woocommerce_available_variation', array(
'variation_id' => $variation->variation_id,
'variation_is_visible' => $variation->variation_is_visible(),
'variation_is_active' => $variation->variation_is_active(),
'is_purchasable' => $variation->is_purchasable(),
'display_price' => $variation->get_display_price(),
'display_regular_price' => $variation->get_display_price( $variation->get_regular_price() ),
'attributes' => $variation->get_variation_attributes(),
'image_src' => $image,
'image_link' => $image_link,
'image_title' => $image_title,
'image_alt' => $image_alt,
'price_html' => $variation->get_price() === "" || $this->get_variation_price( 'min' ) !== $this->get_variation_price( 'max' ) ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
'availability_html' => $availability_html,
'sku' => $variation->get_sku(),
'weight' => $variation->get_weight() . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) ),
'dimensions' => $variation->get_dimensions(),
'min_qty' => 1,
'max_qty' => $variation->backorders_allowed() ? '' : $variation->get_stock_quantity(),
'backorders_allowed' => $variation->backorders_allowed(),
'is_in_stock' => $variation->is_in_stock(),
'is_downloadable' => $variation->is_downloadable() ,
'is_virtual' => $variation->is_virtual(),
'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
'variation_description' => $variation->get_variation_description(),
), $this, $variation );
}
/**
* Sync variable product prices with the children lowest/highest prices.
*/

View File

@ -17,7 +17,7 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
class WC_Product_Variation extends WC_Product {
/** @public int ID of the variable product. */
/** @public int ID of the variation itself. */
public $variation_id;
/** @public object Parent Variable product object. */

View File

@ -881,10 +881,10 @@ if ( ! function_exists( 'woocommerce_variable_add_to_cart' ) ) {
// Load the template
wc_get_template( 'single-product/add-to-cart/variable.php', array(
'available_variations' => $product->get_available_variations(),
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_variation_default_attributes()
) );
'available_variations' => sizeof( $product->get_children() ) <= apply_filters( 'woocommerce_max_variations', 20 ) ? $product->get_available_variations() : false,
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_variation_default_attributes()
) );
}
}
if ( ! function_exists( 'woocommerce_external_add_to_cart' ) ) {

View File

@ -20,7 +20,7 @@ global $product, $post;
<?php do_action( 'woocommerce_before_variations_form' ); ?>
<?php if ( ! empty( $available_variations ) ) : ?>
<?php if ( 1 || ! empty( $available_variations ) ) : ?>
<table class="variations" cellspacing="0">
<tbody>
<?php $loop = 0; foreach ( $attributes as $name => $options ) : $loop++; ?>