2012-11-21 18:07:45 +00:00
|
|
|
<?php
|
2014-09-20 18:57:58 +00:00
|
|
|
|
2014-08-13 14:03:30 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2014-09-20 18:57:58 +00:00
|
|
|
exit; // Exit if accessed directly
|
2014-08-13 14:03:30 +00:00
|
|
|
}
|
2013-02-20 17:14:46 +00:00
|
|
|
|
2012-11-21 18:07:45 +00:00
|
|
|
/**
|
|
|
|
* Variable Product Class
|
|
|
|
*
|
|
|
|
* The WooCommerce product class handles individual product data.
|
|
|
|
*
|
|
|
|
* @class WC_Product_Variable
|
2012-12-03 19:19:58 +00:00
|
|
|
* @version 2.0.0
|
2012-11-21 18:07:45 +00:00
|
|
|
* @package WooCommerce/Classes/Products
|
2013-02-20 17:14:46 +00:00
|
|
|
* @category Class
|
2012-11-21 18:07:45 +00:00
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
class WC_Product_Variable extends WC_Product {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-12-14 21:52:46 +00:00
|
|
|
/** @public array Array of child products/posts/variations. */
|
2015-06-08 12:18:23 +00:00
|
|
|
public $children = null;
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2012-12-14 21:52:46 +00:00
|
|
|
/** @public string The product's total stock, including that of its children. */
|
2012-12-20 11:54:38 +00:00
|
|
|
public $total_stock;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-21 18:07:45 +00:00
|
|
|
/**
|
2014-06-24 12:03:25 +00:00
|
|
|
* Constructor
|
2012-11-27 16:22:47 +00:00
|
|
|
*
|
2012-11-21 18:07:45 +00:00
|
|
|
* @param mixed $product
|
|
|
|
*/
|
2012-12-19 23:04:25 +00:00
|
|
|
public function __construct( $product ) {
|
2012-12-20 11:54:38 +00:00
|
|
|
$this->product_type = 'variable';
|
2012-11-22 10:22:18 +00:00
|
|
|
parent::__construct( $product );
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2013-09-25 11:35:06 +00:00
|
|
|
/**
|
|
|
|
* Get the add to cart button text
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function add_to_cart_text() {
|
|
|
|
return apply_filters( 'woocommerce_product_add_to_cart_text', __( 'Select options', 'woocommerce' ), $this );
|
|
|
|
}
|
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
/**
|
|
|
|
* Get total stock.
|
|
|
|
*
|
|
|
|
* This is the stock of parent and children combined.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function get_total_stock() {
|
|
|
|
if ( empty( $this->total_stock ) ) {
|
|
|
|
$transient_name = 'wc_product_total_stock_' . $this->id . WC_Cache_Helper::get_transient_version( 'product' );
|
|
|
|
|
|
|
|
if ( false === ( $this->total_stock = get_transient( $transient_name ) ) ) {
|
|
|
|
$this->total_stock = max( 0, wc_stock_amount( $this->stock ) );
|
2012-11-21 18:07:45 +00:00
|
|
|
|
|
|
|
if ( sizeof( $this->get_children() ) > 0 ) {
|
2014-06-24 12:03:25 +00:00
|
|
|
foreach ( $this->get_children() as $child_id ) {
|
|
|
|
if ( 'yes' === get_post_meta( $child_id, '_manage_stock', true ) ) {
|
|
|
|
$stock = get_post_meta( $child_id, '_stock', true );
|
2014-06-25 10:25:28 +00:00
|
|
|
$this->total_stock += max( 0, wc_stock_amount( $stock ) );
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-24 12:02:56 +00:00
|
|
|
set_transient( $transient_name, $this->total_stock, DAY_IN_SECONDS * 30 );
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-25 10:25:28 +00:00
|
|
|
return wc_stock_amount( $this->total_stock );
|
2015-03-27 15:15:40 +00:00
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2012-12-28 09:59:20 +00:00
|
|
|
/**
|
|
|
|
* Set stock level of the product.
|
|
|
|
*
|
|
|
|
* @param mixed $amount (default: null)
|
2014-05-10 20:45:14 +00:00
|
|
|
* @param string $mode can be set, add, or subtract
|
2012-12-28 09:59:20 +00:00
|
|
|
* @return int Stock
|
|
|
|
*/
|
2014-06-24 12:03:25 +00:00
|
|
|
public function set_stock( $amount = null, $mode = 'set' ) {
|
2013-08-13 15:56:09 +00:00
|
|
|
$this->total_stock = '';
|
2015-06-10 12:41:29 +00:00
|
|
|
delete_transient( 'wc_product_total_stock_' . $this->id . WC_Cache_Helper::get_transient_version( 'product' ) );
|
2014-05-10 20:45:14 +00:00
|
|
|
return parent::set_stock( $amount, $mode );
|
2012-12-28 09:59:20 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 12:03:25 +00:00
|
|
|
/**
|
|
|
|
* Performed after a stock level change at product level
|
|
|
|
*/
|
|
|
|
protected function check_stock_status() {
|
|
|
|
$set_child_stock_status = '';
|
|
|
|
|
|
|
|
if ( ! $this->backorders_allowed() && $this->get_stock_quantity() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) {
|
|
|
|
$set_child_stock_status = 'outofstock';
|
|
|
|
} elseif ( $this->backorders_allowed() || $this->get_stock_quantity() > get_option( 'woocommerce_notify_no_stock_amount' ) ) {
|
|
|
|
$set_child_stock_status = 'instock';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $set_child_stock_status ) {
|
|
|
|
foreach ( $this->get_children() as $child_id ) {
|
|
|
|
if ( 'yes' !== get_post_meta( $child_id, '_manage_stock', true ) ) {
|
|
|
|
wc_update_product_stock_status( $child_id, $set_child_stock_status );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Children statuses changed, so sync self
|
|
|
|
self::sync_stock_status( $this->id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set_stock_status function.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function set_stock_status( $status ) {
|
2014-08-13 14:03:30 +00:00
|
|
|
$status = 'outofstock' === $status ? 'outofstock' : 'instock';
|
2014-06-24 12:03:25 +00:00
|
|
|
|
|
|
|
if ( update_post_meta( $this->id, '_stock_status', $status ) ) {
|
|
|
|
do_action( 'woocommerce_product_set_stock_status', $this->id, $status );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 18:07:45 +00:00
|
|
|
/**
|
2015-06-08 12:18:23 +00:00
|
|
|
* Return a products child ids.
|
2014-05-10 20:45:14 +00:00
|
|
|
*
|
2014-03-19 10:06:13 +00:00
|
|
|
* @param boolean $visible_only Only return variations which are not hidden
|
|
|
|
* @return array of children ids
|
2012-11-21 18:07:45 +00:00
|
|
|
*/
|
2014-03-19 10:06:13 +00:00
|
|
|
public function get_children( $visible_only = false ) {
|
2015-06-09 09:32:38 +00:00
|
|
|
$key = $visible_only ? 'visible' : 'all';
|
|
|
|
$transient_name = 'wc_product_children' . $this->id . WC_Cache_Helper::get_transient_version( 'product' );
|
|
|
|
|
|
|
|
// Get value of transient
|
2015-06-08 12:18:23 +00:00
|
|
|
if ( ! is_array( $this->children ) ) {
|
2014-08-12 11:06:18 +00:00
|
|
|
$this->children = get_transient( $transient_name );
|
2015-06-09 09:32:38 +00:00
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-06-09 09:32:38 +00:00
|
|
|
// Get value from DB
|
|
|
|
if ( empty( $this->children ) || ! is_array( $this->children ) || ! isset( $this->children[ $key ] ) ) {
|
|
|
|
$args = array(
|
|
|
|
'post_parent' => $this->id,
|
|
|
|
'post_type' => 'product_variation',
|
|
|
|
'orderby' => 'menu_order',
|
|
|
|
'order' => 'ASC',
|
|
|
|
'fields' => 'ids',
|
|
|
|
'post_status' => 'publish',
|
|
|
|
'numberposts' => -1
|
|
|
|
);
|
|
|
|
if ( $visible_only ) {
|
|
|
|
$args['meta_query'] = array(
|
|
|
|
'relation' => 'AND',
|
|
|
|
// Price is required
|
|
|
|
array(
|
|
|
|
'key' => '_price',
|
|
|
|
'value' => '',
|
|
|
|
'compare' => '!=',
|
|
|
|
),
|
|
|
|
// Must be in stock
|
|
|
|
array(
|
|
|
|
'key' => '_stock_status',
|
|
|
|
'value' => 'instock',
|
|
|
|
'compare' => '=',
|
|
|
|
)
|
2015-03-27 15:15:40 +00:00
|
|
|
);
|
2014-03-19 10:06:13 +00:00
|
|
|
}
|
2015-06-09 09:32:38 +00:00
|
|
|
$this->children[ $key ] = get_posts( $args );
|
|
|
|
set_transient( $transient_name, $this->children, DAY_IN_SECONDS * 30 );
|
2012-12-05 17:57:53 +00:00
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-06-09 09:32:38 +00:00
|
|
|
return apply_filters( 'woocommerce_get_children', $this->children[ $key ], $this, $visible_only );
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_child function.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $child_id
|
2015-01-23 14:37:20 +00:00
|
|
|
* @return WC_Product WC_Product or WC_Product_variation
|
2012-11-21 18:07:45 +00:00
|
|
|
*/
|
2012-12-14 21:52:46 +00:00
|
|
|
public function get_child( $child_id ) {
|
2014-10-07 09:49:54 +00:00
|
|
|
return wc_get_product( $child_id, array(
|
2012-11-29 16:48:40 +00:00
|
|
|
'parent_id' => $this->id,
|
2012-12-19 23:04:25 +00:00
|
|
|
'parent' => $this
|
2014-08-13 14:03:30 +00:00
|
|
|
) );
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the product has any child product.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-12-14 21:52:46 +00:00
|
|
|
public function has_child() {
|
2012-11-21 18:07:45 +00:00
|
|
|
return sizeof( $this->get_children() ) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the product is on sale.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-12-14 21:52:46 +00:00
|
|
|
public function is_on_sale() {
|
2015-01-22 16:05:59 +00:00
|
|
|
$is_on_sale = false;
|
2015-03-25 13:35:49 +00:00
|
|
|
$prices = $this->get_variation_prices();
|
|
|
|
if ( $prices['regular_price'] !== $prices['price'] ) {
|
|
|
|
$is_on_sale = true;
|
2012-11-29 16:48:40 +00:00
|
|
|
}
|
2015-01-22 13:02:43 +00:00
|
|
|
return apply_filters( 'woocommerce_product_is_on_sale', $is_on_sale, $this );
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 10:28:59 +00:00
|
|
|
/**
|
|
|
|
* Get the min or max variation regular price.
|
|
|
|
* @param string $min_or_max - min or max
|
2013-11-08 15:53:52 +00:00
|
|
|
* @param boolean $display Whether the value is going to be displayed
|
2013-07-23 10:28:59 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-08 15:53:52 +00:00
|
|
|
public function get_variation_regular_price( $min_or_max = 'min', $display = false ) {
|
2015-03-25 13:35:49 +00:00
|
|
|
$prices = $this->get_variation_prices( $display );
|
|
|
|
$price = 'min' === $min_or_max ? current( $prices['regular_price'] ) : end( $prices['regular_price'] );
|
2013-12-07 09:11:23 +00:00
|
|
|
return apply_filters( 'woocommerce_get_variation_regular_price', $price, $this, $min_or_max, $display );
|
2013-07-23 10:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the min or max variation sale price.
|
|
|
|
* @param string $min_or_max - min or max
|
2013-11-08 15:53:52 +00:00
|
|
|
* @param boolean $display Whether the value is going to be displayed
|
2013-07-23 10:28:59 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-08 15:53:52 +00:00
|
|
|
public function get_variation_sale_price( $min_or_max = 'min', $display = false ) {
|
2015-03-25 13:35:49 +00:00
|
|
|
$prices = $this->get_variation_prices( $display );
|
|
|
|
$price = 'min' === $min_or_max ? current( $prices['sale_price'] ) : end( $prices['sale_price'] );
|
2013-12-07 09:11:23 +00:00
|
|
|
return apply_filters( 'woocommerce_get_variation_sale_price', $price, $this, $min_or_max, $display );
|
2013-07-23 10:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the min or max variation (active) price.
|
|
|
|
* @param string $min_or_max - min or max
|
2013-11-08 15:53:52 +00:00
|
|
|
* @param boolean $display Whether the value is going to be displayed
|
2013-07-23 10:28:59 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-08 15:53:52 +00:00
|
|
|
public function get_variation_price( $min_or_max = 'min', $display = false ) {
|
2015-03-25 13:35:49 +00:00
|
|
|
$prices = $this->get_variation_prices( $display );
|
|
|
|
$price = 'min' === $min_or_max ? current( $prices['price'] ) : end( $prices['price'] );
|
|
|
|
return apply_filters( 'woocommerce_get_variation_price', $price, $this, $min_or_max, $display );
|
|
|
|
}
|
2014-05-10 20:45:14 +00:00
|
|
|
|
2015-03-25 13:35:49 +00:00
|
|
|
/**
|
|
|
|
* Get an array of all sale and regular prices from all variations.
|
|
|
|
* @param bool Are prices for display? If so, taxes will be calculated.
|
|
|
|
* @return array()
|
|
|
|
*/
|
|
|
|
public function get_variation_prices( $display = false ) {
|
2015-03-25 14:52:33 +00:00
|
|
|
$cache_key = 'var_prices_' . md5( json_encode( apply_filters( 'woocommerce_get_variation_prices_hash', array(
|
2015-03-25 16:27:11 +00:00
|
|
|
$this->id,
|
2015-03-25 14:52:33 +00:00
|
|
|
$display ? WC_Tax::get_rates() : '',
|
|
|
|
WC_Cache_Helper::get_transient_version( 'product' )
|
|
|
|
), $this, $display ) ) );
|
2015-03-25 13:35:49 +00:00
|
|
|
|
|
|
|
if ( false === ( $prices_array = get_transient( $cache_key ) ) ) {
|
|
|
|
$prices = array();
|
|
|
|
$regular_prices = array();
|
|
|
|
$sale_prices = array();
|
|
|
|
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
|
|
|
|
|
2015-06-08 12:18:23 +00:00
|
|
|
foreach ( $this->get_children( true ) as $variation_id ) {
|
2015-03-25 13:35:49 +00:00
|
|
|
$price = get_post_meta( $variation_id, '_price', true );
|
|
|
|
$regular_price = get_post_meta( $variation_id, '_regular_price', true );
|
|
|
|
$sale_price = get_post_meta( $variation_id, '_sale_price', true );
|
2013-12-03 14:54:09 +00:00
|
|
|
|
2015-03-25 13:35:49 +00:00
|
|
|
// If sale price does not equal price, the product is not yet on sale
|
|
|
|
if ( $price != $sale_price ) {
|
|
|
|
$sale_price = $regular_price;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are getting prices for display, we need to account for taxes
|
|
|
|
if ( $display && ( $variation = $this->get_child( $variation_id ) ) ) {
|
2015-03-25 15:23:28 +00:00
|
|
|
$price = $tax_display_mode == 'incl' ? $variation->get_price_including_tax( 1, $price ) : $variation->get_price_excluding_tax( 1, $price );
|
|
|
|
$regular_price = $tax_display_mode == 'incl' ? $variation->get_price_including_tax( 1, $regular_price ) : $variation->get_price_excluding_tax( 1, $regular_price );
|
|
|
|
$sale_price = $tax_display_mode == 'incl' ? $variation->get_price_including_tax( 1, $sale_price ) : $variation->get_price_excluding_tax( 1, $sale_price );
|
2015-03-25 13:35:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 16:23:19 +00:00
|
|
|
$prices[ $variation_id ] = $price;
|
|
|
|
$regular_prices[ $variation_id ] = $regular_price;
|
|
|
|
$sale_prices[ $variation_id ] = $sale_price;
|
2013-12-03 14:54:09 +00:00
|
|
|
}
|
2015-03-25 13:35:49 +00:00
|
|
|
|
2015-03-25 16:23:19 +00:00
|
|
|
asort( $prices );
|
|
|
|
asort( $regular_prices );
|
|
|
|
asort( $sale_prices );
|
2015-03-25 13:35:49 +00:00
|
|
|
|
|
|
|
$prices_array = array(
|
|
|
|
'price' => $prices,
|
|
|
|
'regular_price' => $regular_prices,
|
|
|
|
'sale_price' => $sale_prices
|
|
|
|
);
|
|
|
|
|
|
|
|
set_transient( $cache_key, $prices_array, DAY_IN_SECONDS * 30 );
|
2013-11-08 15:53:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 15:48:51 +00:00
|
|
|
return apply_filters( 'woocommerce_variation_prices', $prices_array, $this, $display );
|
2013-07-23 10:28:59 +00:00
|
|
|
}
|
|
|
|
|
2012-11-21 18:07:45 +00:00
|
|
|
/**
|
|
|
|
* Returns the price in html format.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $price (default: '')
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-12-14 21:52:46 +00:00
|
|
|
public function get_price_html( $price = '' ) {
|
2013-12-04 12:08:11 +00:00
|
|
|
if ( $this->get_price() === '' ) {
|
2013-09-19 15:31:54 +00:00
|
|
|
$price = apply_filters( 'woocommerce_variable_empty_price_html', '', $this );
|
2013-12-04 12:08:11 +00:00
|
|
|
} else {
|
2015-03-25 15:23:28 +00:00
|
|
|
$prices = $this->get_variation_prices( true );
|
|
|
|
$min_price = current( $prices['price'] );
|
|
|
|
$max_price = end( $prices['price'] );
|
|
|
|
$price = $min_price !== $max_price ? sprintf( _x( '%1$s–%2$s', 'Price range: from-to', 'woocommerce' ), wc_price( $min_price ), wc_price( $max_price ) ) : wc_price( $min_price );
|
|
|
|
$is_free = $min_price == 0 && $max_price == 0;
|
|
|
|
|
|
|
|
if ( $this->is_on_sale() ) {
|
|
|
|
$min_regular_price = current( $prices['regular_price'] );
|
|
|
|
$max_regular_price = end( $prices['regular_price'] );
|
|
|
|
$regular_price = $min_regular_price !== $max_regular_price ? sprintf( _x( '%1$s–%2$s', 'Price range: from-to', 'woocommerce' ), wc_price( $min_regular_price ), wc_price( $max_regular_price ) ) : wc_price( $min_regular_price );
|
|
|
|
$price = apply_filters( 'woocommerce_variable_sale_price_html', $this->get_price_html_from_to( $regular_price, $price ) . $this->get_price_suffix(), $this );
|
|
|
|
} elseif ( $is_free ) {
|
2015-03-25 13:35:49 +00:00
|
|
|
$price = apply_filters( 'woocommerce_variable_free_price_html', __( 'Free!', 'woocommerce' ), $this );
|
2012-11-21 18:07:45 +00:00
|
|
|
} else {
|
2015-03-25 13:35:49 +00:00
|
|
|
$price = apply_filters( 'woocommerce_variable_price_html', $price . $this->get_price_suffix(), $this );
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return apply_filters( 'woocommerce_get_price_html', $price, $this );
|
|
|
|
}
|
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
/**
|
|
|
|
* Return an array of attributes used for variations, as well as their possible values.
|
|
|
|
*
|
|
|
|
* @return array of attributes and their available values
|
|
|
|
*/
|
|
|
|
public function get_variation_attributes() {
|
|
|
|
$variation_attributes = array();
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
if ( ! $this->has_child() ) {
|
|
|
|
return $variation_attributes;
|
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
$attributes = $this->get_attributes();
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
foreach ( $attributes as $attribute ) {
|
|
|
|
if ( ! $attribute['is_variation'] ) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-06-11 13:43:02 +00:00
|
|
|
$values = array();
|
2015-03-27 15:15:40 +00:00
|
|
|
$attribute_field_name = 'attribute_' . sanitize_title( $attribute['name'] );
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-06-11 13:43:02 +00:00
|
|
|
// Get used values from children variations
|
2015-03-27 15:15:40 +00:00
|
|
|
foreach ( $this->get_children() as $child_id ) {
|
|
|
|
$variation = $this->get_child( $child_id );
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2013-10-10 15:34:44 +00:00
|
|
|
if ( ! empty( $variation->variation_id ) ) {
|
2014-08-13 14:03:30 +00:00
|
|
|
if ( ! $variation->variation_is_visible() ) {
|
2013-10-10 15:34:44 +00:00
|
|
|
continue; // Disabled or hidden
|
2014-08-13 14:03:30 +00:00
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2013-10-10 15:34:44 +00:00
|
|
|
$child_variation_attributes = $variation->get_variation_attributes();
|
|
|
|
|
2015-06-11 13:43:02 +00:00
|
|
|
if ( isset( $child_variation_attributes[ $attribute_field_name ] ) ) {
|
|
|
|
$values[] = $child_variation_attributes[ $attribute_field_name ];
|
2015-03-27 15:15:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
// empty value indicates that all options for given attribute are available
|
|
|
|
if ( in_array( '', $values ) ) {
|
2015-06-11 13:43:02 +00:00
|
|
|
$values = $attribute['is_taxonomy'] ? wp_get_post_terms( $this->id, $attribute['name'], array( 'fields' => 'slugs' ) ) : wc_get_text_attributes( $attribute['value'] );
|
2012-11-21 18:07:45 +00:00
|
|
|
|
|
|
|
// Order custom attributes (non taxonomy) as defined
|
2015-03-27 15:15:40 +00:00
|
|
|
} elseif ( ! $attribute['is_taxonomy'] ) {
|
2015-06-11 13:43:02 +00:00
|
|
|
$text_attributes = wc_get_text_attributes( $attribute['value'] );
|
|
|
|
$assigned_text_attributes = $values;
|
|
|
|
$values = array();
|
|
|
|
|
|
|
|
// Pre 2.4 handling where 'slugs' were saved instead of the full text attribute
|
2015-06-16 14:10:53 +00:00
|
|
|
if ( $assigned_text_attributes === array_map( 'sanitize_title', $assigned_text_attributes ) && version_compare( get_post_meta( $this->id, '_product_version', true ), '2.4.0', '<' ) ) {
|
2015-06-11 13:43:02 +00:00
|
|
|
$assigned_text_attributes = array_map( 'sanitize_title', $assigned_text_attributes );
|
2015-06-11 14:42:18 +00:00
|
|
|
|
2015-06-11 13:43:02 +00:00
|
|
|
foreach ( $text_attributes as $text_attribute ) {
|
|
|
|
if ( in_array( sanitize_title( $text_attribute ), $assigned_text_attributes ) ) {
|
|
|
|
$values[] = $text_attribute;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach ( $text_attributes as $text_attribute ) {
|
|
|
|
if ( in_array( $text_attribute, $assigned_text_attributes ) ) {
|
|
|
|
$values[] = $text_attribute;
|
|
|
|
}
|
|
|
|
}
|
2015-03-27 15:15:40 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
$variation_attributes[ $attribute['name'] ] = array_unique( $values );
|
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
return $variation_attributes;
|
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
/**
|
|
|
|
* If set, get the default attributes for a variable product.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_variation_default_attributes() {
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
$default = isset( $this->default_attributes ) ? $this->default_attributes : '';
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
return apply_filters( 'woocommerce_product_default_attributes', (array) maybe_unserialize( $default ), $this );
|
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
/**
|
|
|
|
* Get an array of available variations for the current product.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_available_variations() {
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
$available_variations = array();
|
2012-11-21 18:07:45 +00:00
|
|
|
|
|
|
|
foreach ( $this->get_children() as $child_id ) {
|
|
|
|
|
|
|
|
$variation = $this->get_child( $child_id );
|
|
|
|
|
2014-09-26 16:22:43 +00:00
|
|
|
// Hide out of stock variations if 'Hide out of stock items from the catalog' is checked
|
2014-06-24 12:03:25 +00:00
|
|
|
if ( empty( $variation->variation_id ) || ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $variation->is_in_stock() ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-09-20 18:57:58 +00:00
|
|
|
|
2014-09-26 16:22:43 +00:00
|
|
|
// Filter 'woocommerce_hide_invisible_variations' to optionally hide invisible variations (disabled variations and variations with empty price)
|
2015-04-29 09:16:58 +00:00
|
|
|
if ( apply_filters( 'woocommerce_hide_invisible_variations', false, $this->id, $variation ) && ! $variation->variation_is_visible() ) {
|
2014-09-26 16:22:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-08-13 10:30:47 +00:00
|
|
|
$variation_attributes = $variation->get_variation_attributes();
|
|
|
|
$availability = $variation->get_availability();
|
|
|
|
$availability_html = empty( $availability['availability'] ) ? '' : '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . wp_kses_post( $availability['availability'] ) . '</p>';
|
|
|
|
$availability_html = apply_filters( 'woocommerce_stock_html', $availability_html, $availability['availability'], $variation );
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2014-06-24 12:03:25 +00:00
|
|
|
if ( has_post_thumbnail( $variation->get_variation_id() ) ) {
|
|
|
|
$attachment_id = get_post_thumbnail_id( $variation->get_variation_id() );
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2014-06-24 12:03:25 +00:00
|
|
|
$attachment = wp_get_attachment_image_src( $attachment_id, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
|
|
|
|
$image = $attachment ? current( $attachment ) : '';
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2014-06-24 12:03:25 +00:00
|
|
|
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
|
|
|
|
$image_link = $attachment ? current( $attachment ) : '';
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2014-06-24 12:03:25 +00:00
|
|
|
$image_title = get_the_title( $attachment_id );
|
|
|
|
$image_alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
|
|
|
|
} else {
|
|
|
|
$image = $image_link = $image_title = $image_alt = '';
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
2014-06-24 12:03:25 +00:00
|
|
|
|
|
|
|
$available_variations[] = apply_filters( 'woocommerce_available_variation', array(
|
2014-12-03 09:28:04 +00:00
|
|
|
'variation_id' => $child_id,
|
|
|
|
'variation_is_visible' => $variation->variation_is_visible(),
|
|
|
|
'variation_is_active' => $variation->variation_is_active(),
|
|
|
|
'is_purchasable' => $variation->is_purchasable(),
|
|
|
|
'display_price' => $variation->get_display_price(),
|
|
|
|
'display_regular_price' => $variation->get_display_price( $variation->get_regular_price() ),
|
|
|
|
'attributes' => $variation_attributes,
|
|
|
|
'image_src' => $image,
|
|
|
|
'image_link' => $image_link,
|
|
|
|
'image_title' => $image_title,
|
|
|
|
'image_alt' => $image_alt,
|
|
|
|
'price_html' => $variation->get_price() === "" || $this->get_variation_price( 'min' ) !== $this->get_variation_price( 'max' ) ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
|
|
|
|
'availability_html' => $availability_html,
|
|
|
|
'sku' => $variation->get_sku(),
|
|
|
|
'weight' => $variation->get_weight() . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) ),
|
|
|
|
'dimensions' => $variation->get_dimensions(),
|
|
|
|
'min_qty' => 1,
|
|
|
|
'max_qty' => $variation->backorders_allowed() ? '' : $variation->get_stock_quantity(),
|
|
|
|
'backorders_allowed' => $variation->backorders_allowed(),
|
|
|
|
'is_in_stock' => $variation->is_in_stock(),
|
|
|
|
'is_downloadable' => $variation->is_downloadable() ,
|
|
|
|
'is_virtual' => $variation->is_virtual(),
|
|
|
|
'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
|
2015-05-14 17:56:26 +00:00
|
|
|
'variation_description' => $variation->get_variation_description(),
|
2014-06-24 12:03:25 +00:00
|
|
|
), $this, $variation );
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $available_variations;
|
2015-03-27 15:15:40 +00:00
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
|
|
|
/**
|
2013-03-03 17:07:31 +00:00
|
|
|
* Sync variable product prices with the children lowest/highest prices.
|
2012-11-21 18:07:45 +00:00
|
|
|
*/
|
2013-08-02 00:35:54 +00:00
|
|
|
public function variable_product_sync( $product_id = '' ) {
|
2014-10-14 13:21:23 +00:00
|
|
|
if ( empty( $product_id ) ) {
|
2013-08-02 00:54:24 +00:00
|
|
|
$product_id = $this->id;
|
2014-10-14 13:21:23 +00:00
|
|
|
}
|
2013-08-02 00:54:24 +00:00
|
|
|
|
2013-10-11 14:56:13 +00:00
|
|
|
// Sync prices with children
|
2013-08-02 00:35:54 +00:00
|
|
|
self::sync( $product_id );
|
2013-10-11 14:56:13 +00:00
|
|
|
|
|
|
|
// Re-load prices
|
2015-03-25 13:35:49 +00:00
|
|
|
$this->price = get_post_meta( $product_id, '_price', true );
|
2014-03-19 10:51:07 +00:00
|
|
|
|
|
|
|
foreach ( array( 'price', 'regular_price', 'sale_price' ) as $price_type ) {
|
|
|
|
$min_variation_id_key = "min_{$price_type}_variation_id";
|
|
|
|
$max_variation_id_key = "max_{$price_type}_variation_id";
|
|
|
|
$min_price_key = "_min_variation_{$price_type}";
|
|
|
|
$max_price_key = "_max_variation_{$price_type}";
|
|
|
|
$this->$min_variation_id_key = get_post_meta( $product_id, '_' . $min_variation_id_key, true );
|
|
|
|
$this->$max_variation_id_key = get_post_meta( $product_id, '_' . $max_variation_id_key, true );
|
|
|
|
$this->$min_price_key = get_post_meta( $product_id, '_' . $min_price_key, true );
|
|
|
|
$this->$max_price_key = get_post_meta( $product_id, '_' . $max_price_key, true );
|
|
|
|
}
|
2013-08-02 00:35:54 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 12:03:25 +00:00
|
|
|
/**
|
|
|
|
* Sync variable product stock status with children
|
|
|
|
* @param int $product_id
|
|
|
|
*/
|
|
|
|
public static function sync_stock_status( $product_id ) {
|
|
|
|
$children = get_posts( array(
|
|
|
|
'post_parent' => $product_id,
|
|
|
|
'posts_per_page'=> -1,
|
|
|
|
'post_type' => 'product_variation',
|
|
|
|
'fields' => 'ids',
|
|
|
|
'post_status' => 'publish'
|
|
|
|
) );
|
|
|
|
|
|
|
|
$stock_status = 'outofstock';
|
2014-09-20 18:57:58 +00:00
|
|
|
|
2014-06-24 12:03:25 +00:00
|
|
|
foreach ( $children as $child_id ) {
|
|
|
|
$child_stock_status = get_post_meta( $child_id, '_stock_status', true );
|
|
|
|
$child_stock_status = $child_stock_status ? $child_stock_status : 'instock';
|
|
|
|
if ( 'instock' === $child_stock_status ) {
|
|
|
|
$stock_status = 'instock';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wc_update_product_stock_status( $product_id, $stock_status );
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:35:54 +00:00
|
|
|
/**
|
2013-11-08 15:53:52 +00:00
|
|
|
* Sync the variable product with it's children
|
2013-08-02 00:35:54 +00:00
|
|
|
*/
|
|
|
|
public static function sync( $product_id ) {
|
2013-11-08 15:53:52 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
2012-11-21 18:07:45 +00:00
|
|
|
$children = get_posts( array(
|
2013-07-30 11:15:09 +00:00
|
|
|
'post_parent' => $product_id,
|
2012-11-21 18:07:45 +00:00
|
|
|
'posts_per_page'=> -1,
|
|
|
|
'post_type' => 'product_variation',
|
|
|
|
'fields' => 'ids',
|
|
|
|
'post_status' => 'publish'
|
2013-11-08 15:53:52 +00:00
|
|
|
) );
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2013-11-08 15:53:52 +00:00
|
|
|
// No published variations - update parent post status. Use $wpdb to prevent endless loop on save_post hooks.
|
|
|
|
if ( ! $children && get_post_status( $product_id ) == 'publish' ) {
|
|
|
|
$wpdb->update( $wpdb->posts, array( 'post_status' => 'draft' ), array( 'ID' => $product_id ) );
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2014-01-08 13:38:31 +00:00
|
|
|
if ( is_admin() ) {
|
|
|
|
WC_Admin_Meta_Boxes::add_error( __( 'This variable product has no active variations so cannot be published. Changing status to draft.', 'woocommerce' ) );
|
|
|
|
}
|
2014-05-10 20:45:14 +00:00
|
|
|
|
2013-11-08 15:53:52 +00:00
|
|
|
// Loop the variations
|
|
|
|
} else {
|
2015-01-16 01:06:09 +00:00
|
|
|
|
|
|
|
// Set the variable product to be virtual/downloadable if all children are virtual/downloadable
|
|
|
|
foreach ( array( '_downloadable', '_virtual' ) as $meta_key ) {
|
|
|
|
$all_variations_yes = true;
|
|
|
|
|
|
|
|
foreach ( $children as $child_id ) {
|
|
|
|
if ( 'yes' != get_post_meta( $child_id, $meta_key, true ) ) {
|
|
|
|
$all_variations_yes = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update_post_meta( $product_id, $meta_key, ( true === $all_variations_yes ) ? 'yes' : 'no' );
|
|
|
|
}
|
|
|
|
|
2014-03-19 10:51:07 +00:00
|
|
|
// Main active prices
|
|
|
|
$min_price = null;
|
|
|
|
$max_price = null;
|
|
|
|
$min_price_id = null;
|
|
|
|
$max_price_id = null;
|
2014-05-10 20:45:14 +00:00
|
|
|
|
2014-03-19 10:51:07 +00:00
|
|
|
// Regular prices
|
|
|
|
$min_regular_price = null;
|
|
|
|
$max_regular_price = null;
|
|
|
|
$min_regular_price_id = null;
|
|
|
|
$max_regular_price_id = null;
|
2014-05-10 20:45:14 +00:00
|
|
|
|
2014-03-19 10:51:07 +00:00
|
|
|
// Sale prices
|
|
|
|
$min_sale_price = null;
|
|
|
|
$max_sale_price = null;
|
|
|
|
$min_sale_price_id = null;
|
|
|
|
$max_sale_price_id = null;
|
|
|
|
|
|
|
|
foreach ( array( 'price', 'regular_price', 'sale_price' ) as $price_type ) {
|
|
|
|
foreach ( $children as $child_id ) {
|
|
|
|
$child_price = get_post_meta( $child_id, '_' . $price_type, true );
|
|
|
|
|
|
|
|
// Skip non-priced variations
|
|
|
|
if ( $child_price === '' ) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2014-03-19 10:51:07 +00:00
|
|
|
// Skip hidden variations
|
|
|
|
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
|
|
|
|
$stock = get_post_meta( $child_id, '_stock', true );
|
|
|
|
if ( $stock !== "" && $stock <= get_option( 'woocommerce_notify_no_stock_amount' ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 14:44:24 +00:00
|
|
|
|
2014-03-19 10:51:07 +00:00
|
|
|
// Find min price
|
|
|
|
if ( is_null( ${"min_{$price_type}"} ) || $child_price < ${"min_{$price_type}"} ) {
|
|
|
|
${"min_{$price_type}"} = $child_price;
|
|
|
|
${"min_{$price_type}_id"} = $child_id;
|
|
|
|
}
|
2014-03-04 14:44:24 +00:00
|
|
|
|
2014-03-19 10:51:07 +00:00
|
|
|
// Find max price
|
|
|
|
if ( $child_price > ${"max_{$price_type}"} ) {
|
|
|
|
${"max_{$price_type}"} = $child_price;
|
|
|
|
${"max_{$price_type}_id"} = $child_id;
|
2014-03-06 12:07:28 +00:00
|
|
|
}
|
2014-03-04 14:44:24 +00:00
|
|
|
}
|
2013-04-04 09:11:12 +00:00
|
|
|
|
2014-03-19 10:51:07 +00:00
|
|
|
// Store prices
|
|
|
|
update_post_meta( $product_id, '_min_variation_' . $price_type, ${"min_{$price_type}"} );
|
|
|
|
update_post_meta( $product_id, '_max_variation_' . $price_type, ${"max_{$price_type}"} );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2014-03-19 10:51:07 +00:00
|
|
|
// Store ids
|
|
|
|
update_post_meta( $product_id, '_min_' . $price_type . '_variation_id', ${"min_{$price_type}_id"} );
|
|
|
|
update_post_meta( $product_id, '_max_' . $price_type . '_variation_id', ${"max_{$price_type}_id"} );
|
2013-03-06 13:25:37 +00:00
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2014-03-19 10:51:07 +00:00
|
|
|
// The VARIABLE PRODUCT price should equal the min price of any type
|
2013-11-08 15:53:52 +00:00
|
|
|
update_post_meta( $product_id, '_price', $min_price );
|
2014-06-19 10:40:33 +00:00
|
|
|
delete_transient( 'wc_products_onsale' );
|
2014-09-20 18:57:58 +00:00
|
|
|
|
2013-11-08 15:53:52 +00:00
|
|
|
do_action( 'woocommerce_variable_product_sync', $product_id, $children );
|
2012-12-19 20:42:25 +00:00
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
2014-03-07 08:29:01 +00:00
|
|
|
}
|