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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-cart.php
woocommerce / includes Last commit date
abstracts 6 years ago admin 6 years ago cli 6 years ago customizer 6 years ago data-stores 6 years ago emails 6 years ago export 6 years ago gateways 6 years ago import 6 years ago integrations 6 years ago interfaces 6 years ago legacy 6 years ago libraries 6 years ago log-handlers 6 years ago payment-tokens 6 years ago queue 7 years ago shipping 6 years ago shortcodes 6 years ago theme-support 6 years ago tracks 6 years ago traits 6 years ago walkers 8 years ago wccom-site 6 years ago widgets 6 years ago class-wc-ajax.php 6 years ago class-wc-api.php 6 years ago class-wc-auth.php 7 years ago class-wc-autoloader.php 6 years ago class-wc-background-emailer.php 6 years ago class-wc-background-updater.php 7 years ago class-wc-breadcrumb.php 7 years ago class-wc-cache-helper.php 6 years ago class-wc-cart-fees.php 6 years ago class-wc-cart-session.php 6 years ago class-wc-cart-totals.php 6 years ago class-wc-cart.php 6 years ago class-wc-checkout.php 6 years ago class-wc-cli.php 8 years ago class-wc-comments.php 6 years ago class-wc-countries.php 6 years ago class-wc-coupon.php 6 years ago class-wc-customer-download-log.php 8 years ago class-wc-customer-download.php 6 years ago class-wc-customer.php 7 years ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 6 years ago class-wc-datetime.php 7 years ago class-wc-deprecated-action-hooks.php 8 years ago class-wc-deprecated-filter-hooks.php 7 years ago class-wc-discounts.php 6 years ago class-wc-download-handler.php 6 years ago class-wc-emails.php 6 years ago class-wc-embed.php 8 years ago class-wc-form-handler.php 6 years ago class-wc-frontend-scripts.php 6 years ago class-wc-geo-ip.php 8 years ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 6 years ago class-wc-https.php 8 years ago class-wc-install.php 6 years ago class-wc-integrations.php 6 years ago class-wc-log-levels.php 7 years ago class-wc-logger.php 6 years ago class-wc-meta-data.php 7 years ago class-wc-order-factory.php 6 years ago class-wc-order-item-coupon.php 7 years ago class-wc-order-item-fee.php 7 years ago class-wc-order-item-meta.php 7 years ago class-wc-order-item-product.php 6 years ago class-wc-order-item-shipping.php 7 years ago class-wc-order-item-tax.php 6 years ago class-wc-order-item.php 6 years ago class-wc-order-query.php 7 years ago class-wc-order-refund.php 8 years ago class-wc-order.php 6 years ago class-wc-payment-gateways.php 6 years ago class-wc-payment-tokens.php 6 years ago class-wc-post-data.php 6 years ago class-wc-post-types.php 7 years ago class-wc-privacy-background-process.php 8 years ago class-wc-privacy-erasers.php 6 years ago class-wc-privacy-exporters.php 6 years ago class-wc-privacy.php 6 years ago class-wc-product-attribute.php 7 years ago class-wc-product-download.php 6 years ago class-wc-product-external.php 8 years ago class-wc-product-factory.php 7 years ago class-wc-product-grouped.php 8 years ago class-wc-product-query.php 7 years ago class-wc-product-simple.php 6 years ago class-wc-product-variable.php 6 years ago class-wc-product-variation.php 7 years ago class-wc-query.php 6 years ago class-wc-rate-limiter.php 6 years ago class-wc-regenerate-images-request.php 7 years ago class-wc-regenerate-images.php 7 years ago class-wc-register-wp-admin-settings.php 8 years ago class-wc-rest-authentication.php 6 years ago class-wc-rest-exception.php 6 years ago class-wc-session-handler.php 6 years ago class-wc-shipping-rate.php 8 years ago class-wc-shipping-zone.php 6 years ago class-wc-shipping-zones.php 6 years ago class-wc-shipping.php 6 years ago class-wc-shortcodes.php 6 years ago class-wc-structured-data.php 6 years ago class-wc-tax.php 6 years ago class-wc-template-loader.php 6 years ago class-wc-tracker.php 6 years ago class-wc-validation.php 6 years ago class-wc-webhook.php 6 years ago class-woocommerce.php 6 years ago wc-account-functions.php 6 years ago wc-attribute-functions.php 6 years ago wc-cart-functions.php 6 years ago wc-conditional-functions.php 6 years ago wc-core-functions.php 6 years ago wc-coupon-functions.php 7 years ago wc-deprecated-functions.php 6 years ago wc-formatting-functions.php 6 years ago wc-notice-functions.php 6 years ago wc-order-functions.php 6 years ago wc-order-item-functions.php 6 years ago wc-page-functions.php 6 years ago wc-product-functions.php 6 years ago wc-rest-functions.php 6 years ago wc-stock-functions.php 6 years ago wc-template-functions.php 6 years ago wc-template-hooks.php 6 years ago wc-term-functions.php 6 years ago wc-update-functions.php 6 years ago wc-user-functions.php 6 years ago wc-webhook-functions.php 7 years ago wc-widget-functions.php 8 years ago
class-wc-cart.php
1958 lines
1 <?php
2 /**
3 * WooCommerce cart
4 *
5 * The WooCommerce cart class stores cart data and active coupons as well as handling customer sessions and some cart related urls.
6 * The cart class also has a price calculation function which calls upon other classes to calculate totals.
7 *
8 * @package WooCommerce/Classes
9 * @version 2.1.0
10 */
11
12 defined( 'ABSPATH' ) || exit;
13
14 require_once WC_ABSPATH . 'includes/legacy/class-wc-legacy-cart.php';
15 require_once WC_ABSPATH . 'includes/class-wc-cart-fees.php';
16 require_once WC_ABSPATH . 'includes/class-wc-cart-session.php';
17
18 /**
19 * WC_Cart class.
20 */
21 class WC_Cart extends WC_Legacy_Cart {
22
23 /**
24 * Contains an array of cart items.
25 *
26 * @var array
27 */
28 public $cart_contents = array();
29
30 /**
31 * Contains an array of removed cart items so we can restore them if needed.
32 *
33 * @var array
34 */
35 public $removed_cart_contents = array();
36
37 /**
38 * Contains an array of coupon codes applied to the cart.
39 *
40 * @var array
41 */
42 public $applied_coupons = array();
43
44 /**
45 * Are prices in the cart displayed inc or excl tax?
46 *
47 * @var string
48 */
49 public $tax_display_cart = 'incl';
50
51 /**
52 * This stores the chosen shipping methods for the cart item packages.
53 *
54 * @var array
55 */
56 protected $shipping_methods;
57
58 /**
59 * Total defaults used to reset.
60 *
61 * @var array
62 */
63 protected $default_totals = array(
64 'subtotal' => 0,
65 'subtotal_tax' => 0,
66 'shipping_total' => 0,
67 'shipping_tax' => 0,
68 'shipping_taxes' => array(),
69 'discount_total' => 0,
70 'discount_tax' => 0,
71 'cart_contents_total' => 0,
72 'cart_contents_tax' => 0,
73 'cart_contents_taxes' => array(),
74 'fee_total' => 0,
75 'fee_tax' => 0,
76 'fee_taxes' => array(),
77 'total' => 0,
78 'total_tax' => 0,
79 );
80 /**
81 * Store calculated totals.
82 *
83 * @var array
84 */
85 protected $totals = array();
86
87 /**
88 * Reference to the cart session handling class.
89 *
90 * @var WC_Cart_Session
91 */
92 protected $session;
93
94 /**
95 * Reference to the cart fees API class.
96 *
97 * @var WC_Cart_Fees
98 */
99 protected $fees_api;
100
101 /**
102 * Constructor for the cart class. Loads options and hooks in the init method.
103 */
104 public function __construct() {
105 $this->session = new WC_Cart_Session( $this );
106 $this->fees_api = new WC_Cart_Fees( $this );
107 $this->tax_display_cart = $this->is_tax_displayed();
108
109 // Register hooks for the objects.
110 $this->session->init();
111
112 add_action( 'woocommerce_add_to_cart', array( $this, 'calculate_totals' ), 20, 0 );
113 add_action( 'woocommerce_applied_coupon', array( $this, 'calculate_totals' ), 20, 0 );
114 add_action( 'woocommerce_cart_item_removed', array( $this, 'calculate_totals' ), 20, 0 );
115 add_action( 'woocommerce_cart_item_restored', array( $this, 'calculate_totals' ), 20, 0 );
116 add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_items' ), 1 );
117 add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_coupons' ), 1 );
118 add_action( 'woocommerce_after_checkout_validation', array( $this, 'check_customer_coupons' ), 1, 2 );
119 }
120
121 /**
122 * When cloning, ensure object properties are handled.
123 *
124 * These properties store a reference to the cart, so we use new instead of clone.
125 */
126 public function __clone() {
127 $this->session = clone $this->session;
128 $this->fees_api = clone $this->fees_api;
129 }
130
131 /*
132 |--------------------------------------------------------------------------
133 | Getters.
134 |--------------------------------------------------------------------------
135 |
136 | Methods to retrieve class properties and avoid direct access.
137 */
138
139 /**
140 * Gets cart contents.
141 *
142 * @since 3.2.0
143 * @return array of cart items
144 */
145 public function get_cart_contents() {
146 return apply_filters( 'woocommerce_get_cart_contents', (array) $this->cart_contents );
147 }
148
149 /**
150 * Return items removed from the cart.
151 *
152 * @since 3.2.0
153 * @return array
154 */
155 public function get_removed_cart_contents() {
156 return (array) $this->removed_cart_contents;
157 }
158
159 /**
160 * Gets the array of applied coupon codes.
161 *
162 * @return array of applied coupons
163 */
164 public function get_applied_coupons() {
165 return (array) $this->applied_coupons;
166 }
167
168 /**
169 * Return all calculated coupon totals.
170 *
171 * @since 3.2.0
172 * @return array
173 */
174 public function get_coupon_discount_totals() {
175 return (array) $this->coupon_discount_totals;
176 }
177 /**
178 * Return all calculated coupon tax totals.
179 *
180 * @since 3.2.0
181 * @return array
182 */
183 public function get_coupon_discount_tax_totals() {
184 return (array) $this->coupon_discount_tax_totals;
185 }
186
187 /**
188 * Return all calculated totals.
189 *
190 * @since 3.2.0
191 * @return array
192 */
193 public function get_totals() {
194 return empty( $this->totals ) ? $this->default_totals : $this->totals;
195 }
196
197 /**
198 * Get a total.
199 *
200 * @since 3.2.0
201 * @param string $key Key of element in $totals array.
202 * @return mixed
203 */
204 protected function get_totals_var( $key ) {
205 return isset( $this->totals[ $key ] ) ? $this->totals[ $key ] : $this->default_totals[ $key ];
206 }
207
208 /**
209 * Get subtotal.
210 *
211 * @since 3.2.0
212 * @return float
213 */
214 public function get_subtotal() {
215 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'subtotal' ) );
216 }
217
218 /**
219 * Get subtotal.
220 *
221 * @since 3.2.0
222 * @return float
223 */
224 public function get_subtotal_tax() {
225 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'subtotal_tax' ) );
226 }
227
228 /**
229 * Get discount_total.
230 *
231 * @since 3.2.0
232 * @return float
233 */
234 public function get_discount_total() {
235 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'discount_total' ) );
236 }
237
238 /**
239 * Get discount_tax.
240 *
241 * @since 3.2.0
242 * @return float
243 */
244 public function get_discount_tax() {
245 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'discount_tax' ) );
246 }
247
248 /**
249 * Get shipping_total.
250 *
251 * @since 3.2.0
252 * @return float
253 */
254 public function get_shipping_total() {
255 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'shipping_total' ) );
256 }
257
258 /**
259 * Get shipping_tax.
260 *
261 * @since 3.2.0
262 * @return float
263 */
264 public function get_shipping_tax() {
265 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'shipping_tax' ) );
266 }
267
268 /**
269 * Gets cart total. This is the total of items in the cart, but after discounts. Subtotal is before discounts.
270 *
271 * @since 3.2.0
272 * @return float
273 */
274 public function get_cart_contents_total() {
275 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'cart_contents_total' ) );
276 }
277
278 /**
279 * Gets cart tax amount.
280 *
281 * @since 3.2.0
282 * @return float
283 */
284 public function get_cart_contents_tax() {
285 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'cart_contents_tax' ) );
286 }
287
288 /**
289 * Gets cart total after calculation.
290 *
291 * @since 3.2.0
292 * @param string $context If the context is view, the value will be formatted for display. This keeps it compatible with pre-3.2 versions.
293 * @return float
294 */
295 public function get_total( $context = 'view' ) {
296 $total = apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'total' ) );
297 return 'view' === $context ? apply_filters( 'woocommerce_cart_total', wc_price( $total ) ) : $total;
298 }
299
300 /**
301 * Get total tax amount.
302 *
303 * @since 3.2.0
304 * @return float
305 */
306 public function get_total_tax() {
307 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'total_tax' ) );
308 }
309
310 /**
311 * Get total fee amount.
312 *
313 * @since 3.2.0
314 * @return float
315 */
316 public function get_fee_total() {
317 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'fee_total' ) );
318 }
319
320 /**
321 * Get total fee tax amount.
322 *
323 * @since 3.2.0
324 * @return float
325 */
326 public function get_fee_tax() {
327 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'fee_tax' ) );
328 }
329
330 /**
331 * Get taxes.
332 *
333 * @since 3.2.0
334 */
335 public function get_shipping_taxes() {
336 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'shipping_taxes' ) );
337 }
338
339 /**
340 * Get taxes.
341 *
342 * @since 3.2.0
343 */
344 public function get_cart_contents_taxes() {
345 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'cart_contents_taxes' ) );
346 }
347
348 /**
349 * Get taxes.
350 *
351 * @since 3.2.0
352 */
353 public function get_fee_taxes() {
354 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'fee_taxes' ) );
355 }
356
357 /**
358 * Return whether or not the cart is displaying prices including tax, rather than excluding tax.
359 *
360 * @since 3.3.0
361 * @return bool
362 */
363 public function display_prices_including_tax() {
364 $customer_exempt = $this->get_customer() && $this->get_customer()->get_is_vat_exempt();
365
366 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, 'incl' === $this->tax_display_cart && ! $customer_exempt );
367 }
368
369 /*
370 |--------------------------------------------------------------------------
371 | Setters.
372 |--------------------------------------------------------------------------
373 |
374 | Methods to set class properties and avoid direct access.
375 */
376
377 /**
378 * Sets the contents of the cart.
379 *
380 * @param array $value Cart array.
381 */
382 public function set_cart_contents( $value ) {
383 $this->cart_contents = (array) $value;
384 }
385
386 /**
387 * Set items removed from the cart.
388 *
389 * @since 3.2.0
390 * @param array $value Item array.
391 */
392 public function set_removed_cart_contents( $value = array() ) {
393 $this->removed_cart_contents = (array) $value;
394 }
395
396 /**
397 * Sets the array of applied coupon codes.
398 *
399 * @param array $value List of applied coupon codes.
400 */
401 public function set_applied_coupons( $value = array() ) {
402 $this->applied_coupons = (array) $value;
403 }
404
405 /**
406 * Sets the array of calculated coupon totals.
407 *
408 * @since 3.2.0
409 * @param array $value Value to set.
410 */
411 public function set_coupon_discount_totals( $value = array() ) {
412 $this->coupon_discount_totals = (array) $value;
413 }
414 /**
415 * Sets the array of calculated coupon tax totals.
416 *
417 * @since 3.2.0
418 * @param array $value Value to set.
419 */
420 public function set_coupon_discount_tax_totals( $value = array() ) {
421 $this->coupon_discount_tax_totals = (array) $value;
422 }
423
424 /**
425 * Set all calculated totals.
426 *
427 * @since 3.2.0
428 * @param array $value Value to set.
429 */
430 public function set_totals( $value = array() ) {
431 $this->totals = wp_parse_args( $value, $this->default_totals );
432 }
433
434 /**
435 * Set subtotal.
436 *
437 * @since 3.2.0
438 * @param string $value Value to set.
439 */
440 public function set_subtotal( $value ) {
441 $this->totals['subtotal'] = wc_format_decimal( $value, wc_get_price_decimals() );
442 }
443
444 /**
445 * Set subtotal.
446 *
447 * @since 3.2.0
448 * @param string $value Value to set.
449 */
450 public function set_subtotal_tax( $value ) {
451 $this->totals['subtotal_tax'] = wc_round_tax_total( $value );
452 }
453
454 /**
455 * Set discount_total.
456 *
457 * @since 3.2.0
458 * @param string $value Value to set.
459 */
460 public function set_discount_total( $value ) {
461 $this->totals['discount_total'] = wc_cart_round_discount( $value, wc_get_price_decimals() );
462 }
463
464 /**
465 * Set discount_tax.
466 *
467 * @since 3.2.0
468 * @param string $value Value to set.
469 */
470 public function set_discount_tax( $value ) {
471 $this->totals['discount_tax'] = wc_round_tax_total( $value );
472 }
473
474 /**
475 * Set shipping_total.
476 *
477 * @since 3.2.0
478 * @param string $value Value to set.
479 */
480 public function set_shipping_total( $value ) {
481 $this->totals['shipping_total'] = wc_format_decimal( $value, wc_get_price_decimals() );
482 }
483
484 /**
485 * Set shipping_tax.
486 *
487 * @since 3.2.0
488 * @param string $value Value to set.
489 */
490 public function set_shipping_tax( $value ) {
491 $this->totals['shipping_tax'] = wc_round_tax_total( $value );
492 }
493
494 /**
495 * Set cart_contents_total.
496 *
497 * @since 3.2.0
498 * @param string $value Value to set.
499 */
500 public function set_cart_contents_total( $value ) {
501 $this->totals['cart_contents_total'] = wc_format_decimal( $value, wc_get_price_decimals() );
502 }
503
504 /**
505 * Set cart tax amount.
506 *
507 * @since 3.2.0
508 * @param string $value Value to set.
509 */
510 public function set_cart_contents_tax( $value ) {
511 $this->totals['cart_contents_tax'] = wc_round_tax_total( $value );
512 }
513
514 /**
515 * Set cart total.
516 *
517 * @since 3.2.0
518 * @param string $value Value to set.
519 */
520 public function set_total( $value ) {
521 $this->totals['total'] = wc_format_decimal( $value, wc_get_price_decimals() );
522 }
523
524 /**
525 * Set total tax amount.
526 *
527 * @since 3.2.0
528 * @param string $value Value to set.
529 */
530 public function set_total_tax( $value ) {
531 $this->totals['total_tax'] = wc_round_tax_total( $value );
532 }
533
534 /**
535 * Set fee amount.
536 *
537 * @since 3.2.0
538 * @param string $value Value to set.
539 */
540 public function set_fee_total( $value ) {
541 $this->totals['fee_total'] = wc_format_decimal( $value, wc_get_price_decimals() );
542 }
543
544 /**
545 * Set fee tax.
546 *
547 * @since 3.2.0
548 * @param string $value Value to set.
549 */
550 public function set_fee_tax( $value ) {
551 $this->totals['fee_tax'] = wc_round_tax_total( $value );
552 }
553
554 /**
555 * Set taxes.
556 *
557 * @since 3.2.0
558 * @param array $value Tax values.
559 */
560 public function set_shipping_taxes( $value ) {
561 $this->totals['shipping_taxes'] = (array) $value;
562 }
563
564 /**
565 * Set taxes.
566 *
567 * @since 3.2.0
568 * @param array $value Tax values.
569 */
570 public function set_cart_contents_taxes( $value ) {
571 $this->totals['cart_contents_taxes'] = (array) $value;
572 }
573
574 /**
575 * Set taxes.
576 *
577 * @since 3.2.0
578 * @param array $value Tax values.
579 */
580 public function set_fee_taxes( $value ) {
581 $this->totals['fee_taxes'] = (array) $value;
582 }
583
584 /*
585 |--------------------------------------------------------------------------
586 | Helper methods.
587 |--------------------------------------------------------------------------
588 */
589
590 /**
591 * Returns the cart and shipping taxes, merged.
592 *
593 * @return array merged taxes
594 */
595 public function get_taxes() {
596 return apply_filters( 'woocommerce_cart_get_taxes', wc_array_merge_recursive_numeric( $this->get_shipping_taxes(), $this->get_cart_contents_taxes(), $this->get_fee_taxes() ), $this );
597 }
598
599 /**
600 * Returns the contents of the cart in an array.
601 *
602 * @return array contents of the cart
603 */
604 public function get_cart() {
605 if ( ! did_action( 'wp_loaded' ) ) {
606 wc_doing_it_wrong( __FUNCTION__, __( 'Get cart should not be called before the wp_loaded action.', 'woocommerce' ), '2.3' );
607 }
608 if ( ! did_action( 'woocommerce_load_cart_from_session' ) ) {
609 $this->session->get_cart_from_session();
610 }
611 return array_filter( $this->get_cart_contents() );
612 }
613
614 /**
615 * Returns a specific item in the cart.
616 *
617 * @param string $item_key Cart item key.
618 * @return array Item data
619 */
620 public function get_cart_item( $item_key ) {
621 return isset( $this->cart_contents[ $item_key ] ) ? $this->cart_contents[ $item_key ] : array();
622 }
623
624 /**
625 * Checks if the cart is empty.
626 *
627 * @return bool
628 */
629 public function is_empty() {
630 return 0 === count( $this->get_cart() );
631 }
632
633 /**
634 * Empties the cart and optionally the persistent cart too.
635 *
636 * @param bool $clear_persistent_cart Should the persistant cart be cleared too. Defaults to true.
637 */
638 public function empty_cart( $clear_persistent_cart = true ) {
639
640 do_action( 'woocommerce_before_cart_emptied', $clear_persistent_cart );
641
642 $this->cart_contents = array();
643 $this->removed_cart_contents = array();
644 $this->shipping_methods = array();
645 $this->coupon_discount_totals = array();
646 $this->coupon_discount_tax_totals = array();
647 $this->applied_coupons = array();
648 $this->totals = $this->default_totals;
649
650 if ( $clear_persistent_cart ) {
651 $this->session->persistent_cart_destroy();
652 }
653
654 $this->fees_api->remove_all_fees();
655
656 do_action( 'woocommerce_cart_emptied', $clear_persistent_cart );
657 }
658
659 /**
660 * Get number of items in the cart.
661 *
662 * @return int
663 */
664 public function get_cart_contents_count() {
665 return apply_filters( 'woocommerce_cart_contents_count', array_sum( wp_list_pluck( $this->get_cart(), 'quantity' ) ) );
666 }
667
668 /**
669 * Get weight of items in the cart.
670 *
671 * @since 2.5.0
672 * @return int
673 */
674 public function get_cart_contents_weight() {
675 $weight = 0;
676
677 foreach ( $this->get_cart() as $cart_item_key => $values ) {
678 if ( $values['data']->has_weight() ) {
679 $weight += (float) $values['data']->get_weight() * $values['quantity'];
680 }
681 }
682
683 return apply_filters( 'woocommerce_cart_contents_weight', $weight );
684 }
685
686 /**
687 * Get cart items quantities - merged so we can do accurate stock checks on items across multiple lines.
688 *
689 * @return array
690 */
691 public function get_cart_item_quantities() {
692 $quantities = array();
693
694 foreach ( $this->get_cart() as $cart_item_key => $values ) {
695 $product = $values['data'];
696 $quantities[ $product->get_stock_managed_by_id() ] = isset( $quantities[ $product->get_stock_managed_by_id() ] ) ? $quantities[ $product->get_stock_managed_by_id() ] + $values['quantity'] : $values['quantity'];
697 }
698
699 return $quantities;
700 }
701
702 /**
703 * Check all cart items for errors.
704 */
705 public function check_cart_items() {
706 $return = true;
707 $result = $this->check_cart_item_validity();
708
709 if ( is_wp_error( $result ) ) {
710 wc_add_notice( $result->get_error_message(), 'error' );
711 $return = false;
712 }
713
714 $result = $this->check_cart_item_stock();
715
716 if ( is_wp_error( $result ) ) {
717 wc_add_notice( $result->get_error_message(), 'error' );
718 $return = false;
719 }
720
721 return $return;
722
723 }
724
725 /**
726 * Check cart coupons for errors.
727 */
728 public function check_cart_coupons() {
729 foreach ( $this->get_applied_coupons() as $code ) {
730 $coupon = new WC_Coupon( $code );
731
732 if ( ! $coupon->is_valid() ) {
733 $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_INVALID_REMOVED );
734 $this->remove_coupon( $code );
735 }
736 }
737 }
738
739 /**
740 * Looks through cart items and checks the posts are not trashed or deleted.
741 *
742 * @return bool|WP_Error
743 */
744 public function check_cart_item_validity() {
745 $return = true;
746
747 foreach ( $this->get_cart() as $cart_item_key => $values ) {
748 $product = $values['data'];
749
750 if ( ! $product || ! $product->exists() || 'trash' === $product->get_status() ) {
751 $this->set_quantity( $cart_item_key, 0 );
752 $return = new WP_Error( 'invalid', __( 'An item which is no longer available was removed from your cart.', 'woocommerce' ) );
753 }
754 }
755
756 return $return;
757 }
758
759 /**
760 * Looks through the cart to check each item is in stock. If not, add an error.
761 *
762 * @return bool|WP_Error
763 */
764 public function check_cart_item_stock() {
765 $error = new WP_Error();
766 $product_qty_in_cart = $this->get_cart_item_quantities();
767 $hold_stock_minutes = (int) get_option( 'woocommerce_hold_stock_minutes', 0 );
768 $current_session_order_id = isset( WC()->session->order_awaiting_payment ) ? absint( WC()->session->order_awaiting_payment ) : 0;
769
770 foreach ( $this->get_cart() as $cart_item_key => $values ) {
771 $product = $values['data'];
772
773 // Check stock based on stock-status.
774 if ( ! $product->is_in_stock() ) {
775 /* translators: %s: product name */
776 $error->add( 'out-of-stock', sprintf( __( 'Sorry, "%s" is not in stock. Please edit your cart and try again. We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name() ) );
777 return $error;
778 }
779
780 // We only need to check products managing stock, with a limited stock qty.
781 if ( ! $product->managing_stock() || $product->backorders_allowed() ) {
782 continue;
783 }
784
785 // Check stock based on all items in the cart and consider any held stock within pending orders.
786 $held_stock = ( $hold_stock_minutes > 0 ) ? wc_get_held_stock_quantity( $product, $current_session_order_id ) : 0;
787 $required_stock = $product_qty_in_cart[ $product->get_stock_managed_by_id() ];
788
789 if ( $product->get_stock_quantity() < ( $held_stock + $required_stock ) ) {
790 /* translators: 1: product name 2: quantity in stock */
791 $error->add( 'out-of-stock', sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s available). We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name(), wc_format_stock_quantity_for_display( $product->get_stock_quantity() - $held_stock, $product ) ) );
792 return $error;
793 }
794 }
795
796 return true;
797 }
798
799 /**
800 * Gets and formats a list of cart item data + variations for display on the frontend.
801 *
802 * @param array $cart_item Cart item object.
803 * @param bool $flat Should the data be returned flat or in a list.
804 * @return string
805 */
806 public function get_item_data( $cart_item, $flat = false ) {
807 wc_deprecated_function( 'WC_Cart::get_item_data', '3.3', 'wc_get_formatted_cart_item_data' );
808
809 return wc_get_formatted_cart_item_data( $cart_item, $flat );
810 }
811
812 /**
813 * Gets cross sells based on the items in the cart.
814 *
815 * @return array cross_sells (item ids)
816 */
817 public function get_cross_sells() {
818 $cross_sells = array();
819 $in_cart = array();
820 if ( ! $this->is_empty() ) {
821 foreach ( $this->get_cart() as $cart_item_key => $values ) {
822 if ( $values['quantity'] > 0 ) {
823 $cross_sells = array_merge( $values['data']->get_cross_sell_ids(), $cross_sells );
824 $in_cart[] = $values['product_id'];
825 }
826 }
827 }
828 $cross_sells = array_diff( $cross_sells, $in_cart );
829 return apply_filters( 'woocommerce_cart_crosssell_ids', wp_parse_id_list( $cross_sells ), $this );
830 }
831
832 /**
833 * Gets the url to remove an item from the cart.
834 *
835 * @param string $cart_item_key contains the id of the cart item.
836 * @return string url to page
837 */
838 public function get_remove_url( $cart_item_key ) {
839 wc_deprecated_function( 'WC_Cart::get_remove_url', '3.3', 'wc_get_cart_remove_url' );
840
841 return wc_get_cart_remove_url( $cart_item_key );
842 }
843
844 /**
845 * Gets the url to re-add an item into the cart.
846 *
847 * @param string $cart_item_key Cart item key to undo.
848 * @return string url to page
849 */
850 public function get_undo_url( $cart_item_key ) {
851 wc_deprecated_function( 'WC_Cart::get_undo_url', '3.3', 'wc_get_cart_undo_url' );
852
853 return wc_get_cart_undo_url( $cart_item_key );
854 }
855
856 /**
857 * Get taxes, merged by code, formatted ready for output.
858 *
859 * @return array
860 */
861 public function get_tax_totals() {
862 $taxes = $this->get_taxes();
863 $tax_totals = array();
864
865 foreach ( $taxes as $key => $tax ) {
866 $code = WC_Tax::get_rate_code( $key );
867
868 if ( $code || apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) === $key ) {
869 if ( ! isset( $tax_totals[ $code ] ) ) {
870 $tax_totals[ $code ] = new stdClass();
871 $tax_totals[ $code ]->amount = 0;
872 }
873 $tax_totals[ $code ]->tax_rate_id = $key;
874 $tax_totals[ $code ]->is_compound = WC_Tax::is_compound( $key );
875 $tax_totals[ $code ]->label = WC_Tax::get_rate_label( $key );
876 $tax_totals[ $code ]->amount += wc_round_tax_total( $tax );
877 $tax_totals[ $code ]->formatted_amount = wc_price( $tax_totals[ $code ]->amount );
878 }
879 }
880
881 if ( apply_filters( 'woocommerce_cart_hide_zero_taxes', true ) ) {
882 $amounts = array_filter( wp_list_pluck( $tax_totals, 'amount' ) );
883 $tax_totals = array_intersect_key( $tax_totals, $amounts );
884 }
885
886 return apply_filters( 'woocommerce_cart_tax_totals', $tax_totals, $this );
887 }
888
889 /**
890 * Get all tax classes for items in the cart.
891 *
892 * @return array
893 */
894 public function get_cart_item_tax_classes() {
895 $found_tax_classes = array();
896
897 foreach ( WC()->cart->get_cart() as $item ) {
898 if ( $item['data'] && ( $item['data']->is_taxable() || $item['data']->is_shipping_taxable() ) ) {
899 $found_tax_classes[] = $item['data']->get_tax_class();
900 }
901 }
902
903 return array_unique( $found_tax_classes );
904 }
905
906 /**
907 * Get all tax classes for shipping based on the items in the cart.
908 *
909 * @return array
910 */
911 public function get_cart_item_tax_classes_for_shipping() {
912 $found_tax_classes = array();
913
914 foreach ( WC()->cart->get_cart() as $item ) {
915 if ( $item['data'] && ( $item['data']->is_shipping_taxable() ) ) {
916 $found_tax_classes[] = $item['data']->get_tax_class();
917 }
918 }
919
920 return array_unique( $found_tax_classes );
921 }
922
923 /**
924 * Determines the value that the customer spent and the subtotal
925 * displayed, used for things like coupon validation.
926 *
927 * Since the coupon lines are displayed based on the TAX DISPLAY value
928 * of cart, this is used to determine the spend.
929 *
930 * If cart totals are shown including tax, use the subtotal.
931 * If cart totals are shown excluding tax, use the subtotal ex tax
932 * (tax is shown after coupons).
933 *
934 * @since 2.6.0
935 * @return string
936 */
937 public function get_displayed_subtotal() {
938 return $this->display_prices_including_tax() ? $this->get_subtotal() + $this->get_subtotal_tax() : $this->get_subtotal();
939 }
940
941 /**
942 * Check if product is in the cart and return cart item key.
943 *
944 * Cart item key will be unique based on the item and its properties, such as variations.
945 *
946 * @param mixed $cart_id id of product to find in the cart.
947 * @return string cart item key
948 */
949 public function find_product_in_cart( $cart_id = false ) {
950 if ( false !== $cart_id ) {
951 if ( is_array( $this->cart_contents ) && isset( $this->cart_contents[ $cart_id ] ) ) {
952 return $cart_id;
953 }
954 }
955 return '';
956 }
957
958 /**
959 * Generate a unique ID for the cart item being added.
960 *
961 * @param int $product_id - id of the product the key is being generated for.
962 * @param int $variation_id of the product the key is being generated for.
963 * @param array $variation data for the cart item.
964 * @param array $cart_item_data other cart item data passed which affects this items uniqueness in the cart.
965 * @return string cart item key
966 */
967 public function generate_cart_id( $product_id, $variation_id = 0, $variation = array(), $cart_item_data = array() ) {
968 $id_parts = array( $product_id );
969
970 if ( $variation_id && 0 !== $variation_id ) {
971 $id_parts[] = $variation_id;
972 }
973
974 if ( is_array( $variation ) && ! empty( $variation ) ) {
975 $variation_key = '';
976 foreach ( $variation as $key => $value ) {
977 $variation_key .= trim( $key ) . trim( $value );
978 }
979 $id_parts[] = $variation_key;
980 }
981
982 if ( is_array( $cart_item_data ) && ! empty( $cart_item_data ) ) {
983 $cart_item_data_key = '';
984 foreach ( $cart_item_data as $key => $value ) {
985 if ( is_array( $value ) || is_object( $value ) ) {
986 $value = http_build_query( $value );
987 }
988 $cart_item_data_key .= trim( $key ) . trim( $value );
989
990 }
991 $id_parts[] = $cart_item_data_key;
992 }
993
994 return apply_filters( 'woocommerce_cart_id', md5( implode( '_', $id_parts ) ), $product_id, $variation_id, $variation, $cart_item_data );
995 }
996
997 /**
998 * Add a product to the cart.
999 *
1000 * @throws Exception Plugins can throw an exception to prevent adding to cart.
1001 * @param int $product_id contains the id of the product to add to the cart.
1002 * @param int $quantity contains the quantity of the item to add.
1003 * @param int $variation_id ID of the variation being added to the cart.
1004 * @param array $variation attribute values.
1005 * @param array $cart_item_data extra cart item data we want to pass into the item.
1006 * @return string|bool $cart_item_key
1007 */
1008 public function add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() ) {
1009 try {
1010 $product_id = absint( $product_id );
1011 $variation_id = absint( $variation_id );
1012
1013 // Ensure we don't add a variation to the cart directly by variation ID.
1014 if ( 'product_variation' === get_post_type( $product_id ) ) {
1015 $variation_id = $product_id;
1016 $product_id = wp_get_post_parent_id( $variation_id );
1017 }
1018
1019 $product_data = wc_get_product( $variation_id ? $variation_id : $product_id );
1020 $quantity = apply_filters( 'woocommerce_add_to_cart_quantity', $quantity, $product_id );
1021
1022 if ( $quantity <= 0 || ! $product_data || 'trash' === $product_data->get_status() ) {
1023 return false;
1024 }
1025
1026 // Load cart item data - may be added by other plugins.
1027 $cart_item_data = (array) apply_filters( 'woocommerce_add_cart_item_data', $cart_item_data, $product_id, $variation_id, $quantity );
1028
1029 // Generate a ID based on product ID, variation ID, variation data, and other cart item data.
1030 $cart_id = $this->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );
1031
1032 // Find the cart item key in the existing cart.
1033 $cart_item_key = $this->find_product_in_cart( $cart_id );
1034
1035 // Force quantity to 1 if sold individually and check for existing item in cart.
1036 if ( $product_data->is_sold_individually() ) {
1037 $quantity = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data );
1038 $found_in_cart = apply_filters( 'woocommerce_add_to_cart_sold_individually_found_in_cart', $cart_item_key && $this->cart_contents[ $cart_item_key ]['quantity'] > 0, $product_id, $variation_id, $cart_item_data, $cart_id );
1039
1040 if ( $found_in_cart ) {
1041 /* translators: %s: product name */
1042 throw new Exception( sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', wc_get_cart_url(), __( 'View cart', 'woocommerce' ), sprintf( __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product_data->get_name() ) ) );
1043 }
1044 }
1045
1046 if ( ! $product_data->is_purchasable() ) {
1047 $message = __( 'Sorry, this product cannot be purchased.', 'woocommerce' );
1048 /**
1049 * Filters message about product unable to be purchased.
1050 *
1051 * @since 3.8.0
1052 * @param string $message Message.
1053 * @param WC_Product $product_data Product data.
1054 */
1055 $message = apply_filters( 'woocommerce_cart_product_cannot_be_purchased_message', $message, $product_data );
1056 throw new Exception( $message );
1057 }
1058
1059 // Stock check - only check if we're managing stock and backorders are not allowed.
1060 if ( ! $product_data->is_in_stock() ) {
1061 /* translators: %s: product name */
1062 throw new Exception( sprintf( __( 'You cannot add &quot;%s&quot; to the cart because the product is out of stock.', 'woocommerce' ), $product_data->get_name() ) );
1063 }
1064
1065 if ( ! $product_data->has_enough_stock( $quantity ) ) {
1066 /* translators: 1: product name 2: quantity in stock */
1067 throw new Exception( sprintf( __( 'You cannot add that amount of &quot;%1$s&quot; to the cart because there is not enough stock (%2$s remaining).', 'woocommerce' ), $product_data->get_name(), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity(), $product_data ) ) );
1068 }
1069
1070 // Stock check - this time accounting for whats already in-cart.
1071 if ( $product_data->managing_stock() ) {
1072 $products_qty_in_cart = $this->get_cart_item_quantities();
1073
1074 if ( isset( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ] ) && ! $product_data->has_enough_stock( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ] + $quantity ) ) {
1075 throw new Exception(
1076 sprintf(
1077 '<a href="%s" class="button wc-forward">%s</a> %s',
1078 wc_get_cart_url(),
1079 __( 'View cart', 'woocommerce' ),
1080 /* translators: 1: quantity in stock 2: current quantity */
1081 sprintf( __( 'You cannot add that amount to the cart &mdash; we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce' ), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity(), $product_data ), wc_format_stock_quantity_for_display( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ], $product_data ) )
1082 )
1083 );
1084 }
1085 }
1086
1087 // If cart_item_key is set, the item is already in the cart.
1088 if ( $cart_item_key ) {
1089 $new_quantity = $quantity + $this->cart_contents[ $cart_item_key ]['quantity'];
1090 $this->set_quantity( $cart_item_key, $new_quantity, false );
1091 } else {
1092 $cart_item_key = $cart_id;
1093
1094 // Add item after merging with $cart_item_data - hook to allow plugins to modify cart item.
1095 $this->cart_contents[ $cart_item_key ] = apply_filters(
1096 'woocommerce_add_cart_item',
1097 array_merge(
1098 $cart_item_data,
1099 array(
1100 'key' => $cart_item_key,
1101 'product_id' => $product_id,
1102 'variation_id' => $variation_id,
1103 'variation' => $variation,
1104 'quantity' => $quantity,
1105 'data' => $product_data,
1106 'data_hash' => wc_get_cart_item_data_hash( $product_data ),
1107 )
1108 ),
1109 $cart_item_key
1110 );
1111 }
1112
1113 $this->cart_contents = apply_filters( 'woocommerce_cart_contents_changed', $this->cart_contents );
1114
1115 do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
1116
1117 return $cart_item_key;
1118
1119 } catch ( Exception $e ) {
1120 if ( $e->getMessage() ) {
1121 wc_add_notice( $e->getMessage(), 'error' );
1122 }
1123 return false;
1124 }
1125 }
1126
1127 /**
1128 * Remove a cart item.
1129 *
1130 * @since 2.3.0
1131 * @param string $cart_item_key Cart item key to remove from the cart.
1132 * @return bool
1133 */
1134 public function remove_cart_item( $cart_item_key ) {
1135 if ( isset( $this->cart_contents[ $cart_item_key ] ) ) {
1136 $this->removed_cart_contents[ $cart_item_key ] = $this->cart_contents[ $cart_item_key ];
1137
1138 unset( $this->removed_cart_contents[ $cart_item_key ]['data'] );
1139
1140 do_action( 'woocommerce_remove_cart_item', $cart_item_key, $this );
1141
1142 unset( $this->cart_contents[ $cart_item_key ] );
1143
1144 do_action( 'woocommerce_cart_item_removed', $cart_item_key, $this );
1145
1146 return true;
1147 }
1148 return false;
1149 }
1150
1151 /**
1152 * Restore a cart item.
1153 *
1154 * @param string $cart_item_key Cart item key to restore to the cart.
1155 * @return bool
1156 */
1157 public function restore_cart_item( $cart_item_key ) {
1158 if ( isset( $this->removed_cart_contents[ $cart_item_key ] ) ) {
1159 $restore_item = $this->removed_cart_contents[ $cart_item_key ];
1160 $this->cart_contents[ $cart_item_key ] = $restore_item;
1161 $this->cart_contents[ $cart_item_key ]['data'] = wc_get_product( $restore_item['variation_id'] ? $restore_item['variation_id'] : $restore_item['product_id'] );
1162
1163 do_action( 'woocommerce_restore_cart_item', $cart_item_key, $this );
1164
1165 unset( $this->removed_cart_contents[ $cart_item_key ] );
1166
1167 do_action( 'woocommerce_cart_item_restored', $cart_item_key, $this );
1168
1169 return true;
1170 }
1171 return false;
1172 }
1173
1174 /**
1175 * Set the quantity for an item in the cart using it's key.
1176 *
1177 * @param string $cart_item_key contains the id of the cart item.
1178 * @param int $quantity contains the quantity of the item.
1179 * @param bool $refresh_totals whether or not to calculate totals after setting the new qty. Can be used to defer calculations if setting quantities in bulk.
1180 * @return bool
1181 */
1182 public function set_quantity( $cart_item_key, $quantity = 1, $refresh_totals = true ) {
1183 if ( 0 === $quantity || $quantity < 0 ) {
1184 wc_do_deprecated_action( 'woocommerce_before_cart_item_quantity_zero', array( $cart_item_key, $this ), '3.7.0', 'woocommerce_remove_cart_item' );
1185 // If we're setting qty to 0 we're removing the item from the cart.
1186 return $this->remove_cart_item( $cart_item_key );
1187 }
1188
1189 // Update qty.
1190 $old_quantity = $this->cart_contents[ $cart_item_key ]['quantity'];
1191 $this->cart_contents[ $cart_item_key ]['quantity'] = $quantity;
1192
1193 do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity, $old_quantity, $this );
1194
1195 if ( $refresh_totals ) {
1196 $this->calculate_totals();
1197 }
1198
1199 /**
1200 * Fired after qty has been changed.
1201 *
1202 * @since 3.6.0
1203 * @param string $cart_item_key contains the id of the cart item. This may be empty if the cart item does not exist any more.
1204 * @param int $quantity contains the quantity of the item.
1205 * @param WC_Cart $this Cart class.
1206 */
1207 do_action( 'woocommerce_cart_item_set_quantity', $cart_item_key, $quantity, $this );
1208
1209 return true;
1210 }
1211
1212 /**
1213 * Get cart's owner.
1214 *
1215 * @since 3.2.0
1216 * @return WC_Customer
1217 */
1218 public function get_customer() {
1219 return WC()->customer;
1220 }
1221
1222 /**
1223 * Calculate totals for the items in the cart.
1224 *
1225 * @uses WC_Cart_Totals
1226 */
1227 public function calculate_totals() {
1228 $this->reset_totals();
1229
1230 if ( $this->is_empty() ) {
1231 $this->session->set_session();
1232 return;
1233 }
1234
1235 do_action( 'woocommerce_before_calculate_totals', $this );
1236
1237 new WC_Cart_Totals( $this );
1238
1239 do_action( 'woocommerce_after_calculate_totals', $this );
1240 }
1241
1242 /**
1243 * Looks at the totals to see if payment is actually required.
1244 *
1245 * @return bool
1246 */
1247 public function needs_payment() {
1248 return apply_filters( 'woocommerce_cart_needs_payment', 0 < $this->get_total( 'edit' ), $this );
1249 }
1250
1251 /*
1252 * Shipping related functions.
1253 */
1254
1255 /**
1256 * Uses the shipping class to calculate shipping then gets the totals when its finished.
1257 */
1258 public function calculate_shipping() {
1259 $this->shipping_methods = $this->needs_shipping() ? $this->get_chosen_shipping_methods( WC()->shipping()->calculate_shipping( $this->get_shipping_packages() ) ) : array();
1260
1261 $shipping_taxes = wp_list_pluck( $this->shipping_methods, 'taxes' );
1262 $merged_taxes = array();
1263 foreach ( $shipping_taxes as $taxes ) {
1264 foreach ( $taxes as $tax_id => $tax_amount ) {
1265 if ( ! isset( $merged_taxes[ $tax_id ] ) ) {
1266 $merged_taxes[ $tax_id ] = 0;
1267 }
1268 $merged_taxes[ $tax_id ] += $tax_amount;
1269 }
1270 }
1271
1272 $this->set_shipping_total( array_sum( wp_list_pluck( $this->shipping_methods, 'cost' ) ) );
1273 $this->set_shipping_tax( array_sum( $merged_taxes ) );
1274 $this->set_shipping_taxes( $merged_taxes );
1275
1276 return $this->shipping_methods;
1277 }
1278
1279 /**
1280 * Given a set of packages with rates, get the chosen ones only.
1281 *
1282 * @since 3.2.0
1283 * @param array $calculated_shipping_packages Array of packages.
1284 * @return array
1285 */
1286 protected function get_chosen_shipping_methods( $calculated_shipping_packages = array() ) {
1287 $chosen_methods = array();
1288 // Get chosen methods for each package to get our totals.
1289 foreach ( $calculated_shipping_packages as $key => $package ) {
1290 $chosen_method = wc_get_chosen_shipping_method_for_package( $key, $package );
1291 if ( $chosen_method ) {
1292 $chosen_methods[ $key ] = $package['rates'][ $chosen_method ];
1293 }
1294 }
1295 return $chosen_methods;
1296 }
1297
1298 /**
1299 * Filter items needing shipping callback.
1300 *
1301 * @since 3.0.0
1302 * @param array $item Item to check for shipping.
1303 * @return bool
1304 */
1305 protected function filter_items_needing_shipping( $item ) {
1306 $product = $item['data'];
1307 return $product && $product->needs_shipping();
1308 }
1309
1310 /**
1311 * Get only items that need shipping.
1312 *
1313 * @since 3.0.0
1314 * @return array
1315 */
1316 protected function get_items_needing_shipping() {
1317 return array_filter( $this->get_cart(), array( $this, 'filter_items_needing_shipping' ) );
1318 }
1319
1320 /**
1321 * Get packages to calculate shipping for.
1322 *
1323 * This lets us calculate costs for carts that are shipped to multiple locations.
1324 *
1325 * Shipping methods are responsible for looping through these packages.
1326 *
1327 * By default we pass the cart itself as a package - plugins can change this.
1328 * through the filter and break it up.
1329 *
1330 * @since 1.5.4
1331 * @return array of cart items
1332 */
1333 public function get_shipping_packages() {
1334 return apply_filters(
1335 'woocommerce_cart_shipping_packages',
1336 array(
1337 array(
1338 'contents' => $this->get_items_needing_shipping(),
1339 'contents_cost' => array_sum( wp_list_pluck( $this->get_items_needing_shipping(), 'line_total' ) ),
1340 'applied_coupons' => $this->get_applied_coupons(),
1341 'user' => array(
1342 'ID' => get_current_user_id(),
1343 ),
1344 'destination' => array(
1345 'country' => $this->get_customer()->get_shipping_country(),
1346 'state' => $this->get_customer()->get_shipping_state(),
1347 'postcode' => $this->get_customer()->get_shipping_postcode(),
1348 'city' => $this->get_customer()->get_shipping_city(),
1349 'address' => $this->get_customer()->get_shipping_address(),
1350 'address_1' => $this->get_customer()->get_shipping_address(), // Provide both address and address_1 for backwards compatibility.
1351 'address_2' => $this->get_customer()->get_shipping_address_2(),
1352 ),
1353 'cart_subtotal' => $this->get_displayed_subtotal(),
1354 ),
1355 )
1356 );
1357 }
1358
1359 /**
1360 * Looks through the cart to see if shipping is actually required.
1361 *
1362 * @return bool whether or not the cart needs shipping
1363 */
1364 public function needs_shipping() {
1365 if ( ! wc_shipping_enabled() || 0 === wc_get_shipping_method_count( true ) ) {
1366 return false;
1367 }
1368 $needs_shipping = false;
1369
1370 foreach ( $this->get_cart_contents() as $cart_item_key => $values ) {
1371 if ( $values['data']->needs_shipping() ) {
1372 $needs_shipping = true;
1373 break;
1374 }
1375 }
1376
1377 return apply_filters( 'woocommerce_cart_needs_shipping', $needs_shipping );
1378 }
1379
1380 /**
1381 * Should the shipping address form be shown.
1382 *
1383 * @return bool
1384 */
1385 public function needs_shipping_address() {
1386 return apply_filters( 'woocommerce_cart_needs_shipping_address', true === $this->needs_shipping() && ! wc_ship_to_billing_address_only() );
1387 }
1388
1389 /**
1390 * Sees if the customer has entered enough data to calc the shipping yet.
1391 *
1392 * @return bool
1393 */
1394 public function show_shipping() {
1395 if ( ! wc_shipping_enabled() || ! $this->get_cart_contents() ) {
1396 return false;
1397 }
1398
1399 if ( 'yes' === get_option( 'woocommerce_shipping_cost_requires_address' ) ) {
1400 if ( ! $this->get_customer()->has_calculated_shipping() ) {
1401 if ( ! $this->get_customer()->get_shipping_country() || ( ! $this->get_customer()->get_shipping_state() && ! $this->get_customer()->get_shipping_postcode() ) ) {
1402 return false;
1403 }
1404 }
1405 }
1406
1407 return apply_filters( 'woocommerce_cart_ready_to_calc_shipping', true );
1408 }
1409
1410 /**
1411 * Gets the shipping total (after calculation).
1412 *
1413 * @return string price or string for the shipping total
1414 */
1415 public function get_cart_shipping_total() {
1416
1417 // Default total assumes Free shipping.
1418 $total = __( 'Free!', 'woocommerce' );
1419
1420 if ( 0 < $this->get_shipping_total() ) {
1421
1422 if ( $this->display_prices_including_tax() ) {
1423 $total = wc_price( $this->shipping_total + $this->shipping_tax_total );
1424
1425 if ( $this->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
1426 $total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
1427 }
1428 } else {
1429 $total = wc_price( $this->shipping_total );
1430
1431 if ( $this->shipping_tax_total > 0 && wc_prices_include_tax() ) {
1432 $total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
1433 }
1434 }
1435 }
1436 return apply_filters( 'woocommerce_cart_shipping_total', $total, $this );
1437 }
1438
1439 /**
1440 * Check for user coupons (now that we have billing email). If a coupon is invalid, add an error.
1441 *
1442 * Checks two types of coupons:
1443 * 1. Where a list of customer emails are set (limits coupon usage to those defined).
1444 * 2. Where a usage_limit_per_user is set (limits coupon usage to a number based on user ID and email).
1445 *
1446 * @param array $posted Post data.
1447 */
1448 public function check_customer_coupons( $posted ) {
1449 foreach ( $this->get_applied_coupons() as $code ) {
1450 $coupon = new WC_Coupon( $code );
1451
1452 if ( $coupon->is_valid() ) {
1453
1454 // Get user and posted emails to compare.
1455 $current_user = wp_get_current_user();
1456 $billing_email = isset( $posted['billing_email'] ) ? $posted['billing_email'] : '';
1457 $check_emails = array_unique(
1458 array_filter(
1459 array_map(
1460 'strtolower',
1461 array_map(
1462 'sanitize_email',
1463 array(
1464 $billing_email,
1465 $current_user->user_email,
1466 )
1467 )
1468 )
1469 )
1470 );
1471
1472 // Limit to defined email addresses.
1473 $restrictions = $coupon->get_email_restrictions();
1474
1475 if ( is_array( $restrictions ) && 0 < count( $restrictions ) && ! $this->is_coupon_emails_allowed( $check_emails, $restrictions ) ) {
1476 $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED );
1477 $this->remove_coupon( $code );
1478 }
1479 }
1480 }
1481 }
1482
1483 /**
1484 * Checks if the given email address(es) matches the ones specified on the coupon.
1485 *
1486 * @param array $check_emails Array of customer email addresses.
1487 * @param array $restrictions Array of allowed email addresses.
1488 * @return bool
1489 */
1490 public function is_coupon_emails_allowed( $check_emails, $restrictions ) {
1491
1492 foreach ( $check_emails as $check_email ) {
1493 // With a direct match we return true.
1494 if ( in_array( $check_email, $restrictions, true ) ) {
1495 return true;
1496 }
1497
1498 // Go through the allowed emails and return true if the email matches a wildcard.
1499 foreach ( $restrictions as $restriction ) {
1500 // Convert to PHP-regex syntax.
1501 $regex = '/^' . str_replace( '*', '(.+)?', $restriction ) . '$/';
1502 preg_match( $regex, $check_email, $match );
1503 if ( ! empty( $match ) ) {
1504 return true;
1505 }
1506 }
1507 }
1508
1509 // No matches, this one isn't allowed.
1510 return false;
1511 }
1512
1513
1514 /**
1515 * Returns whether or not a discount has been applied.
1516 *
1517 * @param string $coupon_code Coupon code to check.
1518 * @return bool
1519 */
1520 public function has_discount( $coupon_code = '' ) {
1521 return $coupon_code ? in_array( wc_format_coupon_code( $coupon_code ), $this->applied_coupons, true ) : count( $this->applied_coupons ) > 0;
1522 }
1523
1524 /**
1525 * Applies a coupon code passed to the method.
1526 *
1527 * @param string $coupon_code - The code to apply.
1528 * @return bool True if the coupon is applied, false if it does not exist or cannot be applied.
1529 */
1530 public function apply_coupon( $coupon_code ) {
1531 // Coupons are globally disabled.
1532 if ( ! wc_coupons_enabled() ) {
1533 return false;
1534 }
1535
1536 // Sanitize coupon code.
1537 $coupon_code = wc_format_coupon_code( $coupon_code );
1538
1539 // Get the coupon.
1540 $the_coupon = new WC_Coupon( $coupon_code );
1541
1542 // Prevent adding coupons by post ID.
1543 if ( $the_coupon->get_code() !== $coupon_code ) {
1544 $the_coupon->set_code( $coupon_code );
1545 $the_coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_EXIST );
1546 return false;
1547 }
1548
1549 // Check it can be used with cart.
1550 if ( ! $the_coupon->is_valid() ) {
1551 wc_add_notice( $the_coupon->get_error_message(), 'error' );
1552 return false;
1553 }
1554
1555 // Check if applied.
1556 if ( $this->has_discount( $coupon_code ) ) {
1557 $the_coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED );
1558 return false;
1559 }
1560
1561 // If its individual use then remove other coupons.
1562 if ( $the_coupon->get_individual_use() ) {
1563 $coupons_to_keep = apply_filters( 'woocommerce_apply_individual_use_coupon', array(), $the_coupon, $this->applied_coupons );
1564
1565 foreach ( $this->applied_coupons as $applied_coupon ) {
1566 $keep_key = array_search( $applied_coupon, $coupons_to_keep, true );
1567 if ( false === $keep_key ) {
1568 $this->remove_coupon( $applied_coupon );
1569 } else {
1570 unset( $coupons_to_keep[ $keep_key ] );
1571 }
1572 }
1573
1574 if ( ! empty( $coupons_to_keep ) ) {
1575 $this->applied_coupons += $coupons_to_keep;
1576 }
1577 }
1578
1579 // Check to see if an individual use coupon is set.
1580 if ( $this->applied_coupons ) {
1581 foreach ( $this->applied_coupons as $code ) {
1582 $coupon = new WC_Coupon( $code );
1583
1584 if ( $coupon->get_individual_use() && false === apply_filters( 'woocommerce_apply_with_individual_use_coupon', false, $the_coupon, $coupon, $this->applied_coupons ) ) {
1585
1586 // Reject new coupon.
1587 $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY );
1588
1589 return false;
1590 }
1591 }
1592 }
1593
1594 $this->applied_coupons[] = $coupon_code;
1595
1596 // Choose free shipping.
1597 if ( $the_coupon->get_free_shipping() ) {
1598 $packages = WC()->shipping()->get_packages();
1599 $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
1600
1601 foreach ( $packages as $i => $package ) {
1602 $chosen_shipping_methods[ $i ] = 'free_shipping';
1603 }
1604
1605 WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
1606 }
1607
1608 $the_coupon->add_coupon_message( WC_Coupon::WC_COUPON_SUCCESS );
1609
1610 do_action( 'woocommerce_applied_coupon', $coupon_code );
1611
1612 return true;
1613 }
1614
1615 /**
1616 * Get array of applied coupon objects and codes.
1617 *
1618 * @param null $deprecated No longer used.
1619 * @return array of applied coupons
1620 */
1621 public function get_coupons( $deprecated = null ) {
1622 $coupons = array();
1623
1624 if ( 'order' === $deprecated ) {
1625 return $coupons;
1626 }
1627
1628 foreach ( $this->get_applied_coupons() as $code ) {
1629 $coupon = new WC_Coupon( $code );
1630 $coupons[ $code ] = $coupon;
1631 }
1632
1633 return $coupons;
1634 }
1635
1636 /**
1637 * Get the discount amount for a used coupon.
1638 *
1639 * @param string $code coupon code.
1640 * @param bool $ex_tax inc or ex tax.
1641 * @return float discount amount
1642 */
1643 public function get_coupon_discount_amount( $code, $ex_tax = true ) {
1644 $totals = $this->get_coupon_discount_totals();
1645 $discount_amount = isset( $totals[ $code ] ) ? $totals[ $code ] : 0;
1646
1647 if ( ! $ex_tax ) {
1648 $discount_amount += $this->get_coupon_discount_tax_amount( $code );
1649 }
1650
1651 return wc_cart_round_discount( $discount_amount, wc_get_price_decimals() );
1652 }
1653
1654 /**
1655 * Get the discount tax amount for a used coupon (for tax inclusive prices).
1656 *
1657 * @param string $code coupon code.
1658 * @return float discount amount
1659 */
1660 public function get_coupon_discount_tax_amount( $code ) {
1661 $totals = $this->get_coupon_discount_tax_totals();
1662 return wc_cart_round_discount( isset( $totals[ $code ] ) ? $totals[ $code ] : 0, wc_get_price_decimals() );
1663 }
1664
1665 /**
1666 * Remove coupons from the cart of a defined type. Type 1 is before tax, type 2 is after tax.
1667 *
1668 * @param null $deprecated No longer used.
1669 */
1670 public function remove_coupons( $deprecated = null ) {
1671 $this->set_coupon_discount_totals( array() );
1672 $this->set_coupon_discount_tax_totals( array() );
1673 $this->set_applied_coupons( array() );
1674 $this->session->set_session();
1675 }
1676
1677 /**
1678 * Remove a single coupon by code.
1679 *
1680 * @param string $coupon_code Code of the coupon to remove.
1681 * @return bool
1682 */
1683 public function remove_coupon( $coupon_code ) {
1684 $coupon_code = wc_format_coupon_code( $coupon_code );
1685 $position = array_search( $coupon_code, $this->get_applied_coupons(), true );
1686
1687 if ( false !== $position ) {
1688 unset( $this->applied_coupons[ $position ] );
1689 }
1690
1691 WC()->session->set( 'refresh_totals', true );
1692
1693 do_action( 'woocommerce_removed_coupon', $coupon_code );
1694
1695 return true;
1696 }
1697
1698 /**
1699 * Trigger an action so 3rd parties can add custom fees.
1700 *
1701 * @since 2.0.0
1702 */
1703 public function calculate_fees() {
1704 do_action( 'woocommerce_cart_calculate_fees', $this );
1705 }
1706
1707 /**
1708 * Return reference to fees API.
1709 *
1710 * @since 3.2.0
1711 * @return WC_Cart_Fees
1712 */
1713 public function fees_api() {
1714 return $this->fees_api;
1715 }
1716
1717 /**
1718 * Add additional fee to the cart.
1719 *
1720 * This method should be called on a callback attached to the
1721 * woocommerce_cart_calculate_fees action during cart/checkout. Fees do not
1722 * persist.
1723 *
1724 * @uses WC_Cart_Fees::add_fee
1725 * @param string $name Unique name for the fee. Multiple fees of the same name cannot be added.
1726 * @param float $amount Fee amount (do not enter negative amounts).
1727 * @param bool $taxable Is the fee taxable? (default: false).
1728 * @param string $tax_class The tax class for the fee if taxable. A blank string is standard tax class. (default: '').
1729 */
1730 public function add_fee( $name, $amount, $taxable = false, $tax_class = '' ) {
1731 $this->fees_api()->add_fee(
1732 array(
1733 'name' => $name,
1734 'amount' => (float) $amount,
1735 'taxable' => $taxable,
1736 'tax_class' => $tax_class,
1737 )
1738 );
1739 }
1740
1741 /**
1742 * Return all added fees from the Fees API.
1743 *
1744 * @uses WC_Cart_Fees::get_fees
1745 * @return array
1746 */
1747 public function get_fees() {
1748 $fees = $this->fees_api()->get_fees();
1749
1750 if ( property_exists( $this, 'fees' ) ) {
1751 $fees = $fees + (array) $this->fees;
1752 }
1753 return $fees;
1754 }
1755
1756 /**
1757 * Gets the total excluding taxes.
1758 *
1759 * @return string formatted price
1760 */
1761 public function get_total_ex_tax() {
1762 return apply_filters( 'woocommerce_cart_total_ex_tax', wc_price( max( 0, $this->get_total( 'edit' ) - $this->get_total_tax() ) ) );
1763 }
1764
1765 /**
1766 * Gets the cart contents total (after calculation).
1767 *
1768 * @return string formatted price
1769 */
1770 public function get_cart_total() {
1771 return apply_filters( 'woocommerce_cart_contents_total', wc_price( wc_prices_include_tax() ? $this->get_cart_contents_total() + $this->get_cart_contents_tax() : $this->get_cart_contents_total() ) );
1772 }
1773
1774 /**
1775 * Gets the sub total (after calculation).
1776 *
1777 * @param bool $compound whether to include compound taxes.
1778 * @return string formatted price
1779 */
1780 public function get_cart_subtotal( $compound = false ) {
1781 /**
1782 * If the cart has compound tax, we want to show the subtotal as cart + shipping + non-compound taxes (after discount).
1783 */
1784 if ( $compound ) {
1785 $cart_subtotal = wc_price( $this->get_cart_contents_total() + $this->get_shipping_total() + $this->get_taxes_total( false, false ) );
1786
1787 } elseif ( $this->display_prices_including_tax() ) {
1788 $cart_subtotal = wc_price( $this->get_subtotal() + $this->get_subtotal_tax() );
1789
1790 if ( $this->get_subtotal_tax() > 0 && ! wc_prices_include_tax() ) {
1791 $cart_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
1792 }
1793 } else {
1794 $cart_subtotal = wc_price( $this->get_subtotal() );
1795
1796 if ( $this->get_subtotal_tax() > 0 && wc_prices_include_tax() ) {
1797 $cart_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
1798 }
1799 }
1800
1801 return apply_filters( 'woocommerce_cart_subtotal', $cart_subtotal, $compound, $this );
1802 }
1803
1804 /**
1805 * Get the product row price per item.
1806 *
1807 * @param WC_Product $product Product object.
1808 * @return string formatted price
1809 */
1810 public function get_product_price( $product ) {
1811 if ( $this->display_prices_including_tax() ) {
1812 $product_price = wc_get_price_including_tax( $product );
1813 } else {
1814 $product_price = wc_get_price_excluding_tax( $product );
1815 }
1816 return apply_filters( 'woocommerce_cart_product_price', wc_price( $product_price ), $product );
1817 }
1818
1819 /**
1820 * Get the product row subtotal.
1821 *
1822 * Gets the tax etc to avoid rounding issues.
1823 *
1824 * When on the checkout (review order), this will get the subtotal based on the customer's tax rate rather than the base rate.
1825 *
1826 * @param WC_Product $product Product object.
1827 * @param int $quantity Quantity being purchased.
1828 * @return string formatted price
1829 */
1830 public function get_product_subtotal( $product, $quantity ) {
1831 $price = $product->get_price();
1832
1833 if ( $product->is_taxable() ) {
1834
1835 if ( $this->display_prices_including_tax() ) {
1836 $row_price = wc_get_price_including_tax( $product, array( 'qty' => $quantity ) );
1837 $product_subtotal = wc_price( $row_price );
1838
1839 if ( ! wc_prices_include_tax() && $this->get_subtotal_tax() > 0 ) {
1840 $product_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
1841 }
1842 } else {
1843 $row_price = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
1844 $product_subtotal = wc_price( $row_price );
1845
1846 if ( wc_prices_include_tax() && $this->get_subtotal_tax() > 0 ) {
1847 $product_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
1848 }
1849 }
1850 } else {
1851 $row_price = $price * $quantity;
1852 $product_subtotal = wc_price( $row_price );
1853 }
1854
1855 return apply_filters( 'woocommerce_cart_product_subtotal', $product_subtotal, $product, $quantity, $this );
1856 }
1857
1858 /**
1859 * Gets the cart tax (after calculation).
1860 *
1861 * @return string formatted price
1862 */
1863 public function get_cart_tax() {
1864 $cart_total_tax = wc_round_tax_total( $this->get_cart_contents_tax() + $this->get_shipping_tax() + $this->get_fee_tax() );
1865
1866 return apply_filters( 'woocommerce_get_cart_tax', $cart_total_tax ? wc_price( $cart_total_tax ) : '' );
1867 }
1868
1869 /**
1870 * Get a tax amount.
1871 *
1872 * @param string $tax_rate_id ID of the tax rate to get taxes for.
1873 * @return float amount
1874 */
1875 public function get_tax_amount( $tax_rate_id ) {
1876 $taxes = wc_array_merge_recursive_numeric( $this->get_cart_contents_taxes(), $this->get_fee_taxes() );
1877 return isset( $taxes[ $tax_rate_id ] ) ? $taxes[ $tax_rate_id ] : 0;
1878 }
1879
1880 /**
1881 * Get a tax amount.
1882 *
1883 * @param string $tax_rate_id ID of the tax rate to get taxes for.
1884 * @return float amount
1885 */
1886 public function get_shipping_tax_amount( $tax_rate_id ) {
1887 $taxes = $this->get_shipping_taxes();
1888 return isset( $taxes[ $tax_rate_id ] ) ? $taxes[ $tax_rate_id ] : 0;
1889 }
1890
1891 /**
1892 * Get tax row amounts with or without compound taxes includes.
1893 *
1894 * @param bool $compound True if getting compound taxes.
1895 * @param bool $display True if getting total to display.
1896 * @return float price
1897 */
1898 public function get_taxes_total( $compound = true, $display = true ) {
1899 $total = 0;
1900 $taxes = $this->get_taxes();
1901 foreach ( $taxes as $key => $tax ) {
1902 if ( ! $compound && WC_Tax::is_compound( $key ) ) {
1903 continue;
1904 }
1905 $total += wc_round_tax_total( $tax );
1906 }
1907 if ( $display ) {
1908 $total = wc_format_decimal( $total, wc_get_price_decimals() );
1909 }
1910 return apply_filters( 'woocommerce_cart_taxes_total', $total, $compound, $display, $this );
1911 }
1912
1913 /**
1914 * Gets the total discount amount.
1915 *
1916 * @return mixed formatted price or false if there are none
1917 */
1918 public function get_total_discount() {
1919 return apply_filters( 'woocommerce_cart_total_discount', $this->get_discount_total() ? wc_price( $this->get_discount_total() ) : false, $this );
1920 }
1921
1922 /**
1923 * Reset cart totals to the defaults. Useful before running calculations.
1924 */
1925 private function reset_totals() {
1926 $this->totals = $this->default_totals;
1927 $this->fees_api->remove_all_fees();
1928 do_action( 'woocommerce_cart_reset', $this, false );
1929 }
1930
1931 /**
1932 * Returns 'incl' if tax should be included in cart, otherwise returns 'excl'.
1933 *
1934 * @return string
1935 */
1936 private function is_tax_displayed() {
1937 if ( $this->get_customer() && $this->get_customer()->get_is_vat_exempt() ) {
1938 return 'excl';
1939 }
1940
1941 return get_option( 'woocommerce_tax_display_cart' );
1942 }
1943
1944 /**
1945 * Returns the hash based on cart contents.
1946 *
1947 * @since 3.6.0
1948 * @return string hash for cart content
1949 */
1950 public function get_cart_hash() {
1951 $cart_session = $this->session->get_cart_for_session();
1952 $hash = $cart_session ? md5( wp_json_encode( $cart_session ) . $this->get_total( 'edit' ) ) : '';
1953 $hash = apply_filters_deprecated( 'woocommerce_add_to_cart_hash', array( $hash, $cart_session ), '3.6.0', 'woocommerce_cart_hash' );
1954
1955 return apply_filters( 'woocommerce_cart_hash', $hash, $cart_session );
1956 }
1957 }
1958