1 <?php
  2 
  3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  4 
  5 /**
  6  * Grouped Product Class
  7  *
  8  * Grouped products cannot be purchased - they are wrappers for other products.
  9  *
 10  * @class       WC_Product_Grouped
 11  * @version     2.0.0
 12  * @package     WooCommerce/Classes/Products
 13  * @category    Class
 14  * @author      WooThemes
 15  */
 16 class WC_Product_Grouped extends WC_Product {
 17 
 18     /** @public array Array of child products/posts/variations. */
 19     public $children;
 20 
 21     /** @public string The product's total stock, including that of its children. */
 22     public $total_stock;
 23 
 24     /**
 25      * __construct function.
 26      *
 27      * @access public
 28      * @param mixed $product
 29      */
 30     public function __construct( $product ) {
 31         $this->product_type = 'grouped';
 32         parent::__construct( $product );
 33     }
 34 
 35     /**
 36      * Get the add to cart button text
 37      *
 38      * @access public
 39      * @return string
 40      */
 41     public function add_to_cart_text() {
 42         return apply_filters( 'woocommerce_product_add_to_cart_text', __( 'View products', 'woocommerce' ), $this );
 43     }
 44 
 45     /**
 46      * Get total stock.
 47      *
 48      * This is the stock of parent and children combined.
 49      *
 50      * @access public
 51      * @return int
 52      */
 53     public function get_total_stock() {
 54 
 55         if ( empty( $this->total_stock ) ) {
 56 
 57             $transient_name = 'wc_product_total_stock_' . $this->id;
 58 
 59             if ( false === ( $this->total_stock = get_transient( $transient_name ) ) ) {
 60                 $this->total_stock = $this->stock;
 61 
 62                 if ( sizeof( $this->get_children() ) > 0 ) {
 63                     foreach ($this->get_children() as $child_id) {
 64                         $stock = get_post_meta( $child_id, '_stock', true );
 65 
 66                         if ( $stock != '' ) {
 67                             $this->total_stock += intval( $stock );
 68                         }
 69                     }
 70                 }
 71 
 72                 set_transient( $transient_name, $this->total_stock, YEAR_IN_SECONDS );
 73             }
 74         }
 75 
 76         return apply_filters( 'woocommerce_stock_amount', $this->total_stock );
 77     }
 78 
 79     /**
 80      * Return the products children posts.
 81      *
 82      * @access public
 83      * @return array
 84      */
 85     public function get_children() {
 86 
 87         if ( ! is_array( $this->children ) ) {
 88 
 89             $this->children = array();
 90 
 91             $transient_name = 'wc_product_children_ids_' . $this->id;
 92 
 93             if ( false === ( $this->children = get_transient( $transient_name ) ) ) {
 94 
 95                 $this->children = get_posts( 'post_parent=' . $this->id . '&post_type=product&orderby=menu_order&order=ASC&fields=ids&post_status=publish&numberposts=-1' );
 96 
 97                 set_transient( $transient_name, $this->children, YEAR_IN_SECONDS );
 98             }
 99         }
100 
101         return (array) $this->children;
102     }
103 
104 
105     /**
106      * get_child function.
107      *
108      * @access public
109      * @param mixed $child_id
110      * @return object WC_Product or WC_Product_variation
111      */
112     public function get_child( $child_id ) {
113         return get_product( $child_id );
114     }
115 
116 
117     /**
118      * Returns whether or not the product has any child product.
119      *
120      * @access public
121      * @return bool
122      */
123     public function has_child() {
124         return sizeof( $this->get_children() ) ? true : false;
125     }
126 
127 
128     /**
129      * Returns whether or not the product is on sale.
130      *
131      * @access public
132      * @return bool
133      */
134     public function is_on_sale() {
135         if ( $this->has_child() ) {
136 
137             foreach ( $this->get_children() as $child_id ) {
138                 $sale_price = get_post_meta( $child_id, '_sale_price', true );
139                 if ( $sale_price !== "" && $sale_price >= 0 )
140                     return true;
141             }
142 
143         } else {
144 
145             if ( $this->sale_price && $this->sale_price == $this->price )
146                 return true;
147 
148         }
149         return false;
150     }
151 
152 
153     /**
154      * Returns false if the product cannot be bought.
155      *
156      * @access public
157      * @return bool
158      */
159     public function is_purchasable() {
160         return apply_filters( 'woocommerce_is_purchasable', false, $this );
161     }
162 
163     /**
164      * Returns the price in html format.
165      *
166      * @access public
167      * @param string $price (default: '')
168      * @return string
169      */
170     public function get_price_html( $price = '' ) {
171         $tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
172         $child_prices     = array();
173 
174         foreach ( $this->get_children() as $child_id )
175             $child_prices[] = get_post_meta( $child_id, '_price', true );
176 
177         $child_prices     = array_unique( $child_prices );
178         $get_price_method = 'get_price_' . $tax_display_mode . 'uding_tax';
179 
180         if ( ! empty( $child_prices ) ) {
181             $min_price = min( $child_prices );
182             $max_price = max( $child_prices );
183         } else {
184             $min_price = '';
185             $max_price = '';
186         }
187 
188         if ( $min_price ) {
189             if ( $min_price == $max_price ) {
190                 $display_price = wc_price( $this->$get_price_method( 1, $min_price ) );
191             } else {
192                 $from          = wc_price( $this->$get_price_method( 1, $min_price ) );
193                 $to            = wc_price( $this->$get_price_method( 1, $max_price ) );
194                 $display_price = sprintf( _x( '%1$s&ndash;%2$s', 'Price range: from-to', 'woocommerce' ), $from, $to );
195             }
196 
197             $price .= $display_price . $this->get_price_suffix();
198 
199             $price = apply_filters( 'woocommerce_grouped_price_html', $price, $this );
200         } else {
201             $price = apply_filters( 'woocommerce_grouped_empty_price_html', '', $this );
202         }
203 
204         return apply_filters( 'woocommerce_get_price_html', $price, $this );
205     }
206 }
207 
WooCommerce API documentation generated by ApiGen 2.8.0