Completed
Push — master ( a2b835...c7783b )
by Mike
11:26
created

WC_Product_Grouped   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 149
rs 10
wmc 25
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add_to_cart_text() 0 3 1
B get_children() 0 24 4
A has_child() 0 3 2
C is_on_sale() 0 22 7
A is_purchasable() 0 3 1
D get_price_html() 0 34 9
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
103
		} else {
104
105
			if ( $this->sale_price && $this->sale_price == $this->price ) {
106
				$is_on_sale = true;
107
			}
108
109
		}
110
111
		return apply_filters( 'woocommerce_product_is_on_sale', $is_on_sale, $this );
112
	}
113
114
115
	/**
116
	 * Returns false if the product cannot be bought.
117
	 *
118
	 * @access public
119
	 * @return bool
120
	 */
121
	public function is_purchasable() {
122
		return apply_filters( 'woocommerce_is_purchasable', false, $this );
123
	}
124
125
	/**
126
	 * Returns the price in html format.
127
	 *
128
	 * @access public
129
	 * @param string $price (default: '')
130
	 * @return string
131
	 */
132
	public function get_price_html( $price = '' ) {
133
		$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
134
		$child_prices     = array();
135
136
		foreach ( $this->get_children() as $child_id ) {
137
			$child          = wc_get_product( $child_id );
138
			if ( '' !== $child->get_price() ) {
139
				$child_prices[] = 'incl' === $tax_display_mode ? $child->get_price_including_tax() : $child->get_price_excluding_tax();
140
			}
141
		}
142
143
		if ( ! empty( $child_prices ) ) {
144
			$min_price = min( $child_prices );
145
			$max_price = max( $child_prices );
146
		} else {
147
			$min_price = '';
148
			$max_price = '';
149
		}
150
151
		if ( '' !== $min_price ) {
152
			$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 );
153
			$is_free = $min_price == 0 && $max_price == 0;
154
155
			if ( $is_free ) {
156
				$price = apply_filters( 'woocommerce_grouped_free_price_html', __( 'Free!', 'woocommerce' ), $this );
157
			} else {
158
				$price = apply_filters( 'woocommerce_grouped_price_html', $price . $this->get_price_suffix(), $this );
159
			}
160
		} else {
161
			$price = apply_filters( 'woocommerce_grouped_empty_price_html', '', $this );
162
		}
163
164
		return apply_filters( 'woocommerce_get_price_html', $price, $this );
165
	}
166
}
167