2011-08-09 15:16:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Product Variation Class
|
|
|
|
*
|
2011-08-10 17:11:11 +00:00
|
|
|
* The WooCommerce product variation class handles product variation data.
|
2011-08-09 15:16:18 +00:00
|
|
|
*
|
2011-08-10 17:11:11 +00:00
|
|
|
* @class woocommerce_product_variation
|
|
|
|
* @package WooCommerce
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
2011-08-10 17:11:11 +00:00
|
|
|
class woocommerce_product_variation extends woocommerce_product {
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
var $variation_data;
|
|
|
|
var $variation_id;
|
|
|
|
var $variation_has_weight;
|
|
|
|
var $variation_has_price;
|
|
|
|
var $variation_has_sale_price;
|
|
|
|
var $variation_has_stock;
|
|
|
|
var $variation_has_sku;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads all product data from custom fields
|
|
|
|
*
|
|
|
|
* @param int $id ID of the product to load
|
|
|
|
*/
|
2011-10-26 18:45:38 +00:00
|
|
|
function woocommerce_product_variation( $variation_id, $parent_id = '', $parent_custom_fields = '' ) {
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
$this->variation_id = $variation_id;
|
|
|
|
|
|
|
|
$product_custom_fields = get_post_custom( $this->variation_id );
|
|
|
|
|
2011-10-26 18:45:38 +00:00
|
|
|
$this->exists = (sizeof($product_custom_fields)>0) ? true : false;
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
$this->variation_data = array();
|
|
|
|
|
|
|
|
foreach ($product_custom_fields as $name => $value) :
|
|
|
|
|
2011-09-11 13:28:15 +00:00
|
|
|
if (!strstr($name, 'attribute_')) continue;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
$this->variation_data[$name] = $value[0];
|
|
|
|
|
|
|
|
endforeach;
|
|
|
|
|
|
|
|
/* Get main product data from parent */
|
2011-10-26 18:45:38 +00:00
|
|
|
$this->id = ($parent_id>0) ? $parent_id : wp_get_post_parent_id( $this->variation_id );
|
|
|
|
if (!$parent_custom_fields) $parent_custom_fields = get_post_custom( $this->id );
|
2011-08-20 15:41:42 +00:00
|
|
|
|
2011-10-26 18:45:38 +00:00
|
|
|
// Define the data we're going to load from the parent: Key => Default value
|
2011-08-20 15:41:42 +00:00
|
|
|
$load_data = array(
|
|
|
|
'sku' => $this->id,
|
2011-11-05 19:03:03 +00:00
|
|
|
'downloadable' => 'no',
|
|
|
|
'virtual' => 'no',
|
2011-08-20 15:41:42 +00:00
|
|
|
'price' => 0,
|
|
|
|
'visibility' => 'hidden',
|
|
|
|
'stock' => 0,
|
|
|
|
'stock_status' => 'instock',
|
|
|
|
'backorders' => 'no',
|
|
|
|
'manage_stock' => 'no',
|
|
|
|
'sale_price' => '',
|
|
|
|
'regular_price' => '',
|
|
|
|
'weight' => '',
|
|
|
|
'tax_status' => 'taxable',
|
|
|
|
'tax_class' => '',
|
|
|
|
'upsell_ids' => array(),
|
|
|
|
'crosssell_ids' => array()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Load the data from the custom fields
|
|
|
|
foreach ($load_data as $key => $default) :
|
2011-10-31 14:09:18 +00:00
|
|
|
$this->$key = (isset($parent_custom_fields[$key][0]) && $parent_custom_fields[$key][0]!=='') ? $parent_custom_fields[$key][0] : $default;
|
2011-08-20 15:41:42 +00:00
|
|
|
endforeach;
|
|
|
|
|
|
|
|
// Load serialised data, unserialise twice to fix WP bug
|
|
|
|
if (isset($product_custom_fields['product_attributes'][0])) $this->attributes = maybe_unserialize( maybe_unserialize( $product_custom_fields['product_attributes'][0] )); else $this->attributes = array();
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
$this->product_type = 'variable';
|
2011-08-21 13:28:54 +00:00
|
|
|
|
|
|
|
$this->variation_has_sku = $this->variation_has_stock = $this->variation_has_weight = $this->variation_has_price = $this->variation_has_sale_price = false;
|
2011-08-20 15:41:42 +00:00
|
|
|
|
|
|
|
/* Override parent data with variation */
|
2011-08-17 23:42:07 +00:00
|
|
|
if (isset($product_custom_fields['sku'][0]) && !empty($product_custom_fields['sku'][0])) :
|
2011-08-09 15:16:18 +00:00
|
|
|
$this->variation_has_sku = true;
|
2011-08-17 23:42:07 +00:00
|
|
|
$this->sku = $product_custom_fields['sku'][0];
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
|
2011-08-20 21:03:31 +00:00
|
|
|
if (isset($product_custom_fields['stock'][0]) && $product_custom_fields['stock'][0]!=='') :
|
2011-08-09 15:16:18 +00:00
|
|
|
$this->variation_has_stock = true;
|
2011-08-21 13:28:54 +00:00
|
|
|
$this->manage_stock = 'yes';
|
2011-08-09 15:16:18 +00:00
|
|
|
$this->stock = $product_custom_fields['stock'][0];
|
|
|
|
endif;
|
|
|
|
|
2011-08-20 21:03:31 +00:00
|
|
|
if (isset($product_custom_fields['weight'][0]) && $product_custom_fields['weight'][0]!=='') :
|
2011-08-09 15:16:18 +00:00
|
|
|
$this->variation_has_weight = true;
|
2011-08-17 23:42:07 +00:00
|
|
|
$this->weight = $product_custom_fields['weight'][0];
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
|
2011-08-20 21:03:31 +00:00
|
|
|
if (isset($product_custom_fields['price'][0]) && $product_custom_fields['price'][0]!=='') :
|
2011-08-09 15:16:18 +00:00
|
|
|
$this->variation_has_price = true;
|
|
|
|
$this->price = $product_custom_fields['price'][0];
|
2011-08-20 15:41:42 +00:00
|
|
|
$this->regular_price = $product_custom_fields['price'][0];
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
|
2011-08-20 21:03:31 +00:00
|
|
|
if (isset($product_custom_fields['sale_price'][0]) && $product_custom_fields['sale_price'][0]!=='') :
|
2011-08-09 15:16:18 +00:00
|
|
|
$this->variation_has_sale_price = true;
|
2011-08-17 23:42:07 +00:00
|
|
|
$this->sale_price = $product_custom_fields['sale_price'][0];
|
2011-08-20 15:41:42 +00:00
|
|
|
if ($this->sale_price < $this->price) $this->price = $this->sale_price;
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
2011-08-21 13:28:54 +00:00
|
|
|
|
|
|
|
$this->total_stock = $this->stock;
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
|
|
|
|
2011-08-22 11:57:50 +00:00
|
|
|
/**
|
|
|
|
* Get variation ID
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function get_variation_id() {
|
|
|
|
return (int) $this->variation_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get variation attribute values
|
|
|
|
*
|
|
|
|
* @return array of attributes and their values for this variation
|
|
|
|
*/
|
|
|
|
function get_variation_attributes() {
|
|
|
|
return $this->variation_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get variation attribute values
|
|
|
|
*
|
|
|
|
* @return string containing the formatted price
|
|
|
|
*/
|
2011-08-09 15:16:18 +00:00
|
|
|
function get_price_html() {
|
2011-08-20 15:41:42 +00:00
|
|
|
if ($this->variation_has_price || $this->variation_has_sale_price) :
|
2011-08-09 15:16:18 +00:00
|
|
|
$price = '';
|
|
|
|
|
|
|
|
if ($this->price) :
|
|
|
|
if ($this->variation_has_sale_price) :
|
2011-08-20 15:41:42 +00:00
|
|
|
$price .= '<del>'.woocommerce_price( $this->regular_price ).'</del> <ins>'.woocommerce_price( $this->sale_price ).'</ins>';
|
2011-08-09 15:16:18 +00:00
|
|
|
else :
|
2011-08-10 17:11:11 +00:00
|
|
|
$price .= woocommerce_price( $this->price );
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
endif;
|
|
|
|
|
|
|
|
return $price;
|
|
|
|
else :
|
2011-08-10 17:11:11 +00:00
|
|
|
return woocommerce_price(parent::get_price());
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reduce stock level of the product
|
|
|
|
*
|
|
|
|
* @param int $by Amount to reduce by
|
|
|
|
*/
|
|
|
|
function reduce_stock( $by = 1 ) {
|
|
|
|
if ($this->variation_has_stock) :
|
|
|
|
if ($this->managing_stock()) :
|
2011-08-21 16:47:49 +00:00
|
|
|
|
|
|
|
$this->stock = $this->stock - $by;
|
|
|
|
$this->total_stock = $this->total_stock - $by;
|
|
|
|
update_post_meta($this->variation_id, 'stock', $this->stock);
|
|
|
|
|
|
|
|
// Parents out of stock attribute
|
|
|
|
if (!$this->is_in_stock()) :
|
|
|
|
|
|
|
|
// Check parent
|
|
|
|
$parent_product = &new woocommerce_product( $this->id );
|
|
|
|
|
|
|
|
if ($parent_product->managing_stock()) :
|
|
|
|
if (!$parent_product->backorders_allowed()) :
|
|
|
|
if ($parent_product->total_stock==0 || $parent_product->total_stock<0) :
|
|
|
|
update_post_meta($this->id, 'stock_status', 'outofstock');
|
|
|
|
endif;
|
|
|
|
endif;
|
|
|
|
else :
|
|
|
|
if ($parent_product->total_stock==0 || $parent_product->total_stock<0) :
|
|
|
|
update_post_meta($this->id, 'stock_status', 'outofstock');
|
|
|
|
endif;
|
|
|
|
endif;
|
|
|
|
|
|
|
|
endif;
|
|
|
|
|
|
|
|
return $this->stock;
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
else :
|
|
|
|
return parent::reduce_stock( $by );
|
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increase stock level of the product
|
|
|
|
*
|
|
|
|
* @param int $by Amount to increase by
|
|
|
|
*/
|
|
|
|
function increase_stock( $by = 1 ) {
|
|
|
|
if ($this->variation_has_stock) :
|
|
|
|
if ($this->managing_stock()) :
|
2011-08-21 16:47:49 +00:00
|
|
|
|
|
|
|
$this->stock = $this->stock + $by;
|
|
|
|
$this->total_stock = $this->total_stock + $by;
|
|
|
|
update_post_meta($this->variation_id, 'stock', $this->stock);
|
|
|
|
|
|
|
|
// Parents out of stock attribute
|
|
|
|
if ($this->is_in_stock()) update_post_meta($this->id, 'stock_status', 'instock');
|
|
|
|
|
|
|
|
return $this->stock;
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
else :
|
|
|
|
return parent::increase_stock( $by );
|
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|