2012-11-21 18:07:45 +00:00
|
|
|
<?php
|
2013-02-20 17:14:46 +00:00
|
|
|
|
2014-09-20 18:57:09 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
2013-02-20 17:14:46 +00:00
|
|
|
|
2012-11-21 18:07:45 +00:00
|
|
|
/**
|
|
|
|
* Grouped Product Class
|
|
|
|
*
|
|
|
|
* Grouped products cannot be purchased - they are wrappers for other products.
|
|
|
|
*
|
|
|
|
* @class WC_Product_Grouped
|
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_Grouped extends WC_Product {
|
|
|
|
|
2012-12-19 23:04:25 +00:00
|
|
|
/** @public array Array of child products/posts/variations. */
|
2012-12-20 11:54:38 +00:00
|
|
|
public $children;
|
2012-12-19 23:04:25 +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
|
|
|
/**
|
|
|
|
* __construct function.
|
2012-11-27 16:22:47 +00:00
|
|
|
*
|
2012-11-21 18:07:45 +00:00
|
|
|
* @access public
|
|
|
|
* @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 = 'grouped';
|
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', __( 'View products', 'woocommerce' ), $this );
|
|
|
|
}
|
|
|
|
|
2012-11-21 18:07:45 +00:00
|
|
|
/**
|
|
|
|
* Get total stock.
|
|
|
|
*
|
|
|
|
* This is the stock of parent and children combined.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-12-14 21:50:51 +00:00
|
|
|
public function get_total_stock() {
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2012-12-19 23:04:25 +00:00
|
|
|
if ( empty( $this->total_stock ) ) {
|
2012-11-21 18:07:45 +00:00
|
|
|
|
|
|
|
$transient_name = 'wc_product_total_stock_' . $this->id;
|
|
|
|
|
|
|
|
if ( false === ( $this->total_stock = get_transient( $transient_name ) ) ) {
|
|
|
|
$this->total_stock = $this->stock;
|
|
|
|
|
|
|
|
if ( sizeof( $this->get_children() ) > 0 ) {
|
|
|
|
foreach ($this->get_children() as $child_id) {
|
|
|
|
$stock = get_post_meta( $child_id, '_stock', true );
|
|
|
|
|
|
|
|
if ( $stock != '' ) {
|
2014-06-25 10:25:28 +00:00
|
|
|
$this->total_stock += wc_stock_amount( $stock );
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 11:58:41 +00:00
|
|
|
set_transient( $transient_name, $this->total_stock, YEAR_IN_SECONDS );
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 10:25:28 +00:00
|
|
|
return wc_stock_amount( $this->total_stock );
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the products children posts.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-12-14 21:50:51 +00:00
|
|
|
public function get_children() {
|
2014-08-12 11:06:18 +00:00
|
|
|
if ( ! is_array( $this->children ) || empty( $this->children ) ) {
|
|
|
|
$transient_name = 'wc_product_children_ids_' . $this->id;
|
|
|
|
$this->children = get_transient( $transient_name );
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2014-08-12 11:06:18 +00:00
|
|
|
if ( empty( $this->children ) ) {
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2013-10-11 12:25:34 +00:00
|
|
|
$this->children = get_posts( 'post_parent=' . $this->id . '&post_type=product&orderby=menu_order&order=ASC&fields=ids&post_status=publish&numberposts=-1' );
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2014-03-12 11:58:41 +00:00
|
|
|
set_transient( $transient_name, $this->children, YEAR_IN_SECONDS );
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (array) $this->children;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_child function.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $child_id
|
|
|
|
* @return object WC_Product or WC_Product_variation
|
|
|
|
*/
|
2012-12-14 21:50:51 +00:00
|
|
|
public function get_child( $child_id ) {
|
2012-11-21 18:07:45 +00:00
|
|
|
return get_product( $child_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the product has any child product.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-12-14 21:50:51 +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.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-12-14 21:50:51 +00:00
|
|
|
public function is_on_sale() {
|
2012-11-29 16:48:40 +00:00
|
|
|
if ( $this->has_child() ) {
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
foreach ( $this->get_children() as $child_id ) {
|
2012-11-21 18:07:45 +00:00
|
|
|
$sale_price = get_post_meta( $child_id, '_sale_price', true );
|
2012-11-29 16:48:40 +00:00
|
|
|
if ( $sale_price !== "" && $sale_price >= 0 )
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
} else {
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
if ( $this->sale_price && $this->sale_price == $this->price )
|
|
|
|
return true;
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns false if the product cannot be bought.
|
|
|
|
*
|
|
|
|
* @access public
|
2014-03-21 21:41:32 +00:00
|
|
|
* @return bool
|
2012-11-21 18:07:45 +00:00
|
|
|
*/
|
2012-12-14 21:50:51 +00:00
|
|
|
public function is_purchasable() {
|
2012-11-21 18:07:45 +00:00
|
|
|
return apply_filters( 'woocommerce_is_purchasable', false, $this );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the price in html format.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $price (default: '')
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-12-14 21:50:51 +00:00
|
|
|
public function get_price_html( $price = '' ) {
|
2013-09-25 11:35:06 +00:00
|
|
|
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
|
|
|
|
$child_prices = array();
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
foreach ( $this->get_children() as $child_id )
|
|
|
|
$child_prices[] = get_post_meta( $child_id, '_price', true );
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2013-12-04 12:08:11 +00:00
|
|
|
$child_prices = array_unique( $child_prices );
|
|
|
|
$get_price_method = 'get_price_' . $tax_display_mode . 'uding_tax';
|
2012-11-21 18:07:45 +00:00
|
|
|
|
|
|
|
if ( ! empty( $child_prices ) ) {
|
|
|
|
$min_price = min( $child_prices );
|
2013-12-04 12:08:11 +00:00
|
|
|
$max_price = max( $child_prices );
|
2012-11-21 18:07:45 +00:00
|
|
|
} else {
|
|
|
|
$min_price = '';
|
2013-12-04 12:08:11 +00:00
|
|
|
$max_price = '';
|
2012-11-21 18:07:45 +00:00
|
|
|
}
|
|
|
|
|
2013-09-19 15:31:54 +00:00
|
|
|
if ( $min_price ) {
|
2013-12-04 12:08:11 +00:00
|
|
|
if ( $min_price == $max_price ) {
|
|
|
|
$display_price = wc_price( $this->$get_price_method( 1, $min_price ) );
|
|
|
|
} else {
|
2013-12-10 17:38:36 +00:00
|
|
|
$from = wc_price( $this->$get_price_method( 1, $min_price ) );
|
|
|
|
$to = wc_price( $this->$get_price_method( 1, $max_price ) );
|
2013-12-04 12:08:11 +00:00
|
|
|
$display_price = sprintf( _x( '%1$s–%2$s', 'Price range: from-to', 'woocommerce' ), $from, $to );
|
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2013-12-04 12:08:11 +00:00
|
|
|
$price .= $display_price . $this->get_price_suffix();
|
2012-11-21 18:07:45 +00:00
|
|
|
|
2013-09-19 15:31:54 +00:00
|
|
|
$price = apply_filters( 'woocommerce_grouped_price_html', $price, $this );
|
|
|
|
} else {
|
|
|
|
$price = apply_filters( 'woocommerce_grouped_empty_price_html', '', $this );
|
|
|
|
}
|
2012-11-21 18:07:45 +00:00
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_get_price_html', $price, $this );
|
|
|
|
}
|
2014-03-07 08:29:01 +00:00
|
|
|
}
|