1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 6 7 8 9 10 11 12 13 14 15
16 class WC_Product_Grouped extends WC_Product {
17
18
19 public $children;
20
21
22 public $total_stock;
23
24 25 26 27 28 29
30 public function __construct( $product ) {
31 $this->product_type = 'grouped';
32 parent::__construct( $product );
33 }
34
35 36 37 38 39 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 47 48 49 50 51 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 81 82 83 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 107 108 109 110 111
112 public function get_child( $child_id ) {
113 return get_product( $child_id );
114 }
115
116
117 118 119 120 121 122
123 public function has_child() {
124 return sizeof( $this->get_children() ) ? true : false;
125 }
126
127
128 129 130 131 132 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 155 156 157 158
159 public function is_purchasable() {
160 return apply_filters( 'woocommerce_is_purchasable', false, $this );
161 }
162
163 164 165 166 167 168 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–%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