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