When handling variations, priority from top to bottom - disabled variations still taking into consideration allow you to exclude certain combinations when using 'all' for variations with lower priority. Message displayed if a variation is unavailable.

@coenjacobs please review
This commit is contained in:
Mike Jolley 2013-10-10 16:34:44 +01:00
parent eef86ab5eb
commit ece95e25ce
5 changed files with 55 additions and 31 deletions

View File

@ -386,6 +386,11 @@
$variation_form.find('.variations_button').hide(); $variation_form.find('.variations_button').hide();
} }
if ( ! variation.variation_is_visible ) {
$variation_form.find('.variations_button').hide();
$variation_form.find('.single_variation').html( '<p>' + wc_add_to_cart_variation_params.i18n_unavailable_text + '</p>' );
}
if ( variation.min_qty ) if ( variation.min_qty )
$single_variation_wrap.find('input[name=quantity]').attr( 'min', variation.min_qty ).val( variation.min_qty ); $single_variation_wrap.find('input[name=quantity]').attr( 'min', variation.min_qty ).val( variation.min_qty );
else else

File diff suppressed because one or more lines are too long

View File

@ -141,6 +141,7 @@ class WC_Frontend_Scripts {
wp_localize_script( 'wc-add-to-cart-variation', 'wc_add_to_cart_variation_params', apply_filters( 'wc_add_to_cart_variation_params', array( wp_localize_script( 'wc-add-to-cart-variation', 'wc_add_to_cart_variation_params', apply_filters( 'wc_add_to_cart_variation_params', array(
'i18n_no_matching_variations_text' => esc_attr__( 'Sorry, no products matched your selection. Please choose a different combination.', 'woocommerce' ), 'i18n_no_matching_variations_text' => esc_attr__( 'Sorry, no products matched your selection. Please choose a different combination.', 'woocommerce' ),
'i18n_unavailable_text' => esc_attr__( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' ),
) ) ); ) ) );
wp_localize_script( 'wc-country-select', 'wc_country_select_params', apply_filters( 'wc_country_select_params', array( wp_localize_script( 'wc-country-select', 'wc_country_select_params', apply_filters( 'wc_country_select_params', array(

View File

@ -286,17 +286,20 @@ class WC_Product_Variable extends WC_Product {
foreach ( $this->get_children() as $child_id ) { foreach ( $this->get_children() as $child_id ) {
if ( get_post_status( $child_id ) != 'publish' ) $variation = $this->get_child( $child_id );
continue; // Disabled
$child = $this->get_child( $child_id ); if ( ! empty( $variation->variation_id ) ) {
$child_variation_attributes = $child->get_variation_attributes(); if ( ! $variation->variation_is_visible() )
continue; // Disabled or hidden
$child_variation_attributes = $variation->get_variation_attributes();
foreach ( $child_variation_attributes as $name => $value ) foreach ( $child_variation_attributes as $name => $value )
if ( $name == $attribute_field_name ) if ( $name == $attribute_field_name )
$values[] = sanitize_title( $value ); $values[] = sanitize_title( $value );
} }
}
// empty value indicates that all options for given attribute are available // empty value indicates that all options for given attribute are available
if ( in_array( '', $values ) ) { if ( in_array( '', $values ) ) {
@ -361,10 +364,6 @@ class WC_Product_Variable extends WC_Product {
$variation = $this->get_child( $child_id ); $variation = $this->get_child( $child_id );
if ( ! empty( $variation->variation_id ) ) { if ( ! empty( $variation->variation_id ) ) {
if ( ! $variation->variation_is_visible() )
continue; // Disabled or hidden
$variation_attributes = $variation->get_variation_attributes(); $variation_attributes = $variation->get_variation_attributes();
$availability = $variation->get_availability(); $availability = $variation->get_availability();
$availability_html = empty( $availability['availability'] ) ? '' : apply_filters( 'woocommerce_stock_html', '<p class="stock ' . esc_attr( $availability['class'] ) . '">'. wp_kses_post( $availability['availability'] ).'</p>', wp_kses_post( $availability['availability'] ) ); $availability_html = empty( $availability['availability'] ) ? '' : apply_filters( 'woocommerce_stock_html', '<p class="stock ' . esc_attr( $availability['class'] ) . '">'. wp_kses_post( $availability['availability'] ).'</p>', wp_kses_post( $availability['availability'] ) );
@ -386,6 +385,7 @@ class WC_Product_Variable extends WC_Product {
$available_variations[] = apply_filters( 'woocommerce_available_variation', array( $available_variations[] = apply_filters( 'woocommerce_available_variation', array(
'variation_id' => $child_id, 'variation_id' => $child_id,
'variation_is_visible' => $variation->variation_is_visible(),
'attributes' => $variation_attributes, 'attributes' => $variation_attributes,
'image_src' => $image, 'image_src' => $image,
'image_link' => $image_link, 'image_link' => $image_link,

View File

@ -221,7 +221,7 @@ class WC_Product_Variation extends WC_Product {
public function variation_is_visible() { public function variation_is_visible() {
$visible = true; $visible = true;
// Published // Published == enabled checkbox
if ( get_post_status( $this->variation_id ) != 'publish' ) if ( get_post_status( $this->variation_id ) != 'publish' )
$visible = false; $visible = false;
@ -236,6 +236,24 @@ class WC_Product_Variation extends WC_Product {
return apply_filters( 'woocommerce_variation_is_visible', $visible, $this->variation_id, $this->id ); return apply_filters( 'woocommerce_variation_is_visible', $visible, $this->variation_id, $this->id );
} }
/**
* Returns false if the product cannot be bought.
*
* @access public
* @return cool
*/
public function is_purchasable() {
// Published == enabled checkbox
if ( get_post_status( $this->variation_id ) != 'publish' )
$purchasable = false;
else
$purchasable = parent::is_purchasable();
return $purchasable;
}
/** /**
* Returns whether or not the variations parent is visible. * Returns whether or not the variations parent is visible.
* *