Completed
Pull Request — master (#11797)
by Aristeides
08:16
created

WC_Product_Grouped::is_on_sale()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 11
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 20
rs 8.2222
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Grouped Product Class.
9
 *
10
 * Grouped products cannot be purchased - they are wrappers for other products.
11
 *
12
 * @class 		WC_Product_Grouped
13
 * @version		2.3.0
14
 * @package		WooCommerce/Classes/Products
15
 * @category	Class
16
 * @author 		WooThemes
17
 */
18
class WC_Product_Grouped extends WC_Product {
19
20
	/** @public array Array of child products/posts/variations. */
21
	public $children;
22
23
	/**
24
	 * Constructor.
25
	 *
26
	 * @access public
27
	 * @param mixed $product
28
	 */
29
	public function __construct( $product ) {
30
		$this->product_type = 'grouped';
31
		parent::__construct( $product );
32
	}
33
34
	/**
35
	 * Get the add to cart button text.
36
	 *
37
	 * @access public
38
	 * @return string
39
	 */
40
	public function add_to_cart_text() {
41
		return apply_filters( 'woocommerce_product_add_to_cart_text', __( 'View products', 'woocommerce' ), $this );
42
	}
43
44
	/**
45
	 * Return the products children posts.
46
	 *
47
	 * @access public
48
	 * @return array
49
	 */
50
	public function get_children() {
51
		if ( ! is_array( $this->children ) || empty( $this->children ) ) {
52
			$transient_name = 'wc_product_children_' . $this->id;
53
			$this->children = array_filter( array_map( 'absint', (array) get_transient( $transient_name ) ) );
54
55
			if ( empty( $this->children ) ) {
56
57
				$args = apply_filters( 'woocommerce_grouped_children_args', array(
58
					'post_parent' 	=> $this->id,
59
					'post_type'		=> 'product',
60
					'orderby'		=> 'menu_order',
61
					'order'			=> 'ASC',
62
					'fields'		=> 'ids',
63
					'post_status'	=> 'publish',
64
					'numberposts'	=> -1,
65
				) );
66
67
				$this->children = get_posts( $args );
68
69
				set_transient( $transient_name, $this->children, DAY_IN_SECONDS * 30 );
70
			}
71
		}
72
		return (array) $this->children;
73
	}
74
75
	/**
76
	 * Returns whether or not the product has any child product.
77
	 *
78
	 * @access public
79
	 * @return bool
80
	 */
81
	public function has_child() {
82
		return sizeof( $this->get_children() ) ? true : false;
83
	}
84
85
	/**
86
	 * Returns whether or not the product is on sale.
87
	 *
88
	 * @access public
89
	 * @return bool
90
	 */
91
	public function is_on_sale() {
92
		$is_on_sale = false;
93
94
		if ( $this->has_child() ) {
95
96
			foreach ( $this->get_children() as $child_id ) {
97
				$sale_price = get_post_meta( $child_id, '_sale_price', true );
98
				if ( $sale_price !== "" && $sale_price >= 0 ) {
99
					$is_on_sale = true;
100
				}
101
			}
102
		} else {
103
104
			if ( $this->sale_price && $this->sale_price == $this->price ) {
105
				$is_on_sale = true;
106
			}
107
		}
108
109
		return apply_filters( 'woocommerce_product_is_on_sale', $is_on_sale, $this );
110
	}
111
112
113
	/**
114
	 * Returns false if the product cannot be bought.
115
	 *
116
	 * @access public
117
	 * @return bool
118
	 */
119
	public function is_purchasable() {
120
		return apply_filters( 'woocommerce_is_purchasable', false, $this );
121
	}
122
123
	/**
124
	 * Returns the price in html format.
125
	 *
126
	 * @access public
127
	 * @param string $price (default: '')
128
	 * @return string
129
	 */
130
	public function get_price_html( $price = '' ) {
131
		$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
132
		$child_prices     = array();
133
134
		foreach ( $this->get_children() as $child_id ) {
135
			$child          = wc_get_product( $child_id );
136
			if ( '' !== $child->get_price() ) {
137
				$child_prices[] = 'incl' === $tax_display_mode ? $child->get_price_including_tax() : $child->get_price_excluding_tax();
138
			}
139
		}
140
141
		if ( ! empty( $child_prices ) ) {
142
			$min_price = min( $child_prices );
143
			$max_price = max( $child_prices );
144
		} else {
145
			$min_price = '';
146
			$max_price = '';
147
		}
148
149
		if ( '' !== $min_price ) {
150
			$price   = $min_price !== $max_price ? sprintf( _x( '%1$s&ndash;%2$s', 'Price range: from-to', 'woocommerce' ), wc_price( $min_price ), wc_price( $max_price ) ) : wc_price( $min_price );
151
			$is_free = $min_price == 0 && $max_price == 0;
152
153
			if ( $is_free ) {
154
				$price = apply_filters( 'woocommerce_grouped_free_price_html', __( 'Free!', 'woocommerce' ), $this );
155
			} else {
156
				$price = apply_filters( 'woocommerce_grouped_price_html', $price . $this->get_price_suffix(), $this, $child_prices );
157
			}
158
		} else {
159
			$price = apply_filters( 'woocommerce_grouped_empty_price_html', '', $this );
160
		}
161
162
		return apply_filters( 'woocommerce_get_price_html', $price, $this );
163
	}
164
}
165