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
|
|
|
*
|
2012-01-27 16:38:39 +00:00
|
|
|
* @class WC_Product_Variation
|
2011-08-10 17:11:11 +00:00
|
|
|
* @package WooCommerce
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
2012-01-27 16:38:39 +00:00
|
|
|
class WC_Product_Variation extends WC_Product {
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
var $variation_data;
|
|
|
|
var $variation_id;
|
2011-11-28 13:13:49 +00:00
|
|
|
var $variation_has_length;
|
|
|
|
var $variation_has_width;
|
|
|
|
var $variation_has_height;
|
2011-08-09 15:16:18 +00:00
|
|
|
var $variation_has_weight;
|
|
|
|
var $variation_has_price;
|
|
|
|
var $variation_has_sale_price;
|
|
|
|
var $variation_has_stock;
|
|
|
|
var $variation_has_sku;
|
2012-02-08 14:39:31 +00:00
|
|
|
var $variation_shipping_class;
|
2012-06-26 12:17:08 +00:00
|
|
|
var $variation_shipping_class_id;
|
2012-04-11 17:56:54 +00:00
|
|
|
var $variation_has_tax_class;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads all product data from custom fields
|
|
|
|
*
|
|
|
|
* @param int $id ID of the product to load
|
|
|
|
*/
|
2012-01-27 16:38:39 +00:00
|
|
|
function __construct( $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 );
|
2012-04-20 10:17:40 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
$this->variation_data = array();
|
|
|
|
|
2012-03-21 18:37:57 +00:00
|
|
|
foreach ( $product_custom_fields as $name => $value ) :
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2012-03-21 18:37:57 +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(
|
2012-02-13 00:34:09 +00:00
|
|
|
'sku' => '',
|
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' => '',
|
2011-11-26 18:22:22 +00:00
|
|
|
'length' => '',
|
|
|
|
'width' => '',
|
|
|
|
'height' => '',
|
2011-08-20 15:41:42 +00:00
|
|
|
'tax_status' => 'taxable',
|
|
|
|
'tax_class' => '',
|
|
|
|
'upsell_ids' => array(),
|
|
|
|
'crosssell_ids' => array()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Load the data from the custom fields
|
2012-03-21 18:37:57 +00:00
|
|
|
foreach ( $load_data as $key => $default )
|
|
|
|
$this->$key = ( isset( $parent_custom_fields['_' . $key][0] ) && $parent_custom_fields['_' . $key][0] !== '' ) ? $parent_custom_fields['_' . $key][0] : $default;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
$this->product_type = 'variable';
|
2011-08-21 13:28:54 +00:00
|
|
|
|
2011-11-28 13:13:49 +00:00
|
|
|
$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_sale_price = false;
|
2011-08-20 15:41:42 +00:00
|
|
|
|
|
|
|
/* Override parent data with variation */
|
2012-03-21 18:37:57 +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-12-24 16:57:36 +00:00
|
|
|
$this->sku = $product_custom_fields['_sku'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2012-03-21 18:37:57 +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-12-24 16:57:36 +00:00
|
|
|
$this->stock = $product_custom_fields['_stock'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2012-03-21 18:37:57 +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-12-24 16:57:36 +00:00
|
|
|
$this->weight = $product_custom_fields['_weight'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2012-03-21 18:37:57 +00:00
|
|
|
if ( isset( $product_custom_fields['_length'][0] ) && $product_custom_fields['_length'][0] !== '' ) {
|
2011-11-28 13:13:49 +00:00
|
|
|
$this->variation_has_length = true;
|
2011-12-24 16:57:36 +00:00
|
|
|
$this->length = $product_custom_fields['_length'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-11-28 13:13:49 +00:00
|
|
|
|
2012-03-21 18:37:57 +00:00
|
|
|
if ( isset( $product_custom_fields['_width'][0] ) && $product_custom_fields['_width'][0] !== '' ) {
|
2011-11-28 13:13:49 +00:00
|
|
|
$this->variation_has_width = true;
|
2011-12-24 16:57:36 +00:00
|
|
|
$this->width = $product_custom_fields['_width'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-11-28 13:13:49 +00:00
|
|
|
|
2012-03-21 18:37:57 +00:00
|
|
|
if ( isset( $product_custom_fields['_height'][0] ) && $product_custom_fields['_height'][0] !== '' ) {
|
2011-11-28 13:13:49 +00:00
|
|
|
$this->variation_has_height = true;
|
2011-12-24 16:57:36 +00:00
|
|
|
$this->height = $product_custom_fields['_height'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-11-28 13:13:49 +00:00
|
|
|
|
2012-03-21 18:37:57 +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;
|
2011-12-24 16:57:36 +00:00
|
|
|
$this->price = $product_custom_fields['_price'][0];
|
|
|
|
$this->regular_price = $product_custom_fields['_price'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2012-03-21 18:37:57 +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-12-24 16:57:36 +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;
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-08-21 13:28:54 +00:00
|
|
|
|
2012-03-21 18:37:57 +00:00
|
|
|
if ( isset( $product_custom_fields['_downloadable'][0] ) && $product_custom_fields['_downloadable'][0] == 'yes' ) {
|
2011-11-09 23:06:17 +00:00
|
|
|
$this->downloadable = 'yes';
|
2012-03-21 18:37:57 +00:00
|
|
|
} else {
|
2011-11-09 23:06:17 +00:00
|
|
|
$this->downloadable = 'no';
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-11-09 23:06:17 +00:00
|
|
|
|
2012-03-21 18:37:57 +00:00
|
|
|
if ( isset( $product_custom_fields['_virtual'][0] ) && $product_custom_fields['_virtual'][0] == 'yes' ) {
|
2011-11-09 23:06:17 +00:00
|
|
|
$this->virtual = 'yes';
|
2012-03-21 18:37:57 +00:00
|
|
|
} else {
|
2011-11-09 23:06:17 +00:00
|
|
|
$this->virtual = 'no';
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-11-09 23:06:17 +00:00
|
|
|
|
2012-04-11 17:56:54 +00:00
|
|
|
if ( isset( $product_custom_fields['_tax_class'][0] ) ) {
|
|
|
|
$this->variation_has_tax_class = true;
|
|
|
|
$this->tax_class = $product_custom_fields['_tax_class'][0];
|
|
|
|
}
|
|
|
|
|
2011-08-21 13:28:54 +00:00
|
|
|
$this->total_stock = $this->stock;
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-06-29 17:56:42 +00:00
|
|
|
|
|
|
|
/** Returns whether or not the variation is visible */
|
|
|
|
function is_visible() {
|
|
|
|
|
|
|
|
$visible = true;
|
|
|
|
|
|
|
|
// Out of stock visibility
|
|
|
|
if ( get_option('woocommerce_hide_out_of_stock_items') == 'yes' && ! $this->is_in_stock() )
|
|
|
|
$visible = false;
|
|
|
|
|
|
|
|
return apply_filters('woocommerce_product_is_visible', $visible, $this->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns whether or not the variations parent is visible */
|
|
|
|
function parent_is_visible() {
|
|
|
|
return parent::is_visible();
|
|
|
|
}
|
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 = '';
|
|
|
|
|
2012-02-26 14:11:56 +00:00
|
|
|
if ($this->price!=='') :
|
2011-08-09 15:16:18 +00:00
|
|
|
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>';
|
2012-01-14 16:42:04 +00:00
|
|
|
$price = apply_filters('woocommerce_variation_sale_price_html', $price, $this);
|
2011-08-09 15:16:18 +00:00
|
|
|
else :
|
2011-08-10 17:11:11 +00:00
|
|
|
$price .= woocommerce_price( $this->price );
|
2012-01-14 16:42:04 +00:00
|
|
|
$price = apply_filters('woocommerce_variation_price_html', $price, $this);
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-09 15:39:14 +00:00
|
|
|
/**
|
|
|
|
* Gets the main product image
|
|
|
|
*/
|
|
|
|
function get_image( $size = 'shop_thumbnail' ) {
|
|
|
|
global $woocommerce;
|
|
|
|
|
|
|
|
if ($this->variation_id && has_post_thumbnail($this->variation_id)) :
|
|
|
|
echo get_the_post_thumbnail($this->variation_id, $size);
|
|
|
|
elseif (has_post_thumbnail($this->id)) :
|
|
|
|
echo get_the_post_thumbnail($this->id, $size);
|
|
|
|
elseif ($parent_id = wp_get_post_parent_id( $this->id ) && has_post_thumbnail($parent_id)) :
|
|
|
|
echo get_the_post_thumbnail($parent_id, $size);
|
|
|
|
else :
|
2012-02-24 16:23:08 +00:00
|
|
|
echo '<img src="'. woocommerce_placeholder_img_src() . '" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />';
|
2011-11-09 15:39:14 +00:00
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/**
|
|
|
|
* Reduce stock level of the product
|
|
|
|
*
|
|
|
|
* @param int $by Amount to reduce by
|
|
|
|
*/
|
|
|
|
function reduce_stock( $by = 1 ) {
|
2012-03-07 13:38:51 +00:00
|
|
|
global $woocommerce;
|
|
|
|
|
2012-03-21 18:37:57 +00:00
|
|
|
if ( $this->variation_has_stock ) {
|
|
|
|
if ( $this->managing_stock() ) {
|
2011-08-21 16:47:49 +00:00
|
|
|
|
2012-06-29 19:40:18 +00:00
|
|
|
$this->stock = $this->stock - $by;
|
|
|
|
$this->total_stock = $this->total_stock - $by;
|
2012-03-21 18:37:57 +00:00
|
|
|
update_post_meta( $this->variation_id, '_stock', $this->stock );
|
2012-06-29 19:40:18 +00:00
|
|
|
$woocommerce->clear_product_transients( $this->id ); // Clear transient
|
2011-08-21 16:47:49 +00:00
|
|
|
|
2012-06-29 19:40:18 +00:00
|
|
|
// Check parents out of stock attribute
|
2012-03-21 18:37:57 +00:00
|
|
|
if ( ! $this->is_in_stock() ) {
|
2011-08-21 16:47:49 +00:00
|
|
|
|
|
|
|
// Check parent
|
2012-01-27 16:38:39 +00:00
|
|
|
$parent_product = new WC_Product( $this->id );
|
2011-08-21 16:47:49 +00:00
|
|
|
|
2012-06-29 19:40:18 +00:00
|
|
|
// Only continue if the parent has backorders off
|
|
|
|
if ( ! $parent_product->backorders_allowed() && $parent_product->get_total_stock() <= 0 ) {
|
|
|
|
|
|
|
|
update_post_meta( $this->id, '_stock_status', 'outofstock' );
|
|
|
|
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-08-21 16:47:49 +00:00
|
|
|
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-08-21 16:47:49 +00:00
|
|
|
|
|
|
|
return $this->stock;
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-08-09 15:16:18 +00:00
|
|
|
return parent::reduce_stock( $by );
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increase stock level of the product
|
|
|
|
*
|
|
|
|
* @param int $by Amount to increase by
|
|
|
|
*/
|
|
|
|
function increase_stock( $by = 1 ) {
|
2012-06-29 19:40:18 +00:00
|
|
|
global $woocommerce;
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
if ($this->variation_has_stock) :
|
|
|
|
if ($this->managing_stock()) :
|
2011-08-21 16:47:49 +00:00
|
|
|
|
2012-06-29 19:40:18 +00:00
|
|
|
$this->stock = $this->stock + $by;
|
|
|
|
$this->total_stock = $this->total_stock + $by;
|
|
|
|
update_post_meta( $this->variation_id, '_stock', $this->stock );
|
|
|
|
$woocommerce->clear_product_transients( $this->id ); // Clear transient
|
2011-08-21 16:47:49 +00:00
|
|
|
|
|
|
|
// Parents out of stock attribute
|
2012-06-29 19:40:18 +00:00
|
|
|
if ( $this->is_in_stock() )
|
|
|
|
update_post_meta( $this->id, '_stock_status', 'instock' );
|
2011-08-21 16:47:49 +00:00
|
|
|
|
|
|
|
return $this->stock;
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
else :
|
|
|
|
return parent::increase_stock( $by );
|
|
|
|
endif;
|
|
|
|
}
|
2012-02-08 14:39:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the shipping class, and if not set, get the shipping class of the parent
|
|
|
|
*/
|
|
|
|
function get_shipping_class() {
|
|
|
|
if (!$this->variation_shipping_class) :
|
2012-02-10 12:16:21 +00:00
|
|
|
$classes = get_the_terms( $this->variation_id, 'product_shipping_class' );
|
2012-02-08 14:39:31 +00:00
|
|
|
if ($classes && !is_wp_error($classes)) $this->variation_shipping_class = current($classes)->slug; else $this->variation_shipping_class = parent::get_shipping_class();
|
|
|
|
endif;
|
|
|
|
return $this->variation_shipping_class;
|
|
|
|
}
|
2012-06-26 12:17:08 +00:00
|
|
|
|
|
|
|
/** Returns the product shipping class ID */
|
|
|
|
function get_shipping_class_id() {
|
|
|
|
if ( ! $this->variation_shipping_class_id ) {
|
|
|
|
|
|
|
|
$classes = get_the_terms( $this->variation_id, 'product_shipping_class' );
|
|
|
|
|
|
|
|
if ( $classes && ! is_wp_error( $classes ) )
|
|
|
|
$this->variation_shipping_class_id = current( $classes )->term_id;
|
|
|
|
else
|
|
|
|
$this->variation_shipping_class_id = parent::get_shipping_class_id();
|
|
|
|
|
|
|
|
}
|
|
|
|
return (int) $this->variation_shipping_class_id;
|
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2012-01-27 16:38:39 +00:00
|
|
|
}
|
|
|
|
|
2012-07-23 18:14:02 +00:00
|
|
|
/** Deprecated */
|
2012-01-27 16:38:39 +00:00
|
|
|
class woocommerce_product_variation extends WC_Product_Variation {
|
|
|
|
public function __construct( $variation_id, $parent_id = '', $parent_custom_fields = '' ) {
|
|
|
|
_deprecated_function( 'woocommerce_product_variation', '1.4', 'WC_Product_Variation()' );
|
|
|
|
parent::__construct( $variation_id, $parent_id, $parent_custom_fields );
|
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|