2011-08-09 15:16:18 +00:00
|
|
|
<?php
|
2013-02-20 17:14:46 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/**
|
|
|
|
* Product Variation Class
|
2012-08-10 13:21:10 +00:00
|
|
|
*
|
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
|
2013-07-23 10:28:59 +00:00
|
|
|
* @version 2.1.0
|
2012-08-14 22:43:48 +00:00
|
|
|
* @package WooCommerce/Classes
|
2013-02-20 17:14:46 +00:00
|
|
|
* @category Class
|
2012-08-14 19:42:38 +00:00
|
|
|
* @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 {
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-12-14 21:54:13 +00:00
|
|
|
/** @public int ID of the variable product. */
|
2012-12-20 11:54:38 +00:00
|
|
|
public $variation_id;
|
2012-12-19 23:04:25 +00:00
|
|
|
|
|
|
|
/** @public object Parent Variable product object. */
|
2012-12-20 11:54:38 +00:00
|
|
|
public $parent;
|
|
|
|
|
|
|
|
/** @public array Stores variation data (attributes) for the current variation. */
|
|
|
|
public $variation_data = array();
|
2012-08-15 17:08:42 +00:00
|
|
|
|
2012-12-14 21:54:13 +00:00
|
|
|
/** @public bool True if the variation has a length. */
|
2012-12-19 23:04:25 +00:00
|
|
|
public $variation_has_length = false;
|
2012-08-15 17:08:42 +00:00
|
|
|
|
2012-12-14 21:54:13 +00:00
|
|
|
/** @public bool True if the variation has a width. */
|
2012-12-19 23:04:25 +00:00
|
|
|
public $variation_has_width = false;
|
2012-08-15 17:08:42 +00:00
|
|
|
|
2012-12-14 21:54:13 +00:00
|
|
|
/** @public bool True if the variation has a height. */
|
2012-12-19 23:04:25 +00:00
|
|
|
public $variation_has_height = false;
|
2012-08-15 17:08:42 +00:00
|
|
|
|
2012-12-14 21:54:13 +00:00
|
|
|
/** @public bool True if the variation has a weight. */
|
2012-12-19 23:04:25 +00:00
|
|
|
public $variation_has_weight = false;
|
2012-08-15 17:08:42 +00:00
|
|
|
|
2012-12-14 21:54:13 +00:00
|
|
|
/** @public bool True if the variation has stock and is managing stock. */
|
2012-12-19 23:04:25 +00:00
|
|
|
public $variation_has_stock = false;
|
2012-08-15 17:08:42 +00:00
|
|
|
|
2012-12-14 21:54:13 +00:00
|
|
|
/** @public bool True if the variation has a sku. */
|
2012-12-19 23:04:25 +00:00
|
|
|
public $variation_has_sku = false;
|
2012-08-15 17:08:42 +00:00
|
|
|
|
2012-12-14 21:54:13 +00:00
|
|
|
/** @public string Stores the shipping class of the variation. */
|
2012-12-19 23:04:25 +00:00
|
|
|
public $variation_shipping_class = false;
|
2012-08-15 17:08:42 +00:00
|
|
|
|
2012-12-14 21:54:13 +00:00
|
|
|
/** @public int Stores the shipping class ID of the variation. */
|
2012-12-19 23:04:25 +00:00
|
|
|
public $variation_shipping_class_id = false;
|
2012-08-15 17:08:42 +00:00
|
|
|
|
2012-12-14 21:54:13 +00:00
|
|
|
/** @public bool True if the variation has a tax class. */
|
2012-12-19 23:04:25 +00:00
|
|
|
public $variation_has_tax_class = false;
|
2012-11-29 16:48:40 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/**
|
|
|
|
* Loads all product data from custom fields
|
|
|
|
*
|
2012-08-14 19:42:38 +00:00
|
|
|
* @access public
|
2012-10-18 10:33:47 +00:00
|
|
|
* @param int $variation_id ID of the variation to load
|
2012-11-22 11:48:16 +00:00
|
|
|
* @param array $args Array of the arguments containing parent product data
|
2012-08-14 19:42:38 +00:00
|
|
|
* @return void
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
2012-12-14 21:54:13 +00:00
|
|
|
public function __construct( $variation, $args = array() ) {
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2013-01-17 11:23:11 +00:00
|
|
|
$this->product_type = 'variation';
|
2012-12-20 11:54:38 +00:00
|
|
|
|
2012-11-21 18:07:45 +00:00
|
|
|
if ( is_object( $variation ) ) {
|
|
|
|
$this->variation_id = absint( $variation->ID );
|
|
|
|
} else {
|
|
|
|
$this->variation_id = absint( $variation );
|
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
/* Get main product data from parent (args) */
|
|
|
|
$this->id = ! empty( $args['parent_id'] ) ? intval( $args['parent_id'] ) : wp_get_post_parent_id( $this->variation_id );
|
2012-11-27 16:16:17 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
// The post doesn't have a parent id, therefore its invalid.
|
2012-12-19 23:04:25 +00:00
|
|
|
if ( empty( $this->id ) )
|
|
|
|
return false;
|
2012-11-27 16:16:17 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
// Get post data
|
|
|
|
$this->parent = ! empty( $args['parent'] ) ? $args['parent'] : get_product( $this->id );
|
2013-01-11 18:30:05 +00:00
|
|
|
$this->post = ! empty( $this->parent->post ) ? $this->parent->post : array();
|
2012-12-19 21:01:15 +00:00
|
|
|
$this->product_custom_fields = get_post_meta( $this->variation_id );
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
// Get the variation attributes from meta
|
|
|
|
foreach ( $this->product_custom_fields as $name => $value ) {
|
2012-12-19 23:04:25 +00:00
|
|
|
if ( ! strstr( $name, 'attribute_' ) )
|
|
|
|
continue;
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2013-03-07 19:34:29 +00:00
|
|
|
$this->variation_data[ $name ] = sanitize_title( $value[0] );
|
2012-08-14 19:42:38 +00:00
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2012-12-19 23:04:25 +00:00
|
|
|
// Now get variation meta to override the parent variable product
|
2012-11-29 16:48:40 +00:00
|
|
|
if ( ! empty( $this->product_custom_fields['_sku'][0] ) ) {
|
2011-08-09 15:16:18 +00:00
|
|
|
$this->variation_has_sku = true;
|
2012-11-29 16:48:40 +00:00
|
|
|
$this->sku = $this->product_custom_fields['_sku'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
if ( isset( $this->product_custom_fields['_stock'][0] ) && $this->product_custom_fields['_stock'][0] !== '' ) {
|
2011-08-09 15:16:18 +00:00
|
|
|
$this->variation_has_stock = true;
|
2012-11-29 16:48:40 +00:00
|
|
|
$this->manage_stock = 'yes';
|
|
|
|
$this->stock = $this->product_custom_fields['_stock'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
if ( isset( $this->product_custom_fields['_weight'][0] ) && $this->product_custom_fields['_weight'][0] !== '' ) {
|
2011-08-09 15:16:18 +00:00
|
|
|
$this->variation_has_weight = true;
|
2012-11-29 16:48:40 +00:00
|
|
|
$this->weight = $this->product_custom_fields['_weight'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
if ( isset( $this->product_custom_fields['_length'][0] ) && $this->product_custom_fields['_length'][0] !== '' ) {
|
2011-11-28 13:13:49 +00:00
|
|
|
$this->variation_has_length = true;
|
2012-11-29 16:48:40 +00:00
|
|
|
$this->length = $this->product_custom_fields['_length'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
if ( isset( $this->product_custom_fields['_width'][0] ) && $this->product_custom_fields['_width'][0] !== '' ) {
|
2011-11-28 13:13:49 +00:00
|
|
|
$this->variation_has_width = true;
|
2012-11-29 16:48:40 +00:00
|
|
|
$this->width = $this->product_custom_fields['_width'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
if ( isset( $this->product_custom_fields['_height'][0] ) && $this->product_custom_fields['_height'][0] !== '' ) {
|
2011-11-28 13:13:49 +00:00
|
|
|
$this->variation_has_height = true;
|
2012-11-29 16:48:40 +00:00
|
|
|
$this->height = $this->product_custom_fields['_height'][0];
|
2012-03-21 18:37:57 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
if ( isset( $this->product_custom_fields['_downloadable'][0] ) && $this->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
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
if ( isset( $this->product_custom_fields['_virtual'][0] ) && $this->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
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
if ( isset( $this->product_custom_fields['_tax_class'][0] ) ) {
|
2012-04-11 17:56:54 +00:00
|
|
|
$this->variation_has_tax_class = true;
|
2012-11-29 16:48:40 +00:00
|
|
|
$this->tax_class = $this->product_custom_fields['_tax_class'][0];
|
2012-04-11 17:56:54 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-01-16 12:10:51 +00:00
|
|
|
if ( isset( $this->product_custom_fields['_sale_price_dates_from'][0] ) )
|
|
|
|
$this->sale_price_dates_from = $this->product_custom_fields['_sale_price_dates_from'][0];
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-01-16 12:10:51 +00:00
|
|
|
if ( isset( $this->product_custom_fields['_sale_price_dates_to'][0] ) )
|
|
|
|
$this->sale_price_dates_from = $this->product_custom_fields['_sale_price_dates_to'][0];
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-01-16 12:10:51 +00:00
|
|
|
// Prices
|
|
|
|
$this->price = isset( $this->product_custom_fields['_price'][0] ) ? $this->product_custom_fields['_price'][0] : '';
|
|
|
|
$this->regular_price = isset( $this->product_custom_fields['_regular_price'][0] ) ? $this->product_custom_fields['_regular_price'][0] : '';
|
|
|
|
$this->sale_price = isset( $this->product_custom_fields['_sale_price'][0] ) ? $this->product_custom_fields['_sale_price'][0] : '';
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-10-08 11:51:00 +00:00
|
|
|
// Backwards compat for prices
|
2013-01-16 12:10:51 +00:00
|
|
|
if ( $this->price !== '' && $this->regular_price == '' ) {
|
2012-10-08 11:51:00 +00:00
|
|
|
update_post_meta( $this->variation_id, '_regular_price', $this->price );
|
2013-01-16 12:10:51 +00:00
|
|
|
$this->regular_price = $this->price;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-01-16 12:10:51 +00:00
|
|
|
if ( $this->sale_price !== '' && $this->sale_price < $this->regular_price ) {
|
2012-10-08 11:51:00 +00:00
|
|
|
update_post_meta( $this->variation_id, '_price', $this->sale_price );
|
|
|
|
$this->price = $this->sale_price;
|
|
|
|
}
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
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
|
|
|
|
2013-01-16 12:10:51 +00:00
|
|
|
/**
|
|
|
|
* Returns whether or not the product post exists.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-07-23 10:28:59 +00:00
|
|
|
public function exists() {
|
2013-01-16 12:10:51 +00:00
|
|
|
return empty( $this->id ) ? false : true;
|
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
2013-06-14 11:21:20 +00:00
|
|
|
* Checks if this particular variation is visible (variations with no price, or out of stock, can be hidden)
|
2012-08-14 19:42:38 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-06-14 11:21:20 +00:00
|
|
|
public function variation_is_visible() {
|
2012-06-29 17:56:42 +00:00
|
|
|
$visible = true;
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2013-06-14 11:21:20 +00:00
|
|
|
// Published
|
|
|
|
if ( get_post_status( $this->variation_id ) != 'publish' )
|
|
|
|
$visible = false;
|
|
|
|
|
2012-06-29 17:56:42 +00:00
|
|
|
// Out of stock visibility
|
2013-06-14 11:21:20 +00:00
|
|
|
elseif ( get_option('woocommerce_hide_out_of_stock_items') == 'yes' && ! $this->is_in_stock() )
|
2012-06-29 17:56:42 +00:00
|
|
|
$visible = false;
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2013-01-16 12:10:51 +00:00
|
|
|
// Price not set
|
2013-07-23 10:28:59 +00:00
|
|
|
elseif ( $this->get_price() == "" )
|
2013-01-16 12:10:51 +00:00
|
|
|
$visible = false;
|
|
|
|
|
2013-06-14 11:21:20 +00:00
|
|
|
return apply_filters( 'woocommerce_variation_is_visible', $visible, $this->variation_id, $this->id );
|
2012-06-29 17:56:42 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
|
|
|
* Returns whether or not the variations parent is visible.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-12-14 21:54:13 +00:00
|
|
|
public function parent_is_visible() {
|
2013-06-14 11:21:20 +00:00
|
|
|
return $this->is_visible();
|
2012-06-29 17:56:42 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2011-08-22 11:57:50 +00:00
|
|
|
/**
|
|
|
|
* Get variation ID
|
2012-08-10 13:21:10 +00:00
|
|
|
*
|
2011-08-22 11:57:50 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2012-12-14 21:54:13 +00:00
|
|
|
public function get_variation_id() {
|
2012-11-19 14:05:03 +00:00
|
|
|
return absint( $this->variation_id );
|
2011-08-22 11:57:50 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2011-08-22 11:57:50 +00:00
|
|
|
/**
|
|
|
|
* Get variation attribute values
|
2012-08-10 13:21:10 +00:00
|
|
|
*
|
2011-08-22 11:57:50 +00:00
|
|
|
* @return array of attributes and their values for this variation
|
|
|
|
*/
|
2012-12-14 21:54:13 +00:00
|
|
|
public function get_variation_attributes() {
|
2011-08-22 11:57:50 +00:00
|
|
|
return $this->variation_data;
|
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2011-08-22 11:57:50 +00:00
|
|
|
/**
|
2013-01-16 12:10:51 +00:00
|
|
|
* Get variation price HTML. Prices are not inherited from parents.
|
2012-08-10 13:21:10 +00:00
|
|
|
*
|
2011-08-22 11:57:50 +00:00
|
|
|
* @return string containing the formatted price
|
|
|
|
*/
|
2013-08-07 14:38:31 +00:00
|
|
|
public function get_price_html( $price = '' ) {
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2013-07-23 10:28:59 +00:00
|
|
|
if ( $this->get_price() !== '' ) {
|
|
|
|
if ( $this->is_on_sale() ) {
|
2013-01-16 12:10:51 +00:00
|
|
|
|
2013-07-23 10:28:59 +00:00
|
|
|
$price = '<del>' . woocommerce_price( $this->get_regular_price() ) . '</del> <ins>' . woocommerce_price( $this->get_sale_price() ) . '</ins>';
|
2013-01-16 12:10:51 +00:00
|
|
|
$price = apply_filters( 'woocommerce_variation_sale_price_html', $price, $this );
|
|
|
|
|
2013-07-23 10:28:59 +00:00
|
|
|
} elseif ( $this->get_price() > 0 ) {
|
2013-01-16 12:10:51 +00:00
|
|
|
|
2013-07-23 10:28:59 +00:00
|
|
|
$price = woocommerce_price( $this->get_price() );
|
2013-01-16 12:10:51 +00:00
|
|
|
$price = apply_filters( 'woocommerce_variation_price_html', $price, $this );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$price = __( 'Free!', 'woocommerce' );
|
|
|
|
$price = apply_filters( 'woocommerce_variation_free_price_html', $price, $this );
|
|
|
|
|
|
|
|
}
|
2012-10-08 11:51:00 +00:00
|
|
|
} else {
|
2013-01-16 12:10:51 +00:00
|
|
|
$price = apply_filters( 'woocommerce_variation_empty_price_html', '', $this );
|
2012-10-08 11:51:00 +00:00
|
|
|
}
|
2013-01-16 12:10:51 +00:00
|
|
|
|
2013-07-26 13:47:23 +00:00
|
|
|
return apply_filters( 'woocommerce_get_variation_price_html', $price, $this );
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
|
|
|
* Gets the main product image.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $size (default: 'shop_thumbnail')
|
|
|
|
* @return string
|
2012-08-10 13:21:10 +00:00
|
|
|
*/
|
2012-12-14 21:54:13 +00:00
|
|
|
public function get_image( $size = 'shop_thumbnail', $attr = array() ) {
|
2011-11-09 15:39:14 +00:00
|
|
|
global $woocommerce;
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-08-12 11:41:26 +00:00
|
|
|
$image = '';
|
|
|
|
|
|
|
|
if ( $this->variation_id && has_post_thumbnail( $this->variation_id ) ) {
|
2012-11-28 18:02:12 +00:00
|
|
|
$image = get_the_post_thumbnail( $this->variation_id, $size, $attr );
|
2012-08-12 11:41:26 +00:00
|
|
|
} elseif ( has_post_thumbnail( $this->id ) ) {
|
2012-11-28 18:02:12 +00:00
|
|
|
$image = get_the_post_thumbnail( $this->id, $size, $attr );
|
2012-08-12 11:41:26 +00:00
|
|
|
} elseif ( $parent_id = wp_get_post_parent_id( $this->id ) && has_post_thumbnail( $parent_id ) ) {
|
2012-11-28 18:02:12 +00:00
|
|
|
$image = get_the_post_thumbnail( $parent_id, $size , $attr);
|
2012-08-12 11:41:26 +00:00
|
|
|
} else {
|
2012-11-27 15:37:01 +00:00
|
|
|
$image = woocommerce_placeholder_img( $size );
|
2012-08-12 11:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $image;
|
2011-11-09 15:39:14 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-12-28 09:59:20 +00:00
|
|
|
/**
|
2013-08-13 15:56:09 +00:00
|
|
|
* Set stock level of the product variation.
|
2012-12-28 09:59:20 +00:00
|
|
|
*
|
2013-08-13 15:56:09 +00:00
|
|
|
* @param int $amount
|
|
|
|
* @param boolean $force_variation_stock If true, the variation's stock will be updated and not the parents.
|
2012-12-28 09:59:20 +00:00
|
|
|
*/
|
2013-08-13 15:56:09 +00:00
|
|
|
function set_stock( $amount = null, $force_variation_stock = false ) {
|
|
|
|
if ( is_null( $amount ) )
|
|
|
|
return;
|
2012-12-28 09:59:20 +00:00
|
|
|
|
2013-08-13 15:56:09 +00:00
|
|
|
if ( $amount === '' && $force_variation_stock ) {
|
|
|
|
|
|
|
|
// If amount is an empty string, stock management is being turned off at variation level
|
|
|
|
$this->variation_has_stock = false;
|
|
|
|
$this->stock = '';
|
|
|
|
unset( $this->manage_stock );
|
|
|
|
|
|
|
|
// Update meta
|
|
|
|
update_post_meta( $this->variation_id, '_stock', '' );
|
2012-12-28 09:59:20 +00:00
|
|
|
|
2013-08-13 15:56:09 +00:00
|
|
|
} elseif ( $this->variation_has_stock || $force_variation_stock ) {
|
2012-12-28 09:59:20 +00:00
|
|
|
|
2013-08-13 15:56:09 +00:00
|
|
|
// Update stock amount
|
|
|
|
$this->stock = intval( $amount );
|
2012-12-28 09:59:20 +00:00
|
|
|
|
2013-08-13 15:56:09 +00:00
|
|
|
// Update meta
|
|
|
|
update_post_meta( $this->variation_id, '_stock', $this->stock );
|
2012-12-28 09:59:20 +00:00
|
|
|
|
2013-08-13 15:56:09 +00:00
|
|
|
// Clear total stock transient
|
|
|
|
delete_transient( 'wc_product_total_stock_' . $this->id );
|
2012-12-28 09:59:20 +00:00
|
|
|
|
2013-08-13 15:56:09 +00:00
|
|
|
// Check parents out of stock attribute
|
|
|
|
if ( ! $this->is_in_stock() ) {
|
2012-12-28 09:59:20 +00:00
|
|
|
|
2013-08-13 15:56:09 +00:00
|
|
|
// Check parent
|
|
|
|
$parent_product = get_product( $this->id );
|
|
|
|
|
|
|
|
// Only continue if the parent has backorders off
|
|
|
|
if ( ! $parent_product->backorders_allowed() && $parent_product->get_total_stock() <= 0 )
|
|
|
|
$this->set_stock_status( 'outofstock' );
|
|
|
|
|
|
|
|
} elseif ( $this->is_in_stock() ) {
|
|
|
|
$this->set_stock_status( 'instock' );
|
2012-12-28 09:59:20 +00:00
|
|
|
}
|
2013-08-13 15:56:09 +00:00
|
|
|
|
|
|
|
// Trigger action
|
|
|
|
do_action( 'woocommerce_product_set_stock', $this );
|
|
|
|
|
|
|
|
return $this->get_stock_quantity();
|
|
|
|
|
2012-12-28 09:59:20 +00:00
|
|
|
} else {
|
2013-08-13 15:56:09 +00:00
|
|
|
|
2012-12-28 09:59:20 +00:00
|
|
|
return parent::set_stock( $amount );
|
2013-08-13 15:56:09 +00:00
|
|
|
|
2012-12-28 09:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/**
|
2012-08-14 19:42:38 +00:00
|
|
|
* Reduce stock level of the product.
|
2011-08-09 15:16:18 +00:00
|
|
|
*
|
2012-08-14 19:42:38 +00:00
|
|
|
* @param int $by (default: 1) Amount to reduce by
|
|
|
|
* @return int stock level
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
2012-12-14 21:54:13 +00:00
|
|
|
public function reduce_stock( $by = 1 ) {
|
2012-03-21 18:37:57 +00:00
|
|
|
if ( $this->variation_has_stock ) {
|
2013-08-13 15:56:09 +00:00
|
|
|
return $this->set_stock( $this->stock - $by );
|
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
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/**
|
2012-08-14 19:42:38 +00:00
|
|
|
* Increase stock level of the product.
|
2011-08-09 15:16:18 +00:00
|
|
|
*
|
2012-08-14 19:42:38 +00:00
|
|
|
* @param int $by (default: 1) Amount to increase by
|
|
|
|
* @return int stock level
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
2012-12-14 21:54:13 +00:00
|
|
|
public function increase_stock( $by = 1 ) {
|
2013-08-13 15:56:09 +00:00
|
|
|
if ( $this->variation_has_stock ) {
|
|
|
|
return $this->set_stock( $this->stock + $by );
|
|
|
|
} else {
|
2011-08-09 15:16:18 +00:00
|
|
|
return parent::increase_stock( $by );
|
2013-08-13 15:56:09 +00:00
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-02-08 14:39:31 +00:00
|
|
|
/**
|
2012-08-14 19:42:38 +00:00
|
|
|
* Get the shipping class, and if not set, get the shipping class of the parent.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
2012-02-08 14:39:31 +00:00
|
|
|
*/
|
2012-12-14 21:54:13 +00:00
|
|
|
public function get_shipping_class() {
|
2012-10-18 10:33:47 +00:00
|
|
|
if ( ! $this->variation_shipping_class ) {
|
2012-02-10 12:16:21 +00:00
|
|
|
$classes = get_the_terms( $this->variation_id, 'product_shipping_class' );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-10-18 10:33:47 +00:00
|
|
|
if ( $classes && ! is_wp_error( $classes ) ) {
|
|
|
|
$this->variation_shipping_class = esc_attr( current( $classes )->slug );
|
|
|
|
} else {
|
|
|
|
$this->variation_shipping_class = parent::get_shipping_class();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-08 14:39:31 +00:00
|
|
|
return $this->variation_shipping_class;
|
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
|
|
|
* Returns the product shipping class ID.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-12-14 21:54:13 +00:00
|
|
|
public function get_shipping_class_id() {
|
2012-06-26 12:17:08 +00:00
|
|
|
if ( ! $this->variation_shipping_class_id ) {
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-06-26 12:17:08 +00:00
|
|
|
$classes = get_the_terms( $this->variation_id, 'product_shipping_class' );
|
2012-08-10 13:21:10 +00:00
|
|
|
|
|
|
|
if ( $classes && ! is_wp_error( $classes ) )
|
|
|
|
$this->variation_shipping_class_id = current( $classes )->term_id;
|
|
|
|
else
|
2012-06-26 12:17:08 +00:00
|
|
|
$this->variation_shipping_class_id = parent::get_shipping_class_id();
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-06-26 12:17:08 +00:00
|
|
|
}
|
2012-11-19 14:05:03 +00:00
|
|
|
return absint( $this->variation_shipping_class_id );
|
2012-06-26 12:17:08 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2012-08-09 22:41:59 +00:00
|
|
|
/**
|
2012-08-28 15:21:54 +00:00
|
|
|
* Get file download path identified by $download_id
|
2012-11-27 16:22:47 +00:00
|
|
|
*
|
2012-08-28 15:21:54 +00:00
|
|
|
* @access public
|
|
|
|
* @param string $download_id file identifier
|
|
|
|
* @return array
|
2012-08-09 22:41:59 +00:00
|
|
|
*/
|
2012-12-14 21:54:13 +00:00
|
|
|
public function get_file_download_path( $download_id ) {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-08-28 15:21:54 +00:00
|
|
|
$file_path = '';
|
|
|
|
$file_paths = apply_filters( 'woocommerce_file_download_paths', get_post_meta( $this->variation_id, '_file_paths', true ), $this->variation_id, null, null );
|
|
|
|
|
|
|
|
if ( ! $download_id && count( $file_paths ) == 1 ) {
|
|
|
|
// backwards compatibility for old-style download URLs and template files
|
|
|
|
$file_path = array_shift( $file_paths );
|
|
|
|
} elseif ( isset( $file_paths[ $download_id ] ) ) {
|
|
|
|
$file_path = $file_paths[ $download_id ];
|
|
|
|
}
|
2012-08-09 22:41:59 +00:00
|
|
|
|
2012-08-28 15:21:54 +00:00
|
|
|
// allow overriding based on the particular file being requested
|
|
|
|
return apply_filters( 'woocommerce_file_download_path', $file_path, $this->variation_id, $download_id );
|
2012-08-09 22:41:59 +00:00
|
|
|
}
|
2013-03-27 07:57:52 +00:00
|
|
|
|
|
|
|
/**
|
2013-03-27 08:06:59 +00:00
|
|
|
* Get product name with extra details such as SKU, price and attributes. Used within admin.
|
2013-03-27 07:57:52 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $product
|
2013-03-27 08:06:59 +00:00
|
|
|
* @return string Formatted product name, including attributes and price
|
2013-03-27 07:57:52 +00:00
|
|
|
*/
|
2013-03-27 08:06:59 +00:00
|
|
|
public function get_formatted_name() {
|
2013-03-27 07:57:52 +00:00
|
|
|
|
|
|
|
if ( $this->get_sku() )
|
|
|
|
$identifier = $this->get_sku();
|
|
|
|
else
|
|
|
|
$identifier = '#' . $this->variation_id;
|
|
|
|
|
|
|
|
$attributes = $this->get_variation_attributes();
|
|
|
|
$extra_data = ' – ' . implode( ', ', $attributes ) . ' – ' . woocommerce_price( $this->get_price() );
|
|
|
|
|
|
|
|
return sprintf( __( '%s – %s%s', 'woocommerce' ), $identifier, $this->get_title(), $extra_data );
|
|
|
|
}
|
2013-08-07 14:38:31 +00:00
|
|
|
}
|