Magic methods, avoid loading all meta on construct

This commit is contained in:
Mike Jolley 2012-12-19 23:04:25 +00:00
parent f972501dfa
commit 41f573a71f
6 changed files with 120 additions and 335 deletions

View File

@ -14,97 +14,16 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
abstract class WC_Product {
/** @var int The product (post) ID. */
var $id;
/** @var array Array of custom fields (meta) containing product data. */
var $product_custom_fields;
public $id = 0;
/** @var array Array of product attributes. */
var $attributes;
public $attributes = '';
/** @var object The actual post object. */
var $post;
/** @var string "Yes" for downloadable products. */
var $downloadable;
/** @var string "Yes" for virtual products. */
var $virtual;
/** @var string The product SKU (stock keeping unit). */
var $sku;
/** @var string The product price. */
var $price;
/** @var string The product's visibility. */
var $visibility;
/** @var string The product's stock level (if applicable). */
var $stock;
/** @var string The product's stock status (instock or outofstock). */
var $stock_status;
/** @var string The product's backorder status. */
var $backorders;
/** @var bool True if the product is stock managed. */
var $manage_stock;
/** @var string The product's sale price. */
var $sale_price;
/** @var string The product's regular non-sale price. */
var $regular_price;
/** @var string The product's weight. */
var $weight;
/** @var string The product's length. */
var $length;
/** @var string The product's width. */
var $width;
/** @var string The product's height. */
var $height;
/** @var string The product's tax status. */
var $tax_status;
/** @var string The product's tax class. */
var $tax_class;
/** @var array Array of product ID's being up-sold. */
var $upsell_ids;
/** @var array Array of product ID's being cross-sold. */
var $crosssell_ids;
public $post = '';
/** @var string The product's type (simple, variable etc). */
var $product_type;
/** @var string Date a sale starts. */
var $sale_price_dates_from;
/** @var string Data a sale ends. */
var $sale_price_dates_to;
/** @var string "Yes" for featured products. */
var $featured;
/** @var string Shipping class slug for the product. */
var $shipping_class;
/** @var int Shipping class ID for the product. */
var $shipping_class_id;
/** @var string Formatted LxWxH. */
var $dimensions;
/** @var string "Yes" if sold individually. */
var $sold_individually;
public $product_type = '';
/**
* __construct function.
@ -112,38 +31,59 @@ abstract class WC_Product {
* @access public
* @param mixed $product
*/
function __construct( $product ) {
public function __construct( $product ) {
if ( is_object( $product ) ) {
$this->id = absint( $product->ID );
$this->id = absint( $product->ID );
$this->post = $product;
} else {
$this->id = absint( $product );
$this->id = absint( $product );
$this->post = get_post( $this->id );
}
$this->product_custom_fields = get_post_custom( $this->id );
}
/**
* Load the data from the custom fields
* __isset function.
*
* @access public
* @param mixed $fields
* @param string $data (default: '')
* @return void
* @param mixed $key
* @return bool
*/
function load_product_data( $fields, $data = '' ) {
if ( ! $data )
$data = $this->product_custom_fields;
if ( $fields )
foreach ( $fields as $key => $default )
$this->$key = isset( $data[ '_' . $key ][0] ) && $data[ '_' . $key ][0] !== '' ? $data[ '_' . $key ][0] : $default;
public function __isset( $key ) {
return metadata_exists( 'post', $this->id, '_' . $key );
}
/**
* __get function.
*
* @access public
* @param mixed $key
* @return mixed
*/
public function __get( $key ) {
if ( in_array( $key, array( 'downloadable', 'virtual', 'backorders', 'manage_stock', 'featured', 'sold_individually' ) ) )
$value = ( $value = get_post_meta( $this->id, '_' . $key, true ) ) ? $value : 'no';
elseif ( 'visibility' == $key )
$value = ( $value = get_post_meta( $this->id, '_visibility', true ) ) ? $value : 'hidden';
elseif ( 'stock' == $key )
$value = ( $value = get_post_meta( $this->id, '_stock', true ) ) ? $value : 0;
elseif ( 'stock_status' == $key )
$value = ( $value = get_post_meta( $this->id, '_stock_status', true ) ) ? $value : 'instock';
elseif ( 'tax_status' == $key )
$value = ( $value = get_post_meta( $this->id, '_visibility', true ) ) ? $value : 'taxable';
elseif ( 'upsell_ids' == $key )
$value = ( $value = get_post_meta( $this->id, '_upsell_ids', true ) ) ? $value : array();
elseif ( 'crosssell_ids' == $key )
$value = ( $value = get_post_meta( $this->id, '_crosssell_ids', true ) ) ? $value : array();
else {
$value = get_post_meta( $this->id, '_' . $key, true );
}
if ( $value )
$this->$key = $value;
return $value;
}
/**
* Get SKU (Stock-keeping unit) - product unique ID.
@ -292,7 +232,7 @@ abstract class WC_Product {
*/
function get_file_download_path( $download_id ) {
$file_paths = isset( $this->product_custom_fields['_file_paths'][0] ) ? $this->product_custom_fields['_file_paths'][0] : '';
$file_paths = isset( $this->file_paths ) ? $this->file_paths : '';
$file_paths = maybe_unserialize( $file_paths );
$file_paths = apply_filters( 'woocommerce_file_download_paths', $file_paths, $this->id, null, null );
@ -1125,11 +1065,7 @@ abstract class WC_Product {
function get_attributes() {
if ( ! is_array( $this->attributes ) ) {
if ( isset( $this->product_custom_fields['_product_attributes'][0] ) )
$this->attributes = maybe_unserialize( maybe_unserialize( $this->product_custom_fields['_product_attributes'][0] ));
else
$this->attributes = array();
$this->attributes = isset( $this->product_attributes ) ? maybe_unserialize( maybe_unserialize( $this->product_attributes ) ) : array();
}
return (array) $this->attributes;

View File

@ -13,38 +13,17 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Product_External extends WC_Product_Simple {
/** @var string URL to external product. */
public $product_url;
/** @var string Text for the buy/link button. */
public $button_text;
/** @var string The product's type. */
public $product_type = 'external';
/**
* __construct function.
*
* @access public
* @param mixed $product
* @param array $args Contains arguments to set up this product
*/
public function __construct( $product, $args ) {
parent::__construct( $product, $args );
$this->product_type = 'external';
$this->downloadable = 'no';
$this->virtual = 'no';
$this->stock = '';
$this->stock_status = 'instock';
$this->manage_stock = 'no';
$this->weight = '';
$this->length = '';
$this->width = '';
$this->height = '';
$this->load_product_data( array(
'product_url' => '',
'button_text' => 'no'
) );
public function __construct( $product ) {
parent::__construct( $product );
}
/**

View File

@ -13,46 +13,23 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Product_Grouped extends WC_Product {
/** @var array Array of child products/posts/variations. */
public $children;
/** @var string The product's type. */
public $product_type = 'grouped';
/** @var string The product's total stock, including that of its children. */
public $total_stock;
/** @public array Array of child products/posts/variations. */
public $children = '';
/** @public string The product's total stock, including that of its children. */
public $total_stock = '';
/**
* __construct function.
*
* @access public
* @param mixed $product
* @param array $args Contains arguments to set up this product
*/
public function __construct( $product, $args ) {
public function __construct( $product ) {
parent::__construct( $product );
$this->product_type = 'grouped';
$this->product_custom_fields = get_post_meta( $this->id );
$this->downloadable = 'no';
$this->virtual = 'no';
$this->stock = '';
$this->stock_status = 'instock';
$this->manage_stock = 'no';
$this->weight = '';
$this->length = '';
$this->width = '';
$this->height = '';
// Load data from custom fields
$this->load_product_data( array(
'sku' => '',
'price' => '',
'visibility' => 'hidden',
'sale_price' => '',
'regular_price' => '',
'upsell_ids' => array(),
'crosssell_ids' => array(),
'featured' => 'no'
) );
}
/**
@ -65,7 +42,7 @@ class WC_Product_Grouped extends WC_Product {
*/
public function get_total_stock() {
if ( is_null( $this->total_stock ) ) {
if ( empty( $this->total_stock ) ) {
$transient_name = 'wc_product_total_stock_' . $this->id;

View File

@ -13,45 +13,17 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Product_Simple extends WC_Product {
/** @var string The product's type. */
public $product_type = 'simple';
/**
* __construct function.
*
* @access public
* @param mixed $product
* @param array $args Contains arguments to set up this product
*/
public function __construct( $product, $args ) {
public function __construct( $product ) {
parent::__construct( $product );
$this->product_type = 'simple';
// Load data from custom fields
$this->load_product_data( array(
'sku' => '',
'downloadable' => 'no',
'virtual' => 'no',
'price' => '',
'visibility' => 'hidden',
'stock' => 0,
'stock_status' => 'instock',
'backorders' => 'no',
'manage_stock' => 'no',
'sale_price' => '',
'regular_price' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'upsell_ids' => array(),
'crosssell_ids' => array(),
'sale_price_dates_from' => '',
'sale_price_dates_to' => '',
'featured' => 'no',
'sold_individually' => 'no'
) );
}
/**
@ -71,7 +43,6 @@ class WC_Product_Simple extends WC_Product {
return apply_filters( 'woocommerce_product_title', apply_filters( 'the_title', $title, $this->id ), $this );
}
/**
* Sync grouped products with the childs lowest price (so they can be sorted by price accurately).
*

View File

@ -13,72 +13,23 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Product_Variable extends WC_Product {
/** @var string The product's type. */
public $product_type = 'variable';
/** @public array Array of child products/posts/variations. */
public $children;
public $children = '';
/** @public string The product's total stock, including that of its children. */
public $total_stock;
/** @public string Used for variation prices. */
public $min_variation_price;
/** @public string Used for variation prices. */
public $max_variation_price;
/** @public string Used for variation prices. */
public $min_variation_regular_price;
/** @public string Used for variation prices. */
public $max_variation_regular_price;
/** @public string Used for variation prices. */
public $min_variation_sale_price;
/** @public string Used for variation prices. */
public $max_variation_sale_price;
public $total_stock = '';
/**
* __construct function.
*
* @access public
* @param mixed $product
* @param array $args Contains arguments to set up this product
*/
public function __construct( $product, $args ) {
public function __construct( $product ) {
parent::__construct( $product );
$this->product_type = 'variable';
$this->product_custom_fields = get_post_meta( $this->id );
// Load data from custom fields
$this->load_product_data( array(
'sku' => '',
'price' => '',
'sale_price' => '',
'regular_price' => '',
'visibility' => 'hidden',
'stock' => 0,
'stock_status' => 'instock',
'backorders' => 'no',
'manage_stock' => 'no',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'upsell_ids' => array(),
'crosssell_ids' => array(),
'featured' => 'no',
'min_variation_price' => '',
'max_variation_price' => '',
'min_variation_regular_price' => '',
'max_variation_regular_price' => '',
'min_variation_sale_price' => '',
'max_variation_sale_price' => '',
'sold_individually' => 'no'
) );
}
/**
@ -91,7 +42,7 @@ class WC_Product_Variable extends WC_Product {
*/
public function get_total_stock() {
if ( is_null( $this->total_stock ) ) {
if ( empty( $this->total_stock ) ) {
$transient_name = 'wc_product_total_stock_' . $this->id;
@ -204,8 +155,7 @@ class WC_Product_Variable extends WC_Product {
public function get_child( $child_id ) {
return get_product( $child_id, array(
'parent_id' => $this->id,
'parent' => $this,
'meta' => $this->product_custom_fields
'parent' => $this
) );
}
@ -228,6 +178,7 @@ class WC_Product_Variable extends WC_Product {
* @return bool
*/
public function is_on_sale() {
if ( $this->has_child() ) {
foreach ( $this->get_children() as $child_id ) {
@ -389,7 +340,7 @@ class WC_Product_Variable extends WC_Product {
*/
public function get_variation_default_attributes() {
$default = isset( $this->product_custom_fields['_default_attributes'][0] ) ? $this->product_custom_fields['_default_attributes'][0] : '';
$default = isset( $this->default_attributes ) ? $this->default_attributes : '';
return apply_filters( 'woocommerce_product_default_attributes', (array) maybe_unserialize( $default ), $this );
}

View File

@ -14,53 +14,53 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Product_Variation extends WC_Product {
/** @var string The product's type. */
public $product_type = 'variable';
/** @public array Stores variation data (attributes) for the current variation. */
public $variation_data;
public $variation_data = array();
/** @public int ID of the variable product. */
public $variation_id;
/** @public bool True if the variation has a length. */
public $variation_has_length;
/** @public bool True if the variation has a width. */
public $variation_has_width;
/** @public bool True if the variation has a height. */
public $variation_has_height;
/** @public bool True if the variation has a weight. */
public $variation_has_weight;
/** @public bool True if the variation has a price. */
public $variation_has_price;
/** @public bool True if the variation has a regular price. */
public $variation_has_regular_price;
/** @public bool True if the variation has a sale price. */
public $variation_has_sale_price;
/** @public bool True if the variation has stock and is managing stock. */
public $variation_has_stock;
/** @public bool True if the variation has a sku. */
public $variation_has_sku;
/** @public string Stores the shipping class of the variation. */
public $variation_shipping_class;
/** @public int Stores the shipping class ID of the variation. */
public $variation_shipping_class_id;
/** @public bool True if the variation has a tax class. */
public $variation_has_tax_class;
/** @public array Array of custom fields (meta) containing product data. */
public $parent_custom_fields;
public $variation_id = 0;
/** @public object Parent Variable product object. */
public $parent;
public $parent = '';
/** @public bool True if the variation has a length. */
public $variation_has_length = false;
/** @public bool True if the variation has a width. */
public $variation_has_width = false;
/** @public bool True if the variation has a height. */
public $variation_has_height = false;
/** @public bool True if the variation has a weight. */
public $variation_has_weight = false;
/** @public bool True if the variation has a price. */
public $variation_has_price = false;
/** @public bool True if the variation has a regular price. */
public $variation_has_regular_price = false;
/** @public bool True if the variation has a sale price. */
public $variation_has_sale_price = false;
/** @public bool True if the variation has stock and is managing stock. */
public $variation_has_stock = false;
/** @public bool True if the variation has a sku. */
public $variation_has_sku = false;
/** @public string Stores the shipping class of the variation. */
public $variation_shipping_class = false;
/** @public int Stores the shipping class ID of the variation. */
public $variation_shipping_class_id = false;
/** @public bool True if the variation has a tax class. */
public $variation_has_tax_class = false;
/**
* Loads all product data from custom fields
@ -72,8 +72,6 @@ class WC_Product_Variation extends WC_Product {
*/
public function __construct( $variation, $args = array() ) {
$this->product_type = 'variable';
if ( is_object( $variation ) ) {
$this->variation_id = absint( $variation->ID );
} else {
@ -84,50 +82,23 @@ class WC_Product_Variation extends WC_Product {
$this->id = ! empty( $args['parent_id'] ) ? intval( $args['parent_id'] ) : wp_get_post_parent_id( $this->variation_id );
// The post doesn't have a parent id, therefore its invalid.
if ( empty( $this->id ) ) return false;
if ( empty( $this->id ) )
return false;
// Get post data
$this->parent = ! empty( $args['parent'] ) ? $args['parent'] : get_product( $this->id );
$this->post = $this->parent->post;
// Get custom fields
$this->product_custom_fields = get_post_meta( $this->variation_id );
$this->parent_custom_fields = ! empty( $args['meta'] ) ? $args['meta'] : get_post_meta( $this->id );
$this->load_product_data( array(
'sku' => '',
'price' => 0,
'visibility' => 'hidden',
'stock' => 0,
'stock_status' => 'instock',
'backorders' => 'no',
'manage_stock' => 'no',
'sale_price' => '',
'regular_price' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'upsell_ids' => array(),
'crosssell_ids' => array()
), $this->parent_custom_fields );
// Get the variation attributes from meta
$this->variation_data = array();
foreach ( $this->product_custom_fields as $name => $value ) {
if ( ! strstr( $name, 'attribute_' ) ) continue;
if ( ! strstr( $name, 'attribute_' ) )
continue;
$this->variation_data[ $name ] = $value[0];
}
// Now get variation meta and override the parent variable product
$this->variation_has_sku = $this->variation_has_stock = $this->variation_has_weight = $this->variation_has_length = $this->variation_has_width = $this->variation_has_height = $this->variation_has_price = $this->variation_has_regular_price = $this->variation_has_sale_price = false;
/* Override parent data with variation */
// Now get variation meta to override the parent variable product
if ( ! empty( $this->product_custom_fields['_sku'][0] ) ) {
$this->variation_has_sku = true;
$this->sku = $this->product_custom_fields['_sku'][0];