2012-11-21 18:07:45 +00:00
< ? php
2014-09-20 18:57:09 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2016-10-18 17:38:42 +00:00
exit ;
2014-09-20 18:57:09 +00:00
}
2013-02-20 17:14:46 +00:00
2012-11-21 18:07:45 +00:00
/**
2015-11-03 13:31:20 +00:00
* Grouped Product Class .
2012-11-21 18:07:45 +00:00
*
* Grouped products cannot be purchased - they are wrappers for other products .
*
* @ class WC_Product_Grouped
2016-10-18 17:38:42 +00:00
* @ version 2.7 . 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 {
2016-10-18 17:38:42 +00:00
/**
* Stores product data .
*
* @ var array
*/
protected $extra_data = array (
'children' => array (),
);
2012-12-19 23:04:25 +00:00
2012-11-21 18:07:45 +00:00
/**
2016-10-18 17:38:42 +00:00
* Merges grouped product data into the parent object .
* @ param int | WC_Product | object $product Product to init .
2012-11-21 18:07:45 +00:00
*/
2016-10-18 17:38:42 +00:00
public function __construct ( $product = 0 ) {
$this -> data = array_merge ( $this -> data , $this -> extra_data );
parent :: __construct ( $product );
2012-11-21 18:07:45 +00:00
}
2013-09-25 11:35:06 +00:00
/**
2016-10-18 17:38:42 +00:00
* Get internal type .
2013-09-25 11:35:06 +00:00
* @ return string
*/
2016-10-18 17:38:42 +00:00
public function get_type () {
return 'grouped' ;
2013-09-25 11:35:06 +00:00
}
2012-11-21 18:07:45 +00:00
/**
2016-10-18 17:38:42 +00:00
* Get the add to cart button text .
2012-11-21 18:07:45 +00:00
*
* @ access public
2016-10-18 17:38:42 +00:00
* @ return string
2012-11-21 18:07:45 +00:00
*/
2016-10-18 17:38:42 +00:00
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
}
/**
* Returns whether or not the product is on sale .
*
* @ return bool
*/
2012-12-14 21:50:51 +00:00
public function is_on_sale () {
2016-10-18 17:38:42 +00:00
global $wpdb ;
2016-10-24 07:28:56 +00:00
$on_sale = $this -> get_children () && 1 === $wpdb -> get_var ( " SELECT 1 FROM $wpdb->postmeta WHERE meta_key = '_sale_price' AND meta_value > 0 AND post_id IN ( " . implode ( ',' , array_map ( 'esc_sql' , $this -> get_children () ) ) . " ); " );
2016-10-18 17:38:42 +00:00
return apply_filters ( 'woocommerce_product_is_on_sale' , $on_sale , $this );
2012-11-21 18:07:45 +00:00
}
/**
* Returns false if the product cannot be bought .
*
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 );
}
/**
2016-10-18 17:38:42 +00:00
* Returns the price in html format . @ todo consider moving to template function
2012-11-21 18:07:45 +00:00
*
* @ 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
2016-01-29 13:58:12 +00:00
foreach ( $this -> get_children () as $child_id ) {
2016-10-18 17:38:42 +00:00
$child = wc_get_product ( $child_id );
2016-06-23 11:16:38 +00:00
if ( '' !== $child -> get_price () ) {
$child_prices [] = 'incl' === $tax_display_mode ? $child -> get_price_including_tax () : $child -> get_price_excluding_tax ();
}
2016-01-29 13:58:12 +00:00
}
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
}
2016-06-23 11:16:38 +00:00
if ( '' !== $min_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 );
2016-09-07 22:32:24 +00:00
$is_free = ( 0 == $min_price && 0 == $max_price );
2016-06-23 11:16:38 +00:00
if ( $is_free ) {
$price = apply_filters ( 'woocommerce_grouped_free_price_html' , __ ( 'Free!' , 'woocommerce' ), $this );
2013-12-04 12:08:11 +00:00
} else {
2016-07-05 13:44:26 +00:00
$price = apply_filters ( 'woocommerce_grouped_price_html' , $price . $this -> get_price_suffix (), $this , $child_prices );
2013-12-04 12:08:11 +00:00
}
2013-09-19 15:31:54 +00:00
} 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 );
}
2016-10-18 17:38:42 +00:00
2016-11-03 12:03:19 +00:00
/*
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
|
| Methods for getting data from the product object .
*/
/**
* Return the children of this product .
*
* @ param string $context
* @ return array
*/
public function get_children ( $context = 'view' ) {
return $this -> get_prop ( 'children' , $context );
}
2016-10-18 17:38:42 +00:00
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
|
| Methods for getting data from the product object .
*/
/**
* Return the children of this product .
*
* @ param array $children
*/
public function set_children ( $children ) {
2016-11-03 12:03:19 +00:00
$this -> set_prop ( 'children' , array_filter ( wp_parse_id_list ( ( array ) $children ) ) );
2016-10-18 17:38:42 +00:00
}
/*
|--------------------------------------------------------------------------
| CRUD methods
|--------------------------------------------------------------------------
*/
/**
2016-11-03 12:03:19 +00:00
* Read post data .
2016-10-18 17:38:42 +00:00
*
* @ since 2.7 . 0
*/
2016-11-03 12:03:19 +00:00
protected function read_product_data () {
parent :: read_product_data ();
2016-10-18 17:38:42 +00:00
$this -> set_props ( array (
2016-11-03 12:03:19 +00:00
'children' => wp_parse_id_list ( get_post_meta ( $this -> get_id (), '_children' , true ) ),
2016-10-18 17:38:42 +00:00
) );
}
2016-10-24 08:19:29 +00:00
/**
* Helper method that updates all the post meta for a grouped product .
*/
protected function update_post_meta () {
if ( update_post_meta ( $this -> get_id (), '_children' , $this -> get_children () ) ) {
$child_prices = array ();
foreach ( $this -> get_children () as $child_id ) {
$child = wc_get_product ( $child_id );
2016-11-02 17:08:05 +00:00
if ( $child ) {
$child_prices [] = $child -> get_price ();
}
2016-10-24 08:19:29 +00:00
}
2016-11-02 10:08:47 +00:00
$child_prices = array_filter ( $child_prices );
2016-10-24 08:19:29 +00:00
delete_post_meta ( $this -> get_id (), '_price' );
2016-11-02 10:08:47 +00:00
if ( ! empty ( $child_prices ) ) {
add_post_meta ( $this -> get_id (), '_price' , min ( $child_prices ) );
add_post_meta ( $this -> get_id (), '_price' , max ( $child_prices ) );
}
2016-10-24 08:19:29 +00:00
}
parent :: update_post_meta ();
}
2014-03-07 08:29:01 +00:00
}