Separate inherited meta data from variation level meta data to stop bubbling up.

Fixes #6065
This commit is contained in:
Mike Jolley 2014-08-28 15:38:51 +01:00
parent f84d90a1a0
commit 50a0577b81
1 changed files with 42 additions and 36 deletions

View File

@ -37,25 +37,29 @@ class WC_Product_Variation extends WC_Product {
public $variation_has_tax_class = true; public $variation_has_tax_class = true;
public $variation_has_downloadable_files = true; public $variation_has_downloadable_files = true;
/** @private array List of meta keys which apply to variations */ /** @private array Data which is only at variation level - no inheritance plus their default values if left blank. */
public $variation_level_meta_keys = array( private $variation_level_meta_data = array(
'manage_stock', 'downloadable' => 'no',
'stock_status', 'virtual' => 'no',
'stock', 'manage_stock' => 'no',
'backorders', 'sale_price_dates_from' => '',
'sku', 'sale_price_dates_to' => '',
'downloadable_files', 'price' => '',
'weight', 'regular_price' => '',
'length', 'sale_price' => '',
'height', 'stock' => 0,
'downloadable', 'stock_status' => 'instock',
'virtual', 'downloadable_files' => array()
'tax_class', );
'sale_price_dates_from',
'sale_price_dates_to', /** @private array Data which can be at variation level, otherwise fallback to parent if not set. */
'price', private $variation_inherited_meta_data = array(
'regular_price', 'tax_class' => '',
'sale_price' 'backorders' => 'no',
'sku' => '',
'weight' => '',
'length' => '',
'height' => ''
); );
/** /**
@ -93,7 +97,9 @@ class WC_Product_Variation extends WC_Product {
* @return bool * @return bool
*/ */
public function __isset( $key ) { public function __isset( $key ) {
if ( in_array( $key, $this->variation_level_meta_keys ) ) { if ( in_array( $key, array_keys( $this->variation_level_meta_data ) ) ) {
return metadata_exists( 'post', $this->variation_id, '_' . $key );
} elseif ( in_array( $key, array_keys( $this->variation_inherited_meta_data ) ) ) {
return metadata_exists( 'post', $this->variation_id, '_' . $key ) || metadata_exists( 'post', $this->id, '_' . $key ); return metadata_exists( 'post', $this->variation_id, '_' . $key ) || metadata_exists( 'post', $this->id, '_' . $key );
} else { } else {
return metadata_exists( 'post', $this->id, '_' . $key ); return metadata_exists( 'post', $this->id, '_' . $key );
@ -108,25 +114,25 @@ class WC_Product_Variation extends WC_Product {
* @return mixed * @return mixed
*/ */
public function __get( $key ) { public function __get( $key ) {
if ( in_array( $key, $this->variation_level_meta_keys ) ) { if ( in_array( $key, array_keys( $this->variation_level_meta_data ) ) ) {
// Get values or default if not set (no) $value = get_post_meta( $this->variation_id, '_' . $key, true );
if ( in_array( $key, array( 'downloadable', 'virtual', 'manage_stock' ) ) ) {
$value = ( $value = get_post_meta( $this->variation_id, '_' . $key, true ) ) ? $value : 'no'; if ( '' === $value ) {
$value = $this->variation_level_meta_data[ $key ];
}
} elseif ( in_array( $key, array_keys( $this->variation_inherited_meta_data ) ) ) {
// Data which must be set (not null), otherwise use parent data
} elseif ( in_array( $key , array( 'tax_class', 'backorders' ) ) ) {
$value = metadata_exists( 'post', $this->variation_id, '_' . $key ) ? get_post_meta( $this->variation_id, '_' . $key, true ) : get_post_meta( $this->id, '_' . $key, true ); $value = metadata_exists( 'post', $this->variation_id, '_' . $key ) ? get_post_meta( $this->variation_id, '_' . $key, true ) : get_post_meta( $this->id, '_' . $key, true );
// Data which is only at variation level - no inheritance // Handle meta data keys which can be empty at variation level to cause inheritance
} elseif ( in_array( $key , array( 'price', 'regular_price', 'sale_price', 'sale_price_dates_to', 'sale_price_dates_from' ) ) ) { if ( '' === $value && in_array( $key, array( 'sku', 'weight', 'length', 'height' ) ) ) {
$value = ( $value = get_post_meta( $this->variation_id, '_' . $key, true ) ) ? $value : ''; $value = get_post_meta( $this->id, '_' . $key, true );
}
} elseif ( 'stock' === $key ) { if ( '' === $value ) {
$value = ( $value = get_post_meta( $this->variation_id, '_stock', true ) ) ? $value : 0; $value = $this->variation_inherited_meta_data[ $key ];
} else {
$value = ( $value = get_post_meta( $this->variation_id, '_' . $key, true ) ) ? $value : get_post_meta( $this->id, '_' . $key, true );
} }
} elseif ( 'variation_data' === $key ) { } elseif ( 'variation_data' === $key ) {