2011-08-09 15:16:18 +00:00
|
|
|
<?php
|
2014-06-24 12:05:13 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2016-11-02 18:50:42 +00:00
|
|
|
exit;
|
2014-06-24 12:05:13 +00:00
|
|
|
}
|
2013-02-20 17:14:46 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/**
|
2016-11-09 12:26:46 +00:00
|
|
|
* Product Variation Class.
|
2016-11-02 18:50:42 +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
|
|
|
*
|
2014-09-26 10:45:15 +00:00
|
|
|
* @class WC_Product_Variation
|
2016-11-02 18:50:42 +00:00
|
|
|
* @version 2.7.0
|
2014-09-26 10:45:15 +00:00
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
2016-11-02 18:50:42 +00:00
|
|
|
class WC_Product_Variation extends WC_Product_Simple {
|
2013-09-20 16:01:09 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/**
|
2016-11-02 18:50:42 +00:00
|
|
|
* Post type.
|
|
|
|
* @var string
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
2016-11-11 14:31:15 +00:00
|
|
|
public $post_type = 'product_variation';
|
2016-08-01 15:24:31 +00:00
|
|
|
|
2016-11-09 12:26:46 +00:00
|
|
|
/**
|
|
|
|
* Parent data.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $parent_data = array(
|
|
|
|
'sku' => '',
|
|
|
|
'manage_stock' => '',
|
|
|
|
'stock_quantity' => '',
|
|
|
|
'weight' => '',
|
|
|
|
'length' => '',
|
|
|
|
'width' => '',
|
|
|
|
'height' => '',
|
|
|
|
'tax_class' => '',
|
|
|
|
);
|
|
|
|
|
2016-11-02 18:50:42 +00:00
|
|
|
/**
|
|
|
|
* Initialize simple product.
|
|
|
|
*
|
|
|
|
* @param mixed $product
|
|
|
|
*/
|
|
|
|
public function __construct( $product = 0 ) {
|
|
|
|
$this->internal_meta_keys[] = '_variation_description';
|
|
|
|
parent::__construct( $product );
|
2014-06-24 12:05:13 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2016-11-09 12:26:46 +00:00
|
|
|
/**
|
|
|
|
* Prefix for action and filter hooks on data.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_hook_prefix() {
|
|
|
|
return 'woocommerce_product_variation_get_';
|
|
|
|
}
|
|
|
|
|
2016-10-17 11:22:23 +00:00
|
|
|
/**
|
|
|
|
* Get internal type.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_type() {
|
|
|
|
return 'variation';
|
|
|
|
}
|
|
|
|
|
2013-01-16 12:10:51 +00:00
|
|
|
/**
|
2016-11-09 12:26:46 +00:00
|
|
|
* If the stock level comes from another product ID.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function get_stock_managed_by_id() {
|
|
|
|
return 'parent' === $this->get_manage_stock() ? $this->get_parent_id() : $this->get_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get variation attribute values.
|
2013-01-16 12:10:51 +00:00
|
|
|
*
|
2016-11-02 18:50:42 +00:00
|
|
|
* @return array of attributes and their values for this variation
|
2013-01-16 12:10:51 +00:00
|
|
|
*/
|
2016-11-02 18:50:42 +00:00
|
|
|
public function get_variation_attributes() {
|
2016-11-09 12:26:46 +00:00
|
|
|
return $this->get_attributes();
|
2013-01-16 12:10:51 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2013-09-23 14:47:47 +00:00
|
|
|
/**
|
|
|
|
* Wrapper for get_permalink. Adds this variations attributes to the URL.
|
2014-09-26 10:45:15 +00:00
|
|
|
*
|
2016-04-21 09:36:46 +00:00
|
|
|
* @param $item_object item array If a cart or order item is passed, we can get a link containing the exact attributes selected for the variation, rather than the default attributes.
|
2013-09-23 14:47:47 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-21 09:36:46 +00:00
|
|
|
public function get_permalink( $item_object = null ) {
|
2016-11-10 19:05:29 +00:00
|
|
|
$url = get_permalink( $this->get_parent_id() );
|
|
|
|
|
2016-04-21 09:36:46 +00:00
|
|
|
if ( ! empty( $item_object['variation'] ) ) {
|
|
|
|
$data = $item_object['variation'];
|
|
|
|
} elseif ( ! empty( $item_object['item_meta_array'] ) ) {
|
|
|
|
$data_keys = array_map( 'wc_variation_attribute_name', wp_list_pluck( $item_object['item_meta_array'], 'key' ) );
|
|
|
|
$data_values = wp_list_pluck( $item_object['item_meta_array'], 'value' );
|
2016-11-02 18:50:42 +00:00
|
|
|
$data = array_intersect_key( array_combine( $data_keys, $data_values ), $this->get_attributes() );
|
2016-04-21 09:36:46 +00:00
|
|
|
} else {
|
2016-11-02 18:50:42 +00:00
|
|
|
$data = $this->get_attributes();
|
2016-04-21 09:36:46 +00:00
|
|
|
}
|
2016-11-10 19:05:29 +00:00
|
|
|
|
|
|
|
return add_query_arg( array_map( 'urlencode', array_filter( $data ) ), $url );
|
2013-09-23 14:47:47 +00:00
|
|
|
}
|
|
|
|
|
2013-09-25 11:35:06 +00:00
|
|
|
/**
|
|
|
|
* Get the add to url used mainly in loops.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function add_to_cart_url() {
|
2016-11-02 18:50:42 +00:00
|
|
|
$variation_data = array_map( 'urlencode', $this->get_attributes() );
|
|
|
|
$url = $this->is_purchasable() ? remove_query_arg( 'added-to-cart', add_query_arg( array( 'variation_id' => $this->get_id(), 'add-to-cart' => $this->get_parent_id() ), $this->get_permalink() ) ) : $this->get_permalink();
|
2013-09-25 11:35:06 +00:00
|
|
|
return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this );
|
|
|
|
}
|
|
|
|
|
2016-11-09 12:26:46 +00:00
|
|
|
/**
|
|
|
|
* Get SKU (Stock-keeping unit) - product unique ID.
|
|
|
|
*
|
|
|
|
* @param string $context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_sku( $context = 'view' ) {
|
|
|
|
$value = $this->get_prop( 'sku', $context );
|
|
|
|
|
|
|
|
// Inherit value from parent.
|
|
|
|
if ( 'view' === $context && empty( $value ) ) {
|
|
|
|
$value = $this->parent_data['sku'];
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the product's weight.
|
|
|
|
*
|
|
|
|
* @param string $context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_weight( $context = 'view' ) {
|
|
|
|
$value = $this->get_prop( 'weight', $context );
|
|
|
|
|
|
|
|
// Inherit value from parent.
|
|
|
|
if ( 'view' === $context && empty( $value ) ) {
|
|
|
|
$value = $this->parent_data['weight'];
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the product length.
|
|
|
|
*
|
|
|
|
* @param string $context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_length( $context = 'view' ) {
|
|
|
|
$value = $this->get_prop( 'length', $context );
|
|
|
|
|
|
|
|
// Inherit value from parent.
|
|
|
|
if ( 'view' === $context && empty( $value ) ) {
|
|
|
|
$value = $this->parent_data['length'];
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the product width.
|
|
|
|
*
|
|
|
|
* @param string $context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_width( $context = 'view' ) {
|
|
|
|
$value = $this->get_prop( 'width', $context );
|
|
|
|
|
|
|
|
// Inherit value from parent.
|
|
|
|
if ( 'view' === $context && empty( $value ) ) {
|
|
|
|
$value = $this->parent_data['width'];
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the product height.
|
|
|
|
*
|
|
|
|
* @param string $context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_height( $context = 'view' ) {
|
|
|
|
$value = $this->get_prop( 'height', $context );
|
|
|
|
|
|
|
|
// Inherit value from parent.
|
|
|
|
if ( 'view' === $context && empty( $value ) ) {
|
|
|
|
$value = $this->parent_data['height'];
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the tax class.
|
|
|
|
*
|
|
|
|
* @param string $context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_tax_class( $context = 'view' ) {
|
|
|
|
$value = $this->get_prop( 'tax_class', $context );
|
|
|
|
|
|
|
|
// Inherit value from parent.
|
2016-11-11 16:22:51 +00:00
|
|
|
if ( 'view' === $context && 'parent' === $value ) {
|
2016-11-09 12:26:46 +00:00
|
|
|
$value = $this->parent_data['tax_class'];
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return if product manage stock.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param string $context
|
|
|
|
* @return boolean|string true, false, or parent.
|
|
|
|
*/
|
|
|
|
public function get_manage_stock( $context = 'view' ) {
|
|
|
|
$value = $this->get_prop( 'manage_stock', $context );
|
|
|
|
|
|
|
|
// Inherit value from parent.
|
|
|
|
if ( 'view' === $context && false === $value && true === wc_string_to_bool( $this->parent_data['manage_stock'] ) ) {
|
|
|
|
$value = 'parent';
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns number of items available for sale.
|
|
|
|
*
|
|
|
|
* @param string $context
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
public function get_stock_quantity( $context = 'view' ) {
|
|
|
|
$value = $this->get_prop( 'stock_quantity', $context );
|
|
|
|
|
|
|
|
// Inherit value from parent.
|
|
|
|
if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) {
|
|
|
|
$value = $this->parent_data['stock_quantity'];
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get backorders.
|
|
|
|
*
|
|
|
|
* @param string $context
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return string yes no or notify
|
|
|
|
*/
|
|
|
|
public function get_backorders( $context = 'view' ) {
|
|
|
|
$value = $this->get_prop( 'backorders', $context );
|
|
|
|
|
|
|
|
// Inherit value from parent.
|
|
|
|
if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) {
|
|
|
|
$value = $this->parent_data['backorders'];
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:59:14 +00:00
|
|
|
/**
|
|
|
|
* Get main image ID.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param string $context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_image_id( $context = 'view' ) {
|
2016-11-11 14:31:15 +00:00
|
|
|
$image_id = $this->get_prop( 'image_id', $context );
|
2016-11-09 16:59:14 +00:00
|
|
|
|
|
|
|
if ( 'view' === $context && ! $image_id ) {
|
2016-11-10 19:11:21 +00:00
|
|
|
$image_id = $this->parent_data['image_id'];
|
2016-11-09 16:59:14 +00:00
|
|
|
}
|
2016-11-10 19:11:21 +00:00
|
|
|
|
2016-11-11 14:31:15 +00:00
|
|
|
return $image_id;
|
2016-11-09 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 18:50:42 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| CRUD methods
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
2013-09-25 11:35:06 +00:00
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
2016-11-02 18:50:42 +00:00
|
|
|
* Callback to remove unwanted meta data.
|
2012-08-14 19:42:38 +00:00
|
|
|
*
|
2016-11-02 18:50:42 +00:00
|
|
|
* @param object $meta
|
|
|
|
* @return bool false if excluded.
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2016-11-02 18:50:42 +00:00
|
|
|
protected function exclude_internal_meta_keys( $meta ) {
|
|
|
|
return ! in_array( $meta->meta_key, $this->get_internal_meta_keys() ) && 0 !== stripos( $meta->meta_key, 'attribute_' );
|
2012-06-29 17:56:42 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2016-11-11 14:31:15 +00:00
|
|
|
/**
|
|
|
|
* Set the parent data array for this variation.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param array
|
|
|
|
*/
|
|
|
|
public function set_parent_data( $parent_data ) {
|
|
|
|
$this->parent_data = $parent_data;
|
|
|
|
}
|
|
|
|
|
2011-08-22 11:57:50 +00:00
|
|
|
/**
|
2016-11-02 18:50:42 +00:00
|
|
|
* Set attributes. Unlike the parent product which uses terms, variations are assigned
|
|
|
|
* specific attributes using name value pairs.
|
|
|
|
* @param array
|
2014-09-26 10:45:15 +00:00
|
|
|
*/
|
2016-11-02 18:50:42 +00:00
|
|
|
public function set_attributes( $attributes ) {
|
2016-11-09 12:26:46 +00:00
|
|
|
$this->set_prop( 'attributes', (array) $attributes );
|
2014-09-26 10:45:15 +00:00
|
|
|
}
|
2012-08-10 13:21:10 +00:00
|
|
|
|
2011-08-22 11:57:50 +00:00
|
|
|
/**
|
2016-11-02 18:50:42 +00:00
|
|
|
* Returns array of attribute name value pairs.
|
2016-11-09 12:26:46 +00:00
|
|
|
*
|
|
|
|
* @param string $context
|
2016-11-02 18:50:42 +00:00
|
|
|
* @return array
|
2014-09-26 10:45:15 +00:00
|
|
|
*/
|
2016-11-09 12:26:46 +00:00
|
|
|
public function get_attributes( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'attributes', $context );
|
2014-09-26 10:45:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 12:05:13 +00:00
|
|
|
/**
|
2016-11-02 18:50:42 +00:00
|
|
|
* Save data (either create or update depending on if we are working on an existing product).
|
2016-01-06 15:15:00 +00:00
|
|
|
*
|
2016-11-02 18:50:42 +00:00
|
|
|
* @since 2.7.0
|
2014-06-24 12:05:13 +00:00
|
|
|
*/
|
2016-11-02 18:50:42 +00:00
|
|
|
public function save() {
|
2016-11-11 14:31:15 +00:00
|
|
|
$this->validate_props();
|
2014-06-24 12:05:13 +00:00
|
|
|
|
2016-11-11 14:31:15 +00:00
|
|
|
if ( $this->data_store ) {
|
|
|
|
if ( $this->get_id() ) {
|
|
|
|
$this->data_store->update( $this );
|
|
|
|
} else {
|
|
|
|
$this->data_store->create( $this );
|
|
|
|
}
|
2016-11-02 18:50:42 +00:00
|
|
|
|
2016-11-11 14:31:15 +00:00
|
|
|
wp_schedule_single_event( time(), 'woocommerce_deferred_product_sync', array( 'product_id' => $this->get_parent_id() ) );
|
|
|
|
return $this->get_id();
|
2014-08-04 18:31:40 +00:00
|
|
|
}
|
2014-08-04 09:51:50 +00:00
|
|
|
}
|
2014-06-24 12:05:13 +00:00
|
|
|
|
2016-11-02 18:50:42 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Conditionals
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2015-08-13 10:12:24 +00:00
|
|
|
/**
|
2016-11-02 18:50:42 +00:00
|
|
|
* Returns false if the product cannot be bought.
|
|
|
|
* Override abstract method so that: i) Disabled variations are not be purchasable by admins. ii) Enabled variations are not purchasable if the parent product is not purchasable.
|
2015-08-13 10:12:24 +00:00
|
|
|
*
|
2016-11-02 18:50:42 +00:00
|
|
|
* @return bool
|
2015-08-13 10:12:24 +00:00
|
|
|
*/
|
2016-11-02 18:50:42 +00:00
|
|
|
public function is_purchasable() {
|
|
|
|
return apply_filters( 'woocommerce_variation_is_purchasable', $this->variation_is_visible() && parent::is_purchasable(), $this );
|
2015-08-13 10:12:24 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 07:57:52 +00:00
|
|
|
/**
|
2016-11-02 18:50:42 +00:00
|
|
|
* Controls whether this particular variation will appear greyed-out (inactive) or not (active).
|
|
|
|
* Used by extensions to make incompatible variations appear greyed-out, etc.
|
|
|
|
* Other possible uses: prevent out-of-stock variations from being selected.
|
2013-03-27 07:57:52 +00:00
|
|
|
*
|
2016-11-02 18:50:42 +00:00
|
|
|
* @return bool
|
2013-03-27 07:57:52 +00:00
|
|
|
*/
|
2016-11-02 18:50:42 +00:00
|
|
|
public function variation_is_active() {
|
|
|
|
return apply_filters( 'woocommerce_variation_is_active', true, $this );
|
2013-03-27 07:57:52 +00:00
|
|
|
}
|
2015-05-14 17:56:26 +00:00
|
|
|
|
|
|
|
/**
|
2016-11-02 18:50:42 +00:00
|
|
|
* Checks if this particular variation is visible. Invisible variations are enabled and can be selected, but no price / stock info is displayed.
|
|
|
|
* Instead, a suitable 'unavailable' message is displayed.
|
|
|
|
* Invisible by default: Disabled variations and variations with an empty price.
|
2015-05-14 17:56:26 +00:00
|
|
|
*
|
2016-11-02 18:50:42 +00:00
|
|
|
* @return bool
|
2015-05-14 17:56:26 +00:00
|
|
|
*/
|
2016-11-02 18:50:42 +00:00
|
|
|
public function variation_is_visible() {
|
|
|
|
return apply_filters( 'woocommerce_variation_is_visible', 'publish' === get_post_status( $this->get_id() ) && '' !== $this->get_price(), $this->get_id(), $this->get_parent_id(), $this );
|
2015-05-14 17:56:26 +00:00
|
|
|
}
|
2013-08-07 14:38:31 +00:00
|
|
|
}
|