Completed
Pull Request — master (#11249)
by
unknown
07:40
created

WC_Product_Grouped::get_children()   D

Complexity

Conditions 9
Paths 41

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 19
c 1
b 0
f 0
nc 41
nop 0
dl 0
loc 30
rs 4.909
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
			$wc_manage_access = is_user_logged_in() && current_user_can( 'manage_woocommerce' ) ? true : false;
54
			
55
			if ( !$wc_manage_access ) $this->children = array_filter( array_map( 'absint', (array) get_transient( $transient_name ) ) );
56
57
        	if ( empty( $this->children ) ) {
58
				
59
				$post_status = array('publish');
60
				
61
				if ( $wc_manage_access ) array_push( $post_status, 'private' );
62
63
        		$args = apply_filters( 'woocommerce_grouped_children_args', array(
64
        			'post_parent' 	=> $this->id,
65
        			'post_type'		=> 'product',
66
        			'orderby'		=> 'menu_order',
67
        			'order'			=> 'ASC',
68
        			'fields'		=> 'ids',
69
        			'post_status'	=> $post_status,
70
        			'numberposts'	=> -1,
71
        		) );
72
73
		        $this->children = get_posts( $args );
74
75
				if ( !$wc_manage_access ) set_transient( $transient_name, $this->children, DAY_IN_SECONDS * 30 );
76
			}
77
		}
78
		return (array) $this->children;
79
	}
80
81
	/**
82
	 * Returns whether or not the product has any child product.
83
	 *
84
	 * @access public
85
	 * @return bool
86
	 */
87
	public function has_child() {
88
		return sizeof( $this->get_children() ) ? true : false;
89
	}
90
91
	/**
92
	 * Returns whether or not the product is on sale.
93
	 *
94
	 * @access public
95
	 * @return bool
96
	 */
97
	public function is_on_sale() {
98
		$is_on_sale = false;
99
100
		if ( $this->has_child() ) {
101
102
			foreach ( $this->get_children() as $child_id ) {
103
				$sale_price = get_post_meta( $child_id, '_sale_price', true );
104
				if ( $sale_price !== "" && $sale_price >= 0 ) {
105
					$is_on_sale = true;
106
				}
107
			}
108
109
		} else {
110
111
			if ( $this->sale_price && $this->sale_price == $this->price ) {
112
				$is_on_sale = true;
113
			}
114
115
		}
116
117
		return apply_filters( 'woocommerce_product_is_on_sale', $is_on_sale, $this );
118
	}
119
120
121
	/**
122
	 * Returns false if the product cannot be bought.
123
	 *
124
	 * @access public
125
	 * @return bool
126
	 */
127
	public function is_purchasable() {
128
		return apply_filters( 'woocommerce_is_purchasable', false, $this );
129
	}
130
131
	/**
132
	 * Returns the price in html format.
133
	 *
134
	 * @access public
135
	 * @param string $price (default: '')
136
	 * @return string
137
	 */
138
	public function get_price_html( $price = '' ) {
139
		$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
140
		$child_prices     = array();
141
142
		foreach ( $this->get_children() as $child_id ) {
143
			$child          = wc_get_product( $child_id );
144
			if ( '' !== $child->get_price() ) {
145
				$child_prices[] = 'incl' === $tax_display_mode ? $child->get_price_including_tax() : $child->get_price_excluding_tax();
146
			}
147
		}
148
149
		if ( ! empty( $child_prices ) ) {
150
			$min_price = min( $child_prices );
151
			$max_price = max( $child_prices );
152
		} else {
153
			$min_price = '';
154
			$max_price = '';
155
		}
156
157
		if ( '' !== $min_price ) {
158
			$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 );
159
			$is_free = $min_price == 0 && $max_price == 0;
160
161
			if ( $is_free ) {
162
				$price = apply_filters( 'woocommerce_grouped_free_price_html', __( 'Free!', 'woocommerce' ), $this );
163
			} else {
164
				$price = apply_filters( 'woocommerce_grouped_price_html', $price . $this->get_price_suffix(), $this );
165
			}
166
		} else {
167
			$price = apply_filters( 'woocommerce_grouped_empty_price_html', '', $this );
168
		}
169
170
		return apply_filters( 'woocommerce_get_price_html', $price, $this );
171
	}
172
}
173