|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
3
|
|
|
exit; |
|
4
|
|
|
} |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Cart Item. |
|
8
|
|
|
* |
|
9
|
|
|
* @version 2.7.0 |
|
10
|
|
|
* @package WooCommerce/Classes |
|
11
|
|
|
* @category Class |
|
12
|
|
|
* @author WooThemes |
|
13
|
|
|
*/ |
|
14
|
|
|
class WC_Cart_Item implements ArrayAccess { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Cart Data array. |
|
18
|
|
|
* @since 2.7.0 |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $data = array( |
|
22
|
|
|
'product_id' => 0, |
|
23
|
|
|
'quantity' => 0, |
|
24
|
|
|
'variation' => array(), |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Product this item represents. |
|
29
|
|
|
* @var WC_Product |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $product = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Constructor. |
|
35
|
|
|
* @param array $data |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( $data = array() ) { |
|
38
|
|
|
$this->set_data( $data ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* offsetGet for ArrayAccess/Backwards compatibility. |
|
43
|
|
|
* @deprecated Add deprecation notices in future release. |
|
44
|
|
|
* @param string $offset |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
public function offsetGet( $offset ) { |
|
48
|
|
|
switch ( $offset ) { |
|
49
|
|
|
case 'data' : |
|
50
|
|
|
return $this->get_product(); |
|
51
|
|
|
case 'variation_id' : |
|
52
|
|
|
return is_callable( array( $this, 'get_variation_id' ) ) ? $this->get_product()->get_variation_id() : 0; |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
return isset( $this->data[ $offset ] ) ? $this->data[ $offset ] : ''; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* offsetSet for ArrayAccess/Backwards compatibility. |
|
59
|
|
|
* @deprecated Add deprecation notices in future release. |
|
60
|
|
|
* @param string $offset |
|
61
|
|
|
* @param mixed $value |
|
62
|
|
|
*/ |
|
63
|
|
|
public function offsetSet( $offset, $value ) { |
|
64
|
|
|
switch ( $offset ) { |
|
65
|
|
|
case 'data' : |
|
66
|
|
|
$this->set_product( $value ); |
|
67
|
|
|
break; |
|
68
|
|
|
case 'variation_id' : |
|
69
|
|
|
$this->set_product( wc_get_product( $value ) ); |
|
|
|
|
|
|
70
|
|
|
break; |
|
71
|
|
|
default : |
|
72
|
|
|
$this->data[ $offset ] = $value; |
|
73
|
|
|
break; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* offsetExists for ArrayAccess |
|
79
|
|
|
* @param string $offset |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function offsetExists( $offset ) { |
|
83
|
|
View Code Duplication |
if ( in_array( $offset, array( 'data' ) ) || isset( $this->data[ $offset ] ) ) { |
|
|
|
|
|
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* offsetUnset for ArrayAccess |
|
91
|
|
|
* @param string $offset |
|
92
|
|
|
*/ |
|
93
|
|
|
public function offsetUnset( $offset ) { |
|
94
|
|
|
unset( $this->data[ $offset ] ); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Gets price of the product. |
|
99
|
|
|
* @return float |
|
100
|
|
|
*/ |
|
101
|
|
|
public function get_price() { |
|
102
|
|
|
return $this->get_product() ? $this->get_product()->get_price() : 0; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Gets price of the product. |
|
107
|
|
|
* @return float |
|
108
|
|
|
*/ |
|
109
|
|
|
public function get_weight() { |
|
110
|
|
|
return $this->get_product() ? $this->get_product()->get_weight() : 0; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Gets price of the product. |
|
115
|
|
|
* @return float |
|
116
|
|
|
*/ |
|
117
|
|
|
public function get_tax_class() { |
|
118
|
|
|
return $this->get_product() ? $this->get_product()->get_tax_class() : ''; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Set product. |
|
123
|
|
|
* @param int $value |
|
124
|
|
|
*/ |
|
125
|
|
|
public function set_product( $value ) { |
|
126
|
|
|
$this->product = $value; |
|
|
|
|
|
|
127
|
|
|
$this->data['product_id'] = is_callable( array( $this->product, 'get_variation_id' ) ) ? $this->product->get_variation_id() : $this->product->get_id(); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get product object. |
|
132
|
|
|
* @return WC_Product |
|
133
|
|
|
*/ |
|
134
|
|
|
public function get_product() { |
|
135
|
|
|
return ! is_null( $this->product ) ? $this->product : ( $this->product = wc_get_product( $this->get_product_id() ) ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get all item data. |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
|
|
public function get_data() { |
|
143
|
|
|
return $this->data; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Product or variation ID this item represents. |
|
148
|
|
|
* @return int |
|
149
|
|
|
*/ |
|
150
|
|
|
public function get_product_id() { |
|
151
|
|
|
return $this->data['product_id']; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Get quantity in cart. |
|
156
|
|
|
* @return int |
|
157
|
|
|
*/ |
|
158
|
|
|
public function get_quantity() { |
|
159
|
|
|
return $this->data['quantity']; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get variation data. |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
public function get_variation() { |
|
167
|
|
|
return $this->data['variation']; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Set product ID. |
|
172
|
|
|
* @param int $value |
|
173
|
|
|
*/ |
|
174
|
|
|
public function set_product_id( $value ) { |
|
175
|
|
|
$this->data['product_id'] = absint( $value ); |
|
176
|
|
|
$this->product = null; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Set Quantity. |
|
181
|
|
|
* @param int $value |
|
182
|
|
|
*/ |
|
183
|
|
|
public function set_quantity( $value ) { |
|
184
|
|
|
$this->data['quantity'] = wc_stock_amount( $value ); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Set variation data. |
|
189
|
|
|
* @param array $value |
|
190
|
|
|
*/ |
|
191
|
|
|
public function set_variation( $value ) { |
|
192
|
|
|
$this->data['variation'] = (array) $value; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Set all data. |
|
197
|
|
|
* @param array $value |
|
|
|
|
|
|
198
|
|
|
*/ |
|
199
|
|
|
public function set_data( $values ) { |
|
200
|
|
|
if ( is_a( $values, 'WC_Cart_Item' ) ) { |
|
201
|
|
|
$values = $values->get_data(); |
|
202
|
|
|
} |
|
203
|
|
|
foreach ( $values as $key => $value ) { |
|
204
|
|
|
if ( in_array( $key, array( 'quantity', 'product_id', 'variation', 'product' ) ) ) { |
|
205
|
|
|
$this->{ "set_$key" }( $value ); |
|
206
|
|
|
} else { |
|
207
|
|
|
$this->data[ $key ] = $value; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: