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