PluginProbe ʕ •ᴥ•ʔ
WooCommerce / 9.9.2
WooCommerce v9.9.2
10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 7.8.0-beta.1 7.8.0-beta.2 7.8.0-rc.1 7.8.0-rc.2 7.8.1 7.8.2 7.8.3 7.8.4 7.9.0 7.9.0-beta.1 7.9.0-beta.2 7.9.0-rc.2 7.9.0-rc.3 7.9.1 7.9.2 8.0.0 8.0.0-beta.1 8.0.0-beta.2 8.0.0-rc.1 8.0.0-rc.2 8.0.1 8.0.2 8.0.3 8.0.4 8.0.5 8.1.0 8.1.0-beta.1 8.1.0-rc.1 8.1.0-rc.2 8.1.1 8.1.2 8.1.3 8.1.4 8.2.0 8.2.0-beta.1 8.2.0-rc.1 8.2.0-rc.2 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.3.0 8.3.0-beta.1 8.3.0-rc.1 8.3.0-rc.2 8.3.1 8.3.2 8.3.3 8.3.4 8.4.0 8.4.0-beta.1 8.4.0-rc.1 8.4.1 8.4.2 8.4.3 8.5.0 8.5.0-beta.1 8.5.0-rc.1 8.5.1 8.5.2 8.5.3 8.5.4 8.5.5 8.6.0 8.6.0-beta.1 8.6.0-rc.1 8.6.1 8.6.2 8.6.3 8.6.4 8.7.0 8.7.0-beta.1 8.7.0-beta.2 8.7.0-rc.1 8.7.1 8.7.2 8.7.3 8.8.0 8.8.0-beta.1 8.8.0-rc.1 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.8.6 8.8.7 8.9.0 8.9.0-beta.1 8.9.0-rc.1 8.9.1 8.9.2 8.9.3 8.9.4 8.9.5 9.0.0 9.0.0-beta.1 9.0.0-beta.2 9.0.0-rc.1 9.0.1 9.0.2 9.0.3 9.0.4 9.1.0 9.1.0-beta.1 9.1.0-rc.1 9.1.1 9.1.2 9.1.3 9.1.4 9.1.5 9.1.6 9.2.0 9.2.0-beta.1 9.2.0-rc.1 9.2.1 9.2.2 9.2.3 9.2.4 9.2.5 9.3.0 9.3.0-beta.1 9.3.0-rc.1 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.3.6 9.4.0 9.4.0-beta.1 9.4.0-beta.2 9.4.0-rc.1 9.4.0-rc.2 9.4.0-rc.3 9.4.0-rc.4 9.4.1 9.4.2 9.4.3 9.4.4 9.4.5 9.5.0 9.5.0-beta.1 9.5.0-beta.2 9.5.0-rc.1 9.5.1 9.5.2 9.5.3 9.5.4 9.6.0 9.6.0-beta.1 9.6.0-beta.2 9.6.0-rc.1 9.6.1 9.6.2 9.6.3 9.6.4 9.7.0 9.7.0-beta.1 9.7.0-rc.1 9.7.1 9.7.2 9.7.3 9.8.0 9.8.0-beta.1 9.8.0-rc.1 9.8.1 9.8.2 9.8.3 9.8.4 9.8.5 9.8.6 9.8.7 9.9.0 9.9.0-beta.1 9.9.0-rc.1 9.9.1 9.9.2 9.9.3 9.9.4 9.9.5 9.9.6 9.9.7 3.7.3 7.1.2 3.8.0 7.2.0 3.8.0-beta.1 7.2.0-beta.1 3.8.0-rc.1 7.2.0-beta.2 3.8.0-rc.2 7.2.0-rc.1 3.8.1 7.2.0-rc.2 3.8.2 7.2.1 3.8.3 7.2.2 3.9.0 7.2.3 3.9.0-beta.1 7.2.4 3.9.0-beta.2 7.3.0 3.9.0-rc.1 7.3.0-beta.1 3.9.0-rc.2 7.3.0-beta.2 3.9.0-rc.3 7.3.0-rc.1 3.9.0-rc.4 7.3.0-rc.2 3.9.1 7.3.1 3.9.2 7.4.0 3.9.3 7.4.0-beta.1 3.9.4 7.4.0-beta.2 3.9.5 7.4.0-rc.1 4.0.0 7.4.0-rc.2 4.0.0-beta.1 7.4.1 4.0.0-rc.1 7.4.2 4.0.0-rc.2 7.5.0 4.0.1 7.5.0-beta.1 4.0.2 7.5.0-beta.2 4.0.3 7.5.0-rc.1 4.0.4 7.5.1 4.1.0 7.5.2 4.1.0-beta.1 7.6.0 4.1.0-beta.2 7.6.0-beta.1 4.1.0-rc.1 7.6.0-beta.2 4.1.0-rc.2 7.6.0-rc.1 4.1.1 7.6.0-rc.2 4.1.2 7.6.0-rc.3 4.1.3 7.6.1 4.1.4 7.6.2 4.2.0 7.7.0 4.2.0-RC.1 7.7.0-beta.1 4.2.0-RC.2 7.7.0-beta.2 4.2.0-beta.1 7.7.0-rc.1 4.2.1 7.7.1 4.2.2 7.7.2 4.2.3 7.7.3 4.2.4 7.8.0 4.2.5 4.3.0 4.3.0-beta.1 4.3.0-rc.1 4.3.0-rc.2 4.3.0-rc.3 4.3.1 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.4.0 4.4.0-beta.1 4.4.0-rc.1 4.4.1 4.4.2 4.4.3 4.4.4 4.5.0 4.5.0-beta.1 4.5.0-rc.1 4.5.0-rc.3 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.6.0-beta.1 4.6.0-rc.1 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.7.0 4.7.0-beta.1 4.7.0-beta.2 4.7.0-rc.1 4.7.1 4.7.1-beta.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.0-beta.1 4.8.0-rc.1 4.8.0-rc.2 4.8.1 4.8.2 4.8.3 4.9.0 4.9.0-beta.1 4.9.0-rc.1 4.9.0-rc.2 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 5.0.0 5.0.0-beta.1 5.0.0-beta.2 5.0.0-rc.1 5.0.0-rc.2 5.0.0-rc.3 5.0.1 5.0.2 5.0.3 5.1.0 5.1.0-beta.1 5.1.0-rc.1 trunk 5.1.1 10.0.0 5.1.2 10.0.0-rc.1 5.1.3 10.0.0-rc.2 5.2.0 10.0.1 5.2.0-beta.1 10.0.2 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3.0.1 6.1.0-rc.1 3.0.2 6.1.0-rc.2 3.0.3 6.1.1 3.0.4 6.1.2 3.0.5 6.1.3 3.0.6 6.2.0 3.0.7 6.2.0-beta.1 3.0.8 6.2.0-rc.1 3.0.9 6.2.0-rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.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 1 year ago admin 1 year ago blocks 1 year ago cli 1 year ago customizer 1 year ago data-stores 1 year ago emails 1 year ago export 1 year ago gateways 1 year ago import 1 year ago integrations 2 years ago interfaces 1 year ago legacy 1 year ago libraries 1 year ago log-handlers 1 year ago payment-tokens 5 years ago product-usage 1 year ago queue 4 years ago react-admin 1 year ago rest-api 1 year ago shipping 1 year ago shortcodes 1 year ago theme-support 2 years ago tracks 1 year ago traits 5 years ago walkers 5 years ago wccom-site 1 year ago widgets 1 year ago class-wc-ajax.php 1 year ago class-wc-auth.php 1 year ago class-wc-autoloader.php 1 year ago class-wc-background-emailer.php 5 years ago class-wc-background-updater.php 5 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 1 year ago class-wc-breadcrumb.php 5 years ago class-wc-cache-helper.php 1 year ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 1 year ago class-wc-cart-totals.php 1 year ago class-wc-cart.php 1 year ago class-wc-checkout.php 1 year ago class-wc-cli.php 1 year ago class-wc-comments.php 1 year ago class-wc-countries.php 1 year ago class-wc-coupon.php 1 year ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 1 year ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 3 years ago class-wc-discounts.php 1 year ago class-wc-download-handler.php 1 year ago class-wc-emails.php 1 year ago class-wc-embed.php 1 year ago class-wc-form-handler.php 1 year ago class-wc-frontend-scripts.php 1 year ago class-wc-geo-ip.php 1 year ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 1 year ago class-wc-https.php 2 years ago class-wc-install.php 1 year ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 1 year ago class-wc-meta-data.php 4 years ago class-wc-order-factory.php 2 years ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 1 year ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 1 year ago class-wc-order-item-shipping.php 1 year ago class-wc-order-item-tax.php 4 years ago class-wc-order-item.php 1 year ago class-wc-order-query.php 4 years ago class-wc-order-refund.php 1 year ago class-wc-order.php 1 year ago class-wc-payment-gateways.php 1 year ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 1 year ago class-wc-post-types.php 1 year ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 1 year ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 4 years ago class-wc-product-download.php 2 years ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 1 year ago class-wc-product-grouped.php 1 year ago class-wc-product-query.php 1 year ago class-wc-product-simple.php 1 year ago class-wc-product-variable.php 1 year ago class-wc-product-variation.php 1 year ago class-wc-query.php 1 year ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 1 year ago class-wc-register-wp-admin-settings.php 4 years ago class-wc-rest-authentication.php 1 year ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 1 year ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 1 year ago class-wc-shortcodes.php 1 year ago class-wc-structured-data.php 1 year ago class-wc-tax.php 2 years ago class-wc-template-loader.php 1 year ago class-wc-tracker.php 1 year ago class-wc-validation.php 2 years ago class-wc-webhook.php 1 year ago class-woocommerce.php 1 year ago wc-account-functions.php 1 year ago wc-attribute-functions.php 1 year ago wc-brands-functions.php 1 year ago wc-cart-functions.php 1 year ago wc-conditional-functions.php 1 year ago wc-core-functions.php 1 year ago wc-coupon-functions.php 1 year ago wc-deprecated-functions.php 1 year ago wc-formatting-functions.php 1 year ago wc-notice-functions.php 1 year ago wc-order-functions.php 1 year ago wc-order-item-functions.php 3 years ago wc-order-step-logger-functions.php 1 year ago wc-page-functions.php 1 year ago wc-product-functions.php 1 year ago wc-rest-functions.php 1 year ago wc-stock-functions.php 1 year ago wc-template-functions.php 1 year ago wc-template-hooks.php 1 year ago wc-term-functions.php 1 year ago wc-update-functions.php 1 year ago wc-user-functions.php 1 year ago wc-webhook-functions.php 1 year ago wc-widget-functions.php 5 years ago
class-wc-cart.php
2262 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 use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
13 use Automattic\WooCommerce\Enums\ProductStatus;
14 use Automattic\WooCommerce\Enums\ProductType;
15 use Automattic\WooCommerce\Utilities\DiscountsUtil;
16 use Automattic\WooCommerce\Utilities\NumberUtil;
17 use Automattic\WooCommerce\Utilities\ShippingUtil;
18
19 defined( 'ABSPATH' ) || exit;
20
21 require_once WC_ABSPATH . 'includes/legacy/class-wc-legacy-cart.php';
22 require_once WC_ABSPATH . 'includes/class-wc-cart-fees.php';
23 require_once WC_ABSPATH . 'includes/class-wc-cart-session.php';
24
25 /**
26 * WC_Cart class.
27 */
28 class WC_Cart extends WC_Legacy_Cart {
29
30 /**
31 * Cart context, used to determine if the cart is being used in a StoreAPI or shortcode context. This should only
32 * be used internally.
33 *
34 * @var string shortcode|store-api
35 */
36 public $cart_context = 'shortcode';
37
38 /**
39 * Contains an array of cart items.
40 *
41 * @var array
42 */
43 public $cart_contents = array();
44
45 /**
46 * Contains an array of removed cart items so we can restore them if needed.
47 *
48 * @var array
49 */
50 public $removed_cart_contents = array();
51
52 /**
53 * Contains an array of coupon codes applied to the cart.
54 *
55 * @var array
56 */
57 public $applied_coupons = array();
58
59 /**
60 * This stores the chosen shipping methods for the cart item packages.
61 *
62 * @var array
63 */
64 protected $shipping_methods;
65
66 /**
67 * Whether the shipping totals have been calculated. This will only return true if shipping was calculated, not if
68 * shipping is disabled or if there are no cart contents.
69 *
70 * @var bool
71 */
72 protected $has_calculated_shipping = false;
73
74 /**
75 * Total defaults used to reset.
76 *
77 * @var array
78 */
79 protected $default_totals = array(
80 'subtotal' => 0,
81 'subtotal_tax' => 0,
82 'shipping_total' => 0,
83 'shipping_tax' => 0,
84 'shipping_taxes' => array(),
85 'discount_total' => 0,
86 'discount_tax' => 0,
87 'cart_contents_total' => 0,
88 'cart_contents_tax' => 0,
89 'cart_contents_taxes' => array(),
90 'fee_total' => 0,
91 'fee_tax' => 0,
92 'fee_taxes' => array(),
93 'total' => 0,
94 'total_tax' => 0,
95 );
96 /**
97 * Store calculated totals.
98 *
99 * @var array
100 */
101 protected $totals = array();
102
103 /**
104 * Reference to the cart session handling class.
105 *
106 * @var WC_Cart_Session
107 */
108 protected $session;
109
110 /**
111 * Reference to the cart fees API class.
112 *
113 * @var WC_Cart_Fees
114 */
115 protected $fees_api;
116
117 /**
118 * Constructor for the cart class. Loads options and hooks in the init method.
119 */
120 public function __construct() {
121 $this->session = new WC_Cart_Session( $this );
122 $this->fees_api = new WC_Cart_Fees();
123
124 // Register hooks for the objects.
125 $this->session->init();
126
127 add_action( 'woocommerce_add_to_cart', array( $this, 'calculate_totals' ), 20, 0 );
128 add_action( 'woocommerce_applied_coupon', array( $this, 'calculate_totals' ), 20, 0 );
129 add_action( 'woocommerce_removed_coupon', array( $this, 'calculate_totals' ), 20, 0 );
130 add_action( 'woocommerce_cart_item_removed', array( $this, 'calculate_totals' ), 20, 0 );
131 add_action( 'woocommerce_cart_item_restored', array( $this, 'calculate_totals' ), 20, 0 );
132 add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_items' ), 1 );
133 add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_coupons' ), 1 );
134 add_action( 'woocommerce_after_checkout_validation', array( $this, 'check_customer_coupons' ), 1, 2 );
135 }
136
137 /**
138 * When cloning, ensure object properties are handled.
139 *
140 * These properties store a reference to the cart, so we use new instead of clone.
141 */
142 public function __clone() {
143 $this->session = clone $this->session;
144 $this->fees_api = clone $this->fees_api;
145
146 $this->session->set_cart( $this );
147 }
148
149 /*
150 |--------------------------------------------------------------------------
151 | Getters.
152 |--------------------------------------------------------------------------
153 |
154 | Methods to retrieve class properties and avoid direct access.
155 */
156
157 /**
158 * Gets cart contents.
159 *
160 * @since 3.2.0
161 * @return array of cart items
162 */
163 public function get_cart_contents() {
164 return apply_filters( 'woocommerce_get_cart_contents', (array) $this->cart_contents );
165 }
166
167 /**
168 * Return items removed from the cart.
169 *
170 * @since 3.2.0
171 * @return array
172 */
173 public function get_removed_cart_contents() {
174 return (array) $this->removed_cart_contents;
175 }
176
177 /**
178 * Gets the array of applied coupon codes.
179 *
180 * @return array of applied coupons
181 */
182 public function get_applied_coupons() {
183 return (array) $this->applied_coupons;
184 }
185
186 /**
187 * Return all calculated coupon totals.
188 *
189 * @since 3.2.0
190 * @return array
191 */
192 public function get_coupon_discount_totals() {
193 return (array) $this->coupon_discount_totals;
194 }
195 /**
196 * Return all calculated coupon tax totals.
197 *
198 * @since 3.2.0
199 * @return array
200 */
201 public function get_coupon_discount_tax_totals() {
202 return (array) $this->coupon_discount_tax_totals;
203 }
204
205 /**
206 * Return all calculated totals.
207 *
208 * @since 3.2.0
209 * @return array
210 */
211 public function get_totals() {
212 return empty( $this->totals ) ? $this->default_totals : $this->totals;
213 }
214
215 /**
216 * Get a total.
217 *
218 * @since 3.2.0
219 * @param string $key Key of element in $totals array.
220 * @return mixed
221 */
222 protected function get_totals_var( $key ) {
223 return isset( $this->totals[ $key ] ) ? $this->totals[ $key ] : $this->default_totals[ $key ];
224 }
225
226 /**
227 * Get subtotal.
228 *
229 * @since 3.2.0
230 * @return float
231 */
232 public function get_subtotal() {
233 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'subtotal' ) );
234 }
235
236 /**
237 * Get subtotal_tax.
238 *
239 * @since 3.2.0
240 * @return float
241 */
242 public function get_subtotal_tax() {
243 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'subtotal_tax' ) );
244 }
245
246 /**
247 * Get discount_total.
248 *
249 * @since 3.2.0
250 * @return float
251 */
252 public function get_discount_total() {
253 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'discount_total' ) );
254 }
255
256 /**
257 * Get discount_tax.
258 *
259 * @since 3.2.0
260 * @return float
261 */
262 public function get_discount_tax() {
263 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'discount_tax' ) );
264 }
265
266 /**
267 * Get shipping_total.
268 *
269 * @since 3.2.0
270 * @return float
271 */
272 public function get_shipping_total() {
273 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'shipping_total' ) );
274 }
275
276 /**
277 * Get shipping_tax.
278 *
279 * @since 3.2.0
280 * @return float
281 */
282 public function get_shipping_tax() {
283 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'shipping_tax' ) );
284 }
285
286 /**
287 * Gets cart total. This is the total of items in the cart, but after discounts. Subtotal is before discounts.
288 *
289 * @since 3.2.0
290 * @return float
291 */
292 public function get_cart_contents_total() {
293 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'cart_contents_total' ) );
294 }
295
296 /**
297 * Gets cart tax amount.
298 *
299 * @since 3.2.0
300 * @return float
301 */
302 public function get_cart_contents_tax() {
303 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'cart_contents_tax' ) );
304 }
305
306 /**
307 * Gets cart total after calculation.
308 *
309 * @since 3.2.0
310 * @param string $context If the context is view, the value will be formatted for display. This keeps it compatible with pre-3.2 versions.
311 * @return float|string
312 */
313 public function get_total( $context = 'view' ) {
314 $total = apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'total' ) );
315 return 'view' === $context ? apply_filters( 'woocommerce_cart_total', wc_price( $total ) ) : $total;
316 }
317
318 /**
319 * Get total tax amount.
320 *
321 * @since 3.2.0
322 * @return float
323 */
324 public function get_total_tax() {
325 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'total_tax' ) );
326 }
327
328 /**
329 * Get total fee amount.
330 *
331 * @since 3.2.0
332 * @return float
333 */
334 public function get_fee_total() {
335 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'fee_total' ) );
336 }
337
338 /**
339 * Get total fee tax amount.
340 *
341 * @since 3.2.0
342 * @return float
343 */
344 public function get_fee_tax() {
345 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'fee_tax' ) );
346 }
347
348 /**
349 * Get taxes.
350 *
351 * @since 3.2.0
352 */
353 public function get_shipping_taxes() {
354 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'shipping_taxes' ) );
355 }
356
357 /**
358 * Get taxes.
359 *
360 * @since 3.2.0
361 */
362 public function get_cart_contents_taxes() {
363 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'cart_contents_taxes' ) );
364 }
365
366 /**
367 * Get taxes.
368 *
369 * @since 3.2.0
370 */
371 public function get_fee_taxes() {
372 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, $this->get_totals_var( 'fee_taxes' ) );
373 }
374
375 /**
376 * Return whether or not the cart is displaying prices including tax, rather than excluding tax.
377 *
378 * @since 3.3.0
379 * @return bool
380 */
381 public function display_prices_including_tax() {
382 return apply_filters( 'woocommerce_cart_' . __FUNCTION__, 'incl' === $this->get_tax_price_display_mode() );
383 }
384
385 /*
386 |--------------------------------------------------------------------------
387 | Setters.
388 |--------------------------------------------------------------------------
389 |
390 | Methods to set class properties and avoid direct access.
391 */
392
393 /**
394 * Sets the contents of the cart.
395 *
396 * @param array $value Cart array.
397 */
398 public function set_cart_contents( $value ) {
399 $this->cart_contents = (array) $value;
400 }
401
402 /**
403 * Set items removed from the cart.
404 *
405 * @since 3.2.0
406 * @param array $value Item array.
407 */
408 public function set_removed_cart_contents( $value = array() ) {
409 $this->removed_cart_contents = (array) $value;
410 }
411
412 /**
413 * Sets the array of applied coupon codes.
414 *
415 * @param array $value List of applied coupon codes.
416 */
417 public function set_applied_coupons( $value = array() ) {
418 $this->applied_coupons = (array) $value;
419 }
420
421 /**
422 * Sets the array of calculated coupon totals.
423 *
424 * @since 3.2.0
425 * @param array $value Value to set.
426 */
427 public function set_coupon_discount_totals( $value = array() ) {
428 $this->coupon_discount_totals = (array) $value;
429 }
430 /**
431 * Sets the array of calculated coupon tax totals.
432 *
433 * @since 3.2.0
434 * @param array $value Value to set.
435 */
436 public function set_coupon_discount_tax_totals( $value = array() ) {
437 $this->coupon_discount_tax_totals = (array) $value;
438 }
439
440 /**
441 * Set all calculated totals.
442 *
443 * @since 3.2.0
444 * @param array $value Value to set.
445 */
446 public function set_totals( $value = array() ) {
447 $this->totals = wp_parse_args( $value, $this->default_totals );
448 }
449
450 /**
451 * Set subtotal.
452 *
453 * @since 3.2.0
454 * @param string $value Value to set.
455 */
456 public function set_subtotal( $value ) {
457 $this->totals['subtotal'] = wc_format_decimal( $value );
458 }
459
460 /**
461 * Set subtotal.
462 *
463 * @since 3.2.0
464 * @param string $value Value to set.
465 */
466 public function set_subtotal_tax( $value ) {
467 $this->totals['subtotal_tax'] = $value;
468 }
469
470 /**
471 * Set discount_total.
472 *
473 * @since 3.2.0
474 * @param string $value Value to set.
475 */
476 public function set_discount_total( $value ) {
477 $this->totals['discount_total'] = $value;
478 }
479
480 /**
481 * Set discount_tax.
482 *
483 * @since 3.2.0
484 * @param string $value Value to set.
485 */
486 public function set_discount_tax( $value ) {
487 $this->totals['discount_tax'] = $value;
488 }
489
490 /**
491 * Set shipping_total.
492 *
493 * @since 3.2.0
494 * @param string $value Value to set.
495 */
496 public function set_shipping_total( $value ) {
497 $this->totals['shipping_total'] = wc_format_decimal( $value );
498 }
499
500 /**
501 * Set shipping_tax.
502 *
503 * @since 3.2.0
504 * @param string $value Value to set.
505 */
506 public function set_shipping_tax( $value ) {
507 $this->totals['shipping_tax'] = $value;
508 }
509
510 /**
511 * Set cart_contents_total.
512 *
513 * @since 3.2.0
514 * @param string $value Value to set.
515 */
516 public function set_cart_contents_total( $value ) {
517 $this->totals['cart_contents_total'] = wc_format_decimal( $value );
518 }
519
520 /**
521 * Set cart tax amount.
522 *
523 * @since 3.2.0
524 * @param string $value Value to set.
525 */
526 public function set_cart_contents_tax( $value ) {
527 $this->totals['cart_contents_tax'] = $value;
528 }
529
530 /**
531 * Set cart total.
532 *
533 * @since 3.2.0
534 * @param string $value Value to set.
535 */
536 public function set_total( $value ) {
537 $this->totals['total'] = wc_format_decimal( $value, wc_get_price_decimals() );
538 }
539
540 /**
541 * Set total tax amount.
542 *
543 * @since 3.2.0
544 * @param string $value Value to set.
545 */
546 public function set_total_tax( $value ) {
547 // We round here because this is a total entry, as opposed to line items in other setters.
548 $this->totals['total_tax'] = wc_round_tax_total( $value );
549 }
550
551 /**
552 * Set fee amount.
553 *
554 * @since 3.2.0
555 * @param string $value Value to set.
556 */
557 public function set_fee_total( $value ) {
558 $this->totals['fee_total'] = wc_format_decimal( $value );
559 }
560
561 /**
562 * Set fee tax.
563 *
564 * @since 3.2.0
565 * @param string $value Value to set.
566 */
567 public function set_fee_tax( $value ) {
568 $this->totals['fee_tax'] = $value;
569 }
570
571 /**
572 * Set taxes.
573 *
574 * @since 3.2.0
575 * @param array $value Tax values.
576 */
577 public function set_shipping_taxes( $value ) {
578 $this->totals['shipping_taxes'] = (array) $value;
579 }
580
581 /**
582 * Set taxes.
583 *
584 * @since 3.2.0
585 * @param array $value Tax values.
586 */
587 public function set_cart_contents_taxes( $value ) {
588 $this->totals['cart_contents_taxes'] = (array) $value;
589 }
590
591 /**
592 * Set taxes.
593 *
594 * @since 3.2.0
595 * @param array $value Tax values.
596 */
597 public function set_fee_taxes( $value ) {
598 $this->totals['fee_taxes'] = (array) $value;
599 }
600
601 /*
602 |--------------------------------------------------------------------------
603 | Helper methods.
604 |--------------------------------------------------------------------------
605 */
606
607 /**
608 * Returns the cart and shipping taxes, merged.
609 *
610 * @return array merged taxes
611 */
612 public function get_taxes() {
613 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 );
614 }
615
616 /**
617 * Returns the contents of the cart in an array.
618 *
619 * @return array contents of the cart
620 */
621 public function get_cart() {
622 if ( ! did_action( 'wp_loaded' ) ) {
623 wc_doing_it_wrong( __FUNCTION__, __( 'Get cart should not be called before the wp_loaded action.', 'woocommerce' ), '2.3' );
624 }
625 if ( ! did_action( 'woocommerce_load_cart_from_session' ) ) {
626 $this->session->get_cart_from_session();
627 }
628 return array_filter( $this->get_cart_contents() );
629 }
630
631 /**
632 * Returns a specific item in the cart.
633 *
634 * @param string $item_key Cart item key.
635 * @return array Item data
636 */
637 public function get_cart_item( $item_key ) {
638 return isset( $this->cart_contents[ $item_key ] ) ? $this->cart_contents[ $item_key ] : array();
639 }
640
641 /**
642 * Checks if the cart is empty.
643 *
644 * @return bool
645 */
646 public function is_empty() {
647 return 0 === count( $this->get_cart() );
648 }
649
650 /**
651 * Empties the cart and optionally the persistent cart too.
652 *
653 * @since 9.7.0 Also clears shipping methods and packages since the items they are linked to are cleared.
654 *
655 * @param bool $clear_persistent_cart Should the persistent cart be cleared too. Defaults to true.
656 */
657 public function empty_cart( $clear_persistent_cart = true ) {
658
659 do_action( 'woocommerce_before_cart_emptied', $clear_persistent_cart );
660
661 $this->cart_contents = array();
662 $this->removed_cart_contents = array();
663 $this->shipping_methods = array();
664 $this->coupon_discount_totals = array();
665 $this->coupon_discount_tax_totals = array();
666 $this->applied_coupons = array();
667 $this->totals = $this->default_totals;
668
669 if ( $clear_persistent_cart ) {
670 $this->session->persistent_cart_destroy();
671 }
672
673 $this->fees_api->remove_all_fees();
674 WC()->shipping()->reset_shipping();
675
676 do_action( 'woocommerce_cart_emptied', $clear_persistent_cart );
677 }
678
679 /**
680 * Get number of items in the cart.
681 *
682 * @return int
683 */
684 public function get_cart_contents_count() {
685 return apply_filters( 'woocommerce_cart_contents_count', array_sum( wp_list_pluck( $this->get_cart(), 'quantity' ) ) );
686 }
687
688 /**
689 * Get weight of items in the cart.
690 *
691 * @since 2.5.0
692 * @return float
693 */
694 public function get_cart_contents_weight() {
695 $weight = 0.0;
696
697 foreach ( $this->get_cart() as $values ) {
698 if ( $values['data']->has_weight() ) {
699 $weight += (float) $values['data']->get_weight() * $values['quantity'];
700 }
701 }
702
703 return apply_filters( 'woocommerce_cart_contents_weight', $weight );
704 }
705
706 /**
707 * Get cart items quantities - merged so we can do accurate stock checks on items across multiple lines.
708 *
709 * @return array
710 */
711 public function get_cart_item_quantities() {
712 $quantities = array();
713
714 foreach ( $this->get_cart() as $values ) {
715 $product = $values['data'];
716 $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'];
717 }
718
719 return $quantities;
720 }
721
722 /**
723 * Check all cart items for errors.
724 */
725 public function check_cart_items() {
726 $return = true;
727 $result = $this->check_cart_item_validity();
728
729 if ( is_wp_error( $result ) ) {
730 wc_add_notice( $result->get_error_message(), 'error' );
731 $return = false;
732 }
733
734 $result = $this->check_cart_item_stock();
735
736 if ( is_wp_error( $result ) ) {
737 wc_add_notice( $result->get_error_message(), 'error' );
738 $return = false;
739 }
740
741 return $return;
742 }
743
744 /**
745 * Check cart coupons for errors.
746 */
747 public function check_cart_coupons() {
748 foreach ( $this->get_applied_coupons() as $code ) {
749 $coupon = new WC_Coupon( $code );
750
751 if ( ! $coupon->is_valid() ) {
752 $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_INVALID_REMOVED );
753 $this->remove_coupon( $code );
754 }
755 }
756 }
757
758 /**
759 * Looks through cart items and checks the posts are not trashed or deleted.
760 *
761 * @return bool|WP_Error
762 */
763 public function check_cart_item_validity() {
764 $return = true;
765
766 foreach ( $this->get_cart() as $cart_item_key => $values ) {
767 $product = $values['data'];
768
769 if ( ! $product || ! $product->exists() || ProductStatus::TRASH === $product->get_status() ) {
770 $this->set_quantity( $cart_item_key, 0 );
771 $return = new WP_Error( 'invalid', __( 'An item which is no longer available was removed from your cart.', 'woocommerce' ) );
772 }
773 }
774
775 return $return;
776 }
777
778 /**
779 * Looks through the cart to check each item is in stock. If not, add an error.
780 *
781 * @return bool|WP_Error
782 */
783 public function check_cart_item_stock() {
784 $error = new WP_Error();
785 $product_qty_in_cart = $this->get_cart_item_quantities();
786 $current_session_order_id = isset( WC()->session->order_awaiting_payment ) ? absint( WC()->session->order_awaiting_payment ) : absint( WC()->session->get( 'store_api_draft_order', 0 ) );
787
788 foreach ( $this->get_cart() as $values ) {
789 $product = $values['data'];
790
791 // Check stock based on stock-status.
792 if ( ! $product->is_in_stock() ) {
793 /* translators: %s: product name */
794 $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() ) );
795 return $error;
796 }
797
798 // We only need to check products managing stock, with a limited stock qty.
799 if ( ! $product->managing_stock() || $product->backorders_allowed() ) {
800 continue;
801 }
802
803 // Check stock based on all items in the cart and consider any held stock within pending orders.
804 $held_stock = wc_get_held_stock_quantity( $product, $current_session_order_id );
805 $required_stock = $product_qty_in_cart[ $product->get_stock_managed_by_id() ];
806
807 /**
808 * Allows filter if product have enough stock to get added to the cart.
809 *
810 * @since 4.6.0
811 * @param bool $has_stock If have enough stock.
812 * @param WC_Product $product Product instance.
813 * @param array $values Cart item values.
814 */
815 if ( apply_filters( 'woocommerce_cart_item_required_stock_is_not_enough', $product->get_stock_quantity() < ( $held_stock + $required_stock ), $product, $values ) ) {
816 /* translators: 1: product name 2: quantity in stock */
817 $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 ) ) );
818 return $error;
819 }
820 }
821
822 return true;
823 }
824
825 /**
826 * Gets and formats a list of cart item data + variations for display on the frontend.
827 *
828 * @param array $cart_item Cart item object.
829 * @param bool $flat Should the data be returned flat or in a list.
830 * @return string
831 */
832 public function get_item_data( $cart_item, $flat = false ) {
833 wc_deprecated_function( 'WC_Cart::get_item_data', '3.3', 'wc_get_formatted_cart_item_data' );
834
835 return wc_get_formatted_cart_item_data( $cart_item, $flat );
836 }
837
838 /**
839 * Gets cross sells based on the items in the cart.
840 *
841 * @return array cross_sells (item ids)
842 */
843 public function get_cross_sells() {
844 $cross_sells = array();
845 $in_cart = array();
846 if ( ! $this->is_empty() ) {
847 foreach ( $this->get_cart() as $values ) {
848 if ( $values['quantity'] > 0 ) {
849 $cross_sells = array_merge( $values['data']->get_cross_sell_ids(), $cross_sells );
850 $in_cart[] = $values['product_id'];
851
852 // Add variations to the in cart array.
853 if ( $values['data']->is_type( ProductType::VARIATION ) ) {
854 $in_cart[] = $values['variation_id'];
855 }
856 }
857 }
858 }
859 $cross_sells = array_diff( $cross_sells, $in_cart );
860 return apply_filters( 'woocommerce_cart_crosssell_ids', wp_parse_id_list( $cross_sells ), $this );
861 }
862
863 /**
864 * Gets the url to remove an item from the cart.
865 *
866 * @param string $cart_item_key contains the id of the cart item.
867 * @return string url to page
868 */
869 public function get_remove_url( $cart_item_key ) {
870 wc_deprecated_function( 'WC_Cart::get_remove_url', '3.3', 'wc_get_cart_remove_url' );
871
872 return wc_get_cart_remove_url( $cart_item_key );
873 }
874
875 /**
876 * Gets the url to re-add an item into the cart.
877 *
878 * @param string $cart_item_key Cart item key to undo.
879 * @return string url to page
880 */
881 public function get_undo_url( $cart_item_key ) {
882 wc_deprecated_function( 'WC_Cart::get_undo_url', '3.3', 'wc_get_cart_undo_url' );
883
884 return wc_get_cart_undo_url( $cart_item_key );
885 }
886
887 /**
888 * Get taxes, merged by code, formatted ready for output.
889 *
890 * @return array
891 */
892 public function get_tax_totals() {
893 $shipping_taxes = $this->get_shipping_taxes(); // Shipping taxes are rounded differently, so we will subtract from all taxes, then round and then add them back.
894 $taxes = $this->get_taxes();
895 $tax_totals = array();
896
897 foreach ( $taxes as $key => $tax ) {
898 $code = WC_Tax::get_rate_code( $key );
899
900 if ( $code || apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) === $key ) {
901 if ( ! isset( $tax_totals[ $code ] ) ) {
902 $tax_totals[ $code ] = new stdClass();
903 $tax_totals[ $code ]->amount = 0;
904 }
905
906 $tax_totals[ $code ]->tax_rate_id = $key;
907 $tax_totals[ $code ]->is_compound = WC_Tax::is_compound( $key );
908 $tax_totals[ $code ]->label = WC_Tax::get_rate_label( $key );
909
910 if ( isset( $shipping_taxes[ $key ] ) ) {
911 $tax -= $shipping_taxes[ $key ];
912 $tax = wc_round_tax_total( $tax );
913 $tax += NumberUtil::round( $shipping_taxes[ $key ], wc_get_price_decimals() );
914 unset( $shipping_taxes[ $key ] );
915 }
916 $tax_totals[ $code ]->amount += wc_round_tax_total( $tax );
917 $tax_totals[ $code ]->formatted_amount = wc_price( $tax_totals[ $code ]->amount );
918 }
919 }
920
921 if ( apply_filters( 'woocommerce_cart_hide_zero_taxes', true ) ) {
922 $amounts = array_filter( wp_list_pluck( $tax_totals, 'amount' ) );
923 $tax_totals = array_intersect_key( $tax_totals, $amounts );
924 }
925
926 return apply_filters( 'woocommerce_cart_tax_totals', $tax_totals, $this );
927 }
928
929 /**
930 * Get all tax classes for items in the cart.
931 *
932 * @return array
933 */
934 public function get_cart_item_tax_classes() {
935 $found_tax_classes = array();
936
937 foreach ( $this->get_cart() as $item ) {
938 if ( $item['data'] && ( $item['data']->is_taxable() || $item['data']->is_shipping_taxable() ) ) {
939 $found_tax_classes[] = $item['data']->get_tax_class();
940 }
941 }
942
943 return array_unique( $found_tax_classes );
944 }
945
946 /**
947 * Get all tax classes for shipping based on the items in the cart.
948 *
949 * @return array
950 */
951 public function get_cart_item_tax_classes_for_shipping() {
952 $found_tax_classes = array();
953
954 foreach ( $this->get_cart() as $item ) {
955 if ( $item['data'] && ( $item['data']->is_shipping_taxable() ) ) {
956 $found_tax_classes[] = $item['data']->get_tax_class();
957 }
958 }
959
960 return array_unique( $found_tax_classes );
961 }
962
963 /**
964 * Determines the value that the customer spent and the subtotal
965 * displayed, used for things like coupon validation.
966 *
967 * Since the coupon lines are displayed based on the TAX DISPLAY value
968 * of cart, this is used to determine the spend.
969 *
970 * If cart totals are shown including tax, use the subtotal.
971 * If cart totals are shown excluding tax, use the subtotal ex tax
972 * (tax is shown after coupons).
973 *
974 * @since 2.6.0
975 * @return string
976 */
977 public function get_displayed_subtotal() {
978 return $this->display_prices_including_tax() ? $this->get_subtotal() + $this->get_subtotal_tax() : $this->get_subtotal();
979 }
980
981 /**
982 * Check if product is in the cart and return cart item key.
983 *
984 * Cart item key will be unique based on the item and its properties, such as variations.
985 *
986 * @param mixed $cart_id id of product to find in the cart.
987 * @return string cart item key
988 */
989 public function find_product_in_cart( $cart_id = false ) {
990 if ( false !== $cart_id ) {
991 if ( is_array( $this->cart_contents ) && isset( $this->cart_contents[ $cart_id ] ) ) {
992 return $cart_id;
993 }
994 }
995 return '';
996 }
997
998 /**
999 * Generate a unique ID for the cart item being added.
1000 *
1001 * @param int $product_id - id of the product the key is being generated for.
1002 * @param int $variation_id of the product the key is being generated for.
1003 * @param array $variation data for the cart item.
1004 * @param array $cart_item_data other cart item data passed which affects this items uniqueness in the cart.
1005 * @return string cart item key
1006 */
1007 public function generate_cart_id( $product_id, $variation_id = 0, $variation = array(), $cart_item_data = array() ) {
1008 $id_parts = array( $product_id );
1009
1010 if ( $variation_id && 0 !== $variation_id ) {
1011 $id_parts[] = $variation_id;
1012 }
1013
1014 if ( is_array( $variation ) && ! empty( $variation ) ) {
1015 $variation_key = '';
1016 foreach ( $variation as $key => $value ) {
1017 $variation_key .= trim( $key ) . trim( $value );
1018 }
1019 $id_parts[] = $variation_key;
1020 }
1021
1022 if ( is_array( $cart_item_data ) && ! empty( $cart_item_data ) ) {
1023 $cart_item_data_key = '';
1024 foreach ( $cart_item_data as $key => $value ) {
1025 if ( is_array( $value ) || is_object( $value ) ) {
1026 $value = http_build_query( $value );
1027 }
1028 $cart_item_data_key .= trim( $key ) . trim( $value );
1029
1030 }
1031 $id_parts[] = $cart_item_data_key;
1032 }
1033
1034 return apply_filters( 'woocommerce_cart_id', md5( implode( '_', $id_parts ) ), $product_id, $variation_id, $variation, $cart_item_data );
1035 }
1036
1037 /**
1038 * Add a product to the cart.
1039 *
1040 * @throws Exception Plugins can throw an exception to prevent adding to cart.
1041 * @param int $product_id contains the id of the product to add to the cart.
1042 * @param int $quantity contains the quantity of the item to add.
1043 * @param int $variation_id ID of the variation being added to the cart.
1044 * @param array $variation attribute values.
1045 * @param array $cart_item_data extra cart item data we want to pass into the item.
1046 * @return string|bool $cart_item_key
1047 */
1048 public function add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() ) {
1049 try {
1050 $product_id = absint( $product_id );
1051 $variation_id = absint( $variation_id );
1052
1053 // Ensure we don't add a variation to the cart directly by variation ID.
1054 if ( 'product_variation' === get_post_type( $product_id ) ) {
1055 $variation_id = $product_id;
1056 $product_id = wp_get_post_parent_id( $variation_id );
1057 }
1058
1059 $product_data = wc_get_product( $variation_id ? $variation_id : $product_id );
1060 $quantity = apply_filters( 'woocommerce_add_to_cart_quantity', $quantity, $product_id );
1061
1062 if ( $quantity <= 0 || ! $product_data || ProductStatus::TRASH === $product_data->get_status() ) {
1063 return false;
1064 }
1065
1066 // Variable product cannot be added to cart without a specified variation.
1067 if ( ! $variation_id && $product_data->is_type( ProductType::VARIABLE ) ) {
1068 /* translators: 1: product link, 2: product name */
1069 throw new Exception( sprintf( __( 'Please choose product options by visiting <a href="%1$s" title="%2$s">%2$s</a>.', 'woocommerce' ), esc_url( $product_data->get_permalink() ), esc_html( $product_data->get_name() ) ) );
1070 }
1071
1072 if ( $product_data->is_type( ProductType::VARIATION ) ) {
1073 $missing_attributes = array();
1074 $parent_data = wc_get_product( $product_data->get_parent_id() );
1075
1076 $variation_attributes = $product_data->get_variation_attributes();
1077 // Filter out 'any' variations, which are empty, as they need to be explicitly specified while adding to cart.
1078 $variation_attributes = array_filter( $variation_attributes );
1079
1080 // Gather posted attributes.
1081 $posted_attributes = array();
1082 foreach ( $parent_data->get_attributes() as $attribute ) {
1083 if ( ! $attribute['is_variation'] ) {
1084 continue;
1085 }
1086 $attribute_key = 'attribute_' . sanitize_title( $attribute['name'] );
1087
1088 if ( isset( $variation[ $attribute_key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1089 if ( $attribute['is_taxonomy'] ) {
1090 // Don't use wc_clean as it destroys sanitized characters.
1091 $value = sanitize_title( wp_unslash( $variation[ $attribute_key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1092 } else {
1093 $value = html_entity_decode( wc_clean( wp_unslash( $variation[ $attribute_key ] ) ), ENT_QUOTES, get_bloginfo( 'charset' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1094 }
1095
1096 // Don't include if it's empty.
1097 if ( ! empty( $value ) || '0' === $value ) {
1098 $posted_attributes[ $attribute_key ] = $value;
1099 }
1100 }
1101 }
1102
1103 // Merge variation attributes and posted attributes.
1104 $posted_and_variation_attributes = array_merge( $variation_attributes, $posted_attributes );
1105
1106 // If no variation ID is set, attempt to get a variation ID from posted attributes.
1107 if ( empty( $variation_id ) ) {
1108 $data_store = WC_Data_Store::load( 'product' );
1109 $variation_id = $data_store->find_matching_product_variation( $parent_data, $posted_attributes );
1110 }
1111
1112 // Do we have a variation ID?
1113 if ( empty( $variation_id ) ) {
1114 throw new Exception( __( 'Please choose product options&hellip;', 'woocommerce' ) );
1115 }
1116
1117 // Check the data we have is valid.
1118 $variation_data = wc_get_product_variation_attributes( $variation_id );
1119 $attributes = array();
1120
1121 foreach ( $parent_data->get_attributes() as $attribute ) {
1122 if ( ! $attribute['is_variation'] ) {
1123 continue;
1124 }
1125
1126 // Get valid value from variation data.
1127 $attribute_key = 'attribute_' . sanitize_title( $attribute['name'] );
1128 $valid_value = isset( $variation_data[ $attribute_key ] ) ? $variation_data[ $attribute_key ] : '';
1129
1130 /**
1131 * If the attribute value was posted, check if it's valid.
1132 *
1133 * If no attribute was posted, only error if the variation has an 'any' attribute which requires a value.
1134 */
1135 if ( isset( $posted_and_variation_attributes[ $attribute_key ] ) ) {
1136 $value = $posted_and_variation_attributes[ $attribute_key ];
1137
1138 // Allow if valid or show error.
1139 if ( $valid_value === $value ) {
1140 $attributes[ $attribute_key ] = $value;
1141 } elseif ( '' === $valid_value && in_array( $value, $attribute->get_slugs(), true ) ) {
1142 // If valid values are empty, this is an 'any' variation so get all possible values.
1143 $attributes[ $attribute_key ] = $value;
1144 } else {
1145 /* translators: %s: Attribute name. */
1146 throw new Exception( sprintf( __( 'Invalid value posted for %s', 'woocommerce' ), wc_attribute_label( $attribute['name'] ) ) );
1147 }
1148 } elseif ( '' === $valid_value ) {
1149 $missing_attributes[] = wc_attribute_label( $attribute['name'] );
1150 }
1151
1152 $variation = $attributes;
1153 }
1154 if ( ! empty( $missing_attributes ) ) {
1155 /* translators: %s: Attribute name. */
1156 throw new Exception( sprintf( _n( '%s is a required field', '%s are required fields', count( $missing_attributes ), 'woocommerce' ), wc_format_list_of_items( $missing_attributes ) ) );
1157 }
1158 }
1159
1160 // Validate variation ID.
1161 if (
1162 0 < $variation_id && // Only check if there's any variation_id.
1163 (
1164 ! $product_data->is_type( ProductType::VARIATION ) || // Check if isn't a variation, it suppose to be a variation at this point.
1165 $product_data->get_parent_id() !== $product_id // Check if belongs to the selected variable product.
1166 )
1167 ) {
1168 $product = wc_get_product( $product_id );
1169 if ( ! ( $product instanceof WC_Product ) ) {
1170 throw new Exception( __( 'The selected product is invalid.', 'woocommerce' ) );
1171 }
1172
1173 /* translators: 1: product link, 2: product name */
1174 throw new Exception( sprintf( __( 'The selected product isn\'t a variation of %2$s, please choose product options by visiting <a href="%1$s" title="%2$s">%2$s</a>.', 'woocommerce' ), esc_url( $product->get_permalink() ), esc_html( $product->get_name() ) ) );
1175 }
1176
1177 // Load cart item data - may be added by other plugins.
1178 $cart_item_data = (array) apply_filters( 'woocommerce_add_cart_item_data', $cart_item_data, $product_id, $variation_id, $quantity );
1179
1180 // Generate a ID based on product ID, variation ID, variation data, and other cart item data.
1181 $cart_id = $this->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );
1182
1183 // Find the cart item key in the existing cart.
1184 $cart_item_key = $this->find_product_in_cart( $cart_id );
1185
1186 // Force quantity to 1 if sold individually and check for existing item in cart.
1187 if ( $product_data->is_sold_individually() ) {
1188 $quantity = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data );
1189 $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 );
1190
1191 if ( $found_in_cart ) {
1192 /* translators: %s: product name */
1193 $message = sprintf( __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product_data->get_name() );
1194
1195 /**
1196 * Filters message about more than 1 product being added to cart.
1197 *
1198 * @since 4.5.0
1199 * @param string $message Message.
1200 * @param WC_Product $product_data Product data.
1201 */
1202 $message = apply_filters( 'woocommerce_cart_product_cannot_add_another_message', $message, $product_data );
1203 $wp_button_class = wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '';
1204
1205 if ( ! CartCheckoutUtils::has_cart_page() ) {
1206 $message = sprintf( '%s', esc_html( $message ) );
1207 } else {
1208 $message = sprintf( '%s <a href="%s" class="button wc-forward%s">%s</a>', $message, esc_url( wc_get_cart_url() ), esc_attr( $wp_button_class ), __( 'View cart', 'woocommerce' ) );
1209 }
1210
1211 throw new Exception( $message );
1212 }
1213 }
1214
1215 if ( ! $product_data->is_purchasable() ) {
1216 $message = __( 'Sorry, this product cannot be purchased.', 'woocommerce' );
1217 /**
1218 * Filters message about product unable to be purchased.
1219 *
1220 * @since 3.8.0
1221 * @param string $message Message.
1222 * @param WC_Product $product_data Product data.
1223 */
1224 $message = apply_filters( 'woocommerce_cart_product_cannot_be_purchased_message', $message, $product_data );
1225 throw new Exception( $message );
1226 }
1227
1228 // Stock check - only check if we're managing stock and backorders are not allowed.
1229 if ( ! $product_data->is_in_stock() ) {
1230 /* translators: %s: product name */
1231 $message = sprintf( __( 'You cannot add &quot;%s&quot; to the cart because the product is out of stock.', 'woocommerce' ), $product_data->get_name() );
1232
1233 /**
1234 * Filters message about product being out of stock.
1235 *
1236 * @since 4.5.0
1237 * @param string $message Message.
1238 * @param WC_Product $product_data Product data.
1239 */
1240 $message = apply_filters( 'woocommerce_cart_product_out_of_stock_message', $message, $product_data );
1241 throw new Exception( $message );
1242 }
1243
1244 if ( ! $product_data->has_enough_stock( $quantity ) ) {
1245 $stock_quantity = $product_data->get_stock_quantity();
1246
1247 /* translators: 1: product name 2: quantity in stock */
1248 $message = 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( $stock_quantity, $product_data ) );
1249
1250 /**
1251 * Filters message about product not having enough stock.
1252 *
1253 * @since 4.5.0
1254 * @param string $message Message.
1255 * @param WC_Product $product_data Product data.
1256 * @param int $stock_quantity Quantity remaining.
1257 */
1258 $message = apply_filters( 'woocommerce_cart_product_not_enough_stock_message', $message, $product_data, $stock_quantity );
1259
1260 throw new Exception( $message );
1261 }
1262
1263 // Stock check - this time accounting for whats already in-cart.
1264 if ( $product_data->managing_stock() ) {
1265 $products_qty_in_cart = $this->get_cart_item_quantities();
1266
1267 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 ) ) {
1268 $stock_quantity = $product_data->get_stock_quantity();
1269 $stock_quantity_in_cart = $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ];
1270 $wp_button_class = wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '';
1271
1272 $message = CartCheckoutUtils::has_cart_page() ? sprintf(
1273 '%s <a href="%s" class="button wc-forward%s">%s</a>',
1274 /* translators: 1: quantity in stock 2: current quantity */
1275 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( $stock_quantity, $product_data ), wc_format_stock_quantity_for_display( $stock_quantity_in_cart, $product_data ) ),
1276 esc_url( wc_get_cart_url() ),
1277 esc_attr( $wp_button_class ),
1278 __( 'View cart', 'woocommerce' )
1279 ) : sprintf(
1280 '%s',
1281 /* translators: 1: quantity in stock 2: current quantity */
1282 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( $stock_quantity, $product_data ), wc_format_stock_quantity_for_display( $stock_quantity_in_cart, $product_data ) )
1283 );
1284
1285 /**
1286 * Filters message about product not having enough stock accounting for what's already in the cart.
1287 *
1288 * @param string $message Message.
1289 * @param WC_Product $product_data Product data.
1290 * @param int $stock_quantity Quantity remaining.
1291 * @param int $stock_quantity_in_cart
1292 *
1293 * @since 5.3.0
1294 */
1295 $message = apply_filters( 'woocommerce_cart_product_not_enough_stock_already_in_cart_message', $message, $product_data, $stock_quantity, $stock_quantity_in_cart );
1296
1297 throw new Exception( $message );
1298 }
1299 }
1300
1301 // If cart_item_key is set, the item is already in the cart.
1302 if ( $cart_item_key ) {
1303 $new_quantity = $quantity + $this->cart_contents[ $cart_item_key ]['quantity'];
1304 $this->set_quantity( $cart_item_key, $new_quantity, false );
1305 } else {
1306 $cart_item_key = $cart_id;
1307
1308 // Add item after merging with $cart_item_data - hook to allow plugins to modify cart item.
1309 $this->cart_contents[ $cart_item_key ] = apply_filters(
1310 'woocommerce_add_cart_item',
1311 array_merge(
1312 $cart_item_data,
1313 array(
1314 'key' => $cart_item_key,
1315 'product_id' => $product_id,
1316 'variation_id' => $variation_id,
1317 'variation' => $variation,
1318 'quantity' => $quantity,
1319 'data' => $product_data,
1320 'data_hash' => wc_get_cart_item_data_hash( $product_data ),
1321 )
1322 ),
1323 $cart_item_key
1324 );
1325 }
1326
1327 $this->cart_contents = apply_filters( 'woocommerce_cart_contents_changed', $this->cart_contents );
1328
1329 do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
1330
1331 return $cart_item_key;
1332
1333 } catch ( Exception $e ) {
1334 if ( $e->getMessage() ) {
1335 wc_add_notice( $e->getMessage(), 'error' );
1336 }
1337 return false;
1338 }
1339 }
1340
1341 /**
1342 * Remove a cart item.
1343 *
1344 * @since 2.3.0
1345 * @param string $cart_item_key Cart item key to remove from the cart.
1346 * @return bool
1347 */
1348 public function remove_cart_item( $cart_item_key ) {
1349 if ( isset( $this->cart_contents[ $cart_item_key ] ) ) {
1350 $this->removed_cart_contents[ $cart_item_key ] = $this->cart_contents[ $cart_item_key ];
1351
1352 unset( $this->removed_cart_contents[ $cart_item_key ]['data'] );
1353
1354 do_action( 'woocommerce_remove_cart_item', $cart_item_key, $this );
1355
1356 unset( $this->cart_contents[ $cart_item_key ] );
1357
1358 do_action( 'woocommerce_cart_item_removed', $cart_item_key, $this );
1359
1360 return true;
1361 }
1362 return false;
1363 }
1364
1365 /**
1366 * Restore a cart item.
1367 *
1368 * @param string $cart_item_key Cart item key to restore to the cart.
1369 * @return bool
1370 */
1371 public function restore_cart_item( $cart_item_key ) {
1372 if ( isset( $this->removed_cart_contents[ $cart_item_key ] ) ) {
1373 $restore_item = $this->removed_cart_contents[ $cart_item_key ];
1374 $this->cart_contents[ $cart_item_key ] = $restore_item;
1375 $this->cart_contents[ $cart_item_key ]['data'] = wc_get_product( $restore_item['variation_id'] ? $restore_item['variation_id'] : $restore_item['product_id'] );
1376
1377 do_action( 'woocommerce_restore_cart_item', $cart_item_key, $this );
1378
1379 unset( $this->removed_cart_contents[ $cart_item_key ] );
1380
1381 do_action( 'woocommerce_cart_item_restored', $cart_item_key, $this );
1382
1383 return true;
1384 }
1385 return false;
1386 }
1387
1388 /**
1389 * Set the quantity for an item in the cart using it's key.
1390 *
1391 * @param string $cart_item_key contains the id of the cart item.
1392 * @param int $quantity contains the quantity of the item.
1393 * @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.
1394 * @return bool
1395 */
1396 public function set_quantity( $cart_item_key, $quantity = 1, $refresh_totals = true ) {
1397 if ( 0 === $quantity || $quantity < 0 ) {
1398 wc_do_deprecated_action( 'woocommerce_before_cart_item_quantity_zero', array( $cart_item_key, $this ), '3.7.0', 'woocommerce_remove_cart_item' );
1399 // If we're setting qty to 0 we're removing the item from the cart.
1400 return $this->remove_cart_item( $cart_item_key );
1401 }
1402
1403 // Update qty.
1404 $old_quantity = $this->cart_contents[ $cart_item_key ]['quantity'];
1405 $this->cart_contents[ $cart_item_key ]['quantity'] = $quantity;
1406
1407 do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity, $old_quantity, $this );
1408
1409 if ( $refresh_totals ) {
1410 $this->calculate_totals();
1411 }
1412
1413 /**
1414 * Fired after qty has been changed.
1415 *
1416 * @since 3.6.0
1417 * @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.
1418 * @param int $quantity contains the quantity of the item.
1419 * @param WC_Cart $this Cart class.
1420 */
1421 do_action( 'woocommerce_cart_item_set_quantity', $cart_item_key, $quantity, $this );
1422
1423 return true;
1424 }
1425
1426 /**
1427 * Get cart's owner.
1428 *
1429 * @since 3.2.0
1430 * @return \WC_Customer
1431 */
1432 public function get_customer() {
1433 return WC()->customer;
1434 }
1435
1436 /**
1437 * Calculate totals for the items in the cart.
1438 *
1439 * @uses WC_Cart_Totals
1440 */
1441 public function calculate_totals() {
1442 $this->reset_totals();
1443
1444 if ( $this->is_empty() ) {
1445 $this->session->set_session();
1446 return;
1447 }
1448
1449 do_action( 'woocommerce_before_calculate_totals', $this );
1450
1451 new WC_Cart_Totals( $this );
1452
1453 do_action( 'woocommerce_after_calculate_totals', $this );
1454 }
1455
1456 /**
1457 * Looks at the totals to see if payment is actually required.
1458 *
1459 * @return bool
1460 */
1461 public function needs_payment() {
1462 return apply_filters( 'woocommerce_cart_needs_payment', 0 < $this->get_total( 'edit' ), $this );
1463 }
1464
1465 /*
1466 * Shipping related functions.
1467 */
1468
1469 /**
1470 * Get selected shipping methods after calculation.
1471 *
1472 * @return array
1473 */
1474 public function get_shipping_methods() {
1475 return $this->shipping_methods;
1476 }
1477
1478 /**
1479 * Whether the shipping totals have been calculated.
1480 *
1481 * @return bool
1482 */
1483 public function has_calculated_shipping() {
1484 return $this->has_calculated_shipping;
1485 }
1486
1487 /**
1488 * Uses the shipping class to calculate shipping then gets the totals when its finished.
1489 */
1490 public function calculate_shipping() {
1491 // Reset totals.
1492 $this->set_shipping_total( 0 );
1493 $this->set_shipping_tax( 0 );
1494 $this->set_shipping_taxes( array() );
1495 $this->shipping_methods = array();
1496 $this->has_calculated_shipping = false;
1497
1498 if ( ! $this->needs_shipping() || ! $this->show_shipping() ) {
1499 return $this->shipping_methods;
1500 }
1501
1502 $this->has_calculated_shipping = true;
1503 $this->shipping_methods = $this->get_chosen_shipping_methods( WC()->shipping()->calculate_shipping( $this->get_shipping_packages() ) );
1504
1505 $shipping_costs = wp_list_pluck( $this->shipping_methods, 'cost' );
1506 $shipping_taxes = wp_list_pluck( $this->shipping_methods, 'taxes' );
1507 $merged_taxes = array();
1508 foreach ( $shipping_taxes as $taxes ) {
1509 foreach ( $taxes as $tax_id => $tax_amount ) {
1510 $merged_taxes[ $tax_id ] = ( $merged_taxes[ $tax_id ] ?? 0 ) + $tax_amount;
1511 }
1512 }
1513
1514 $this->set_shipping_total( array_sum( $shipping_costs ) );
1515 $this->set_shipping_tax( array_sum( $merged_taxes ) );
1516 $this->set_shipping_taxes( $merged_taxes );
1517
1518 return $this->shipping_methods;
1519 }
1520
1521 /**
1522 * Given a set of packages with rates, get the chosen ones only.
1523 *
1524 * @since 3.2.0
1525 * @param array $calculated_shipping_packages Array of packages.
1526 * @return array
1527 */
1528 protected function get_chosen_shipping_methods( $calculated_shipping_packages = array() ) {
1529 return ShippingUtil::get_selected_shipping_rates_from_packages( $calculated_shipping_packages );
1530 }
1531
1532 /**
1533 * Filter items needing shipping callback.
1534 *
1535 * @since 3.0.0
1536 * @param array $item Item to check for shipping.
1537 * @return bool
1538 */
1539 protected function filter_items_needing_shipping( $item ) {
1540 $product = $item['data'];
1541 return $product && $product->needs_shipping();
1542 }
1543
1544 /**
1545 * Get only items that need shipping.
1546 *
1547 * @since 3.0.0
1548 * @return array
1549 */
1550 protected function get_items_needing_shipping() {
1551 return array_filter( $this->get_cart(), array( $this, 'filter_items_needing_shipping' ) );
1552 }
1553
1554 /**
1555 * Get packages to calculate shipping for.
1556 *
1557 * This lets us calculate costs for carts that are shipped to multiple locations.
1558 *
1559 * Shipping methods are responsible for looping through these packages.
1560 *
1561 * By default we pass the cart itself as a package - plugins can change this.
1562 * through the filter and break it up.
1563 *
1564 * @since 1.5.4
1565 * @return array of cart items
1566 */
1567 public function get_shipping_packages() {
1568 return apply_filters(
1569 'woocommerce_cart_shipping_packages',
1570 array(
1571 array(
1572 'contents' => $this->get_items_needing_shipping(),
1573 'contents_cost' => array_sum( wp_list_pluck( $this->get_items_needing_shipping(), 'line_total' ) ),
1574 'applied_coupons' => $this->get_applied_coupons(),
1575 'user' => array(
1576 'ID' => get_current_user_id(),
1577 ),
1578 'destination' => array(
1579 'country' => $this->get_customer()->get_shipping_country(),
1580 'state' => $this->get_customer()->get_shipping_state(),
1581 'postcode' => $this->get_customer()->get_shipping_postcode(),
1582 'city' => $this->get_customer()->get_shipping_city(),
1583 'address' => $this->get_customer()->get_shipping_address(),
1584 'address_1' => $this->get_customer()->get_shipping_address(), // Provide both address and address_1 for backwards compatibility.
1585 'address_2' => $this->get_customer()->get_shipping_address_2(),
1586 ),
1587 'cart_subtotal' => $this->get_displayed_subtotal(),
1588 ),
1589 )
1590 );
1591 }
1592
1593 /**
1594 * Looks through the cart to see if shipping is actually required.
1595 *
1596 * @return bool whether or not the cart needs shipping
1597 */
1598 public function needs_shipping() {
1599 if ( ! wc_shipping_enabled() || 0 === wc_get_shipping_method_count( true ) ) {
1600 return false;
1601 }
1602
1603 $needs_shipping = false;
1604
1605 foreach ( $this->get_cart_contents() as $values ) {
1606 if ( $values['data']->needs_shipping() ) {
1607 $needs_shipping = true;
1608 break;
1609 }
1610 }
1611
1612 return apply_filters( 'woocommerce_cart_needs_shipping', $needs_shipping );
1613 }
1614
1615 /**
1616 * Should the shipping address form be shown.
1617 *
1618 * @return bool
1619 */
1620 public function needs_shipping_address() {
1621 return apply_filters( 'woocommerce_cart_needs_shipping_address', true === $this->needs_shipping() && ! wc_ship_to_billing_address_only() );
1622 }
1623
1624 /**
1625 * Sees if the customer has entered enough data to calculate shipping.
1626 *
1627 * @return bool
1628 */
1629 public function show_shipping() {
1630 // If there are no shipping methods or no cart contents, no need to calculate shipping.
1631 if ( ! wc_shipping_enabled() || 0 === wc_get_shipping_method_count( true ) || ! $this->get_cart_contents() ) {
1632 return false;
1633 }
1634
1635 if ( 'yes' === get_option( 'woocommerce_shipping_cost_requires_address' ) ) {
1636 if ( 'store-api' === $this->cart_context ) {
1637 $customer = $this->get_customer();
1638
1639 if ( ! $customer instanceof \WC_Customer || ! $customer->has_full_shipping_address() ) {
1640 return false;
1641 }
1642 } else {
1643 $country = $this->get_customer()->get_shipping_country();
1644 if ( ! $country ) {
1645 return false;
1646 }
1647 $country_fields = WC()->countries->get_address_fields( $country, 'shipping_' );
1648 $checkout_fields = WC()->checkout()->get_checkout_fields();
1649
1650 /**
1651 * Filter to not require shipping state for shipping calculation, even if it is required at checkout.
1652 * This can be used to allow shipping calculations to be done without a state.
1653 *
1654 * @since 8.4.0
1655 *
1656 * @param bool $show_state Whether to use the state field. Default true.
1657 */
1658 $state_enabled = apply_filters( 'woocommerce_shipping_calculator_enable_state', true );
1659 $state_required = isset( $country_fields['shipping_state'] ) && $country_fields['shipping_state']['required'];
1660 // Takes care of late unsetting of checkout fields via hooks (woocommerce_checkout_fields, woocommerce_shipping_fields).
1661 $checkout_state_field_exists = isset( $checkout_fields['shipping']['shipping_state'] );
1662 if ( $state_enabled && $state_required && ! $this->get_customer()->get_shipping_state() && $checkout_state_field_exists ) {
1663 return false;
1664 }
1665 /**
1666 * Filter to not require shipping postcode for shipping calculation, even if it is required at checkout.
1667 * This can be used to allow shipping calculations to be done without a postcode.
1668 *
1669 * @since 8.4.0
1670 *
1671 * @param bool $show_postcode Whether to use the postcode field. Default true.
1672 */
1673 $postcode_enabled = apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true );
1674 $postcode_required = isset( $country_fields['shipping_postcode'] ) && $country_fields['shipping_postcode']['required'];
1675 // Takes care of late unsetting of checkout fields via hooks (woocommerce_checkout_fields, woocommerce_shipping_fields).
1676 $checkout_postcode_field_exists = isset( $checkout_fields['shipping']['shipping_postcode'] );
1677 if ( $postcode_enabled && $postcode_required && '' === $this->get_customer()->get_shipping_postcode() && $checkout_postcode_field_exists ) {
1678 return false;
1679 }
1680 }
1681 }
1682
1683 /**
1684 * Filter to allow plugins to prevent shipping calculations.
1685 *
1686 * @since 2.7.0
1687 *
1688 * @param bool $ready Whether the cart is ready to calculate shipping.
1689 */
1690 return apply_filters( 'woocommerce_cart_ready_to_calc_shipping', true );
1691 }
1692
1693 /**
1694 * Gets the shipping total (after calculation).
1695 *
1696 * @return string price or string for the shipping total
1697 */
1698 public function get_cart_shipping_total() {
1699
1700 // Default total assumes Free shipping.
1701 $total = __( 'Free!', 'woocommerce' );
1702
1703 if ( 0 < $this->get_shipping_total() ) {
1704
1705 if ( $this->display_prices_including_tax() ) {
1706 $total = wc_price( $this->shipping_total + $this->shipping_tax_total );
1707
1708 if ( $this->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
1709 $total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
1710 }
1711 } else {
1712 $total = wc_price( $this->shipping_total );
1713
1714 if ( $this->shipping_tax_total > 0 && wc_prices_include_tax() ) {
1715 $total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
1716 }
1717 }
1718 }
1719 return apply_filters( 'woocommerce_cart_shipping_total', $total, $this );
1720 }
1721
1722 /**
1723 * Check for user coupons (now that we have billing email). If a coupon is invalid, add an error.
1724 *
1725 * Checks two types of coupons:
1726 * 1. Where a list of customer emails are set (limits coupon usage to those defined).
1727 * 2. Where a usage_limit_per_user is set (limits coupon usage to a number based on user ID and email).
1728 *
1729 * @param array $posted Post data.
1730 */
1731 public function check_customer_coupons( $posted ) {
1732 foreach ( $this->get_applied_coupons() as $code ) {
1733 $coupon = new WC_Coupon( $code );
1734
1735 if ( $coupon->is_valid() ) {
1736
1737 // Get user and posted emails to compare.
1738 $current_user = wp_get_current_user();
1739 $billing_email = isset( $posted['billing_email'] ) ? $posted['billing_email'] : '';
1740 $check_emails = array_unique(
1741 array_filter(
1742 array_map(
1743 'strtolower',
1744 array_map(
1745 'sanitize_email',
1746 array(
1747 $billing_email,
1748 $current_user->user_email,
1749 )
1750 )
1751 )
1752 )
1753 );
1754
1755 // Limit to defined email addresses.
1756 $restrictions = $coupon->get_email_restrictions();
1757
1758 if ( is_array( $restrictions ) && 0 < count( $restrictions ) && ! DiscountsUtil::is_coupon_emails_allowed( $check_emails, $restrictions ) ) {
1759 $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED );
1760 $this->remove_coupon( $code );
1761 }
1762
1763 $coupon_usage_limit = $coupon->get_usage_limit_per_user();
1764 if ( 0 < $coupon_usage_limit && 0 === get_current_user_id() ) {
1765 // For guest, usage per user has not been enforced yet. Enforce it now.
1766 $coupon_data_store = $coupon->get_data_store();
1767 $billing_email = strtolower( sanitize_email( $billing_email ) );
1768 if ( $coupon_data_store && $coupon_data_store->get_usage_by_email( $coupon, $billing_email ) >= $coupon_usage_limit ) {
1769 if ( $coupon_data_store->get_tentative_usages_for_user( $coupon->get_id(), array( $billing_email ) ) ) {
1770 $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_COUPON_STUCK_GUEST );
1771 } else {
1772 $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED );
1773 }
1774 }
1775 }
1776 }
1777 }
1778 }
1779
1780 /**
1781 * Checks if the given email address(es) matches the ones specified on the coupon.
1782 *
1783 * @param array $check_emails Array of customer email addresses.
1784 * @param array $restrictions Array of allowed email addresses.
1785 *
1786 * @return bool
1787 * @deprecated 9.0.0 In favor of static method Automattic\WooCommerce\Utilities\DiscountsUtil::is_coupon_emails_allowed.
1788 */
1789 public function is_coupon_emails_allowed( $check_emails, $restrictions ) {
1790 wc_doing_it_wrong(
1791 'WC_Cart::is_coupon_emails_allowed',
1792 __( 'This method has been deprecated and will be removed soon. Use Automattic\WooCommerce\Utilities\DiscountsUtil::is_coupon_emails_allowed instead.', 'woocommerce' ),
1793 '9.0.0'
1794 );
1795
1796 return DiscountsUtil::is_coupon_emails_allowed( $check_emails, $restrictions );
1797 }
1798
1799
1800 /**
1801 * Returns whether or not a discount has been applied.
1802 *
1803 * @param string $coupon_code Coupon code to check.
1804 * @return bool
1805 */
1806 public function has_discount( $coupon_code = '' ) {
1807 return $coupon_code ? in_array(
1808 wc_strtolower( wc_format_coupon_code( $coupon_code ) ),
1809 array_map(
1810 function ( $code ) {
1811 return wc_strtolower( wc_format_coupon_code( $code ) );
1812 },
1813 $this->applied_coupons
1814 ),
1815 true
1816 ) : count( $this->applied_coupons ) > 0;
1817 }
1818
1819 /**
1820 * Applies a coupon code passed to the method.
1821 *
1822 * @param string $coupon_code - The code to apply.
1823 * @return bool True if the coupon is applied, false if it does not exist or cannot be applied.
1824 */
1825 public function apply_coupon( $coupon_code ) {
1826 // Coupons are globally disabled.
1827 if ( ! wc_coupons_enabled() ) {
1828 return false;
1829 }
1830
1831 // Sanitize coupon code.
1832 $coupon_code = wc_format_coupon_code( $coupon_code );
1833
1834 // Get the coupon.
1835 $the_coupon = new WC_Coupon( $coupon_code );
1836
1837 // Prevent adding coupons by post ID.
1838 if ( ! wc_is_same_coupon( $the_coupon->get_code(), $coupon_code ) ) {
1839 $the_coupon->set_code( $coupon_code );
1840 $the_coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_EXIST );
1841 return false;
1842 }
1843
1844 // Check it can be used with cart.
1845 if ( ! $the_coupon->is_valid() ) {
1846 wc_add_notice( $the_coupon->get_error_message(), 'error' );
1847 return false;
1848 }
1849
1850 // Check if applied.
1851 if ( $this->has_discount( $coupon_code ) ) {
1852 $the_coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED );
1853 return false;
1854 }
1855
1856 // If its individual use then remove other coupons.
1857 if ( $the_coupon->get_individual_use() ) {
1858 $coupons_to_keep = apply_filters( 'woocommerce_apply_individual_use_coupon', array(), $the_coupon, $this->applied_coupons );
1859
1860 foreach ( $this->applied_coupons as $applied_coupon ) {
1861 $keep_key = array_search( $applied_coupon, $coupons_to_keep, true );
1862 if ( false === $keep_key ) {
1863 $this->remove_coupon( $applied_coupon );
1864 } else {
1865 unset( $coupons_to_keep[ $keep_key ] );
1866 }
1867 }
1868
1869 if ( ! empty( $coupons_to_keep ) ) {
1870 $this->applied_coupons += $coupons_to_keep;
1871 }
1872 }
1873
1874 // Check to see if an individual use coupon is set.
1875 if ( $this->applied_coupons ) {
1876 foreach ( $this->applied_coupons as $code ) {
1877 $coupon = new WC_Coupon( $code );
1878
1879 if ( $coupon->get_individual_use() && false === apply_filters( 'woocommerce_apply_with_individual_use_coupon', false, $the_coupon, $coupon, $this->applied_coupons ) ) {
1880
1881 // Reject new coupon.
1882 $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY );
1883
1884 return false;
1885 }
1886 }
1887 }
1888
1889 $this->applied_coupons[] = $coupon_code;
1890
1891 // Choose free shipping.
1892 if ( $the_coupon->get_free_shipping() ) {
1893 $packages = WC()->shipping()->get_packages();
1894 $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
1895
1896 foreach ( $packages as $i => $package ) {
1897 $chosen_shipping_methods[ $i ] = 'free_shipping';
1898 }
1899
1900 WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
1901 }
1902
1903 $the_coupon->add_coupon_message( WC_Coupon::WC_COUPON_SUCCESS );
1904
1905 do_action( 'woocommerce_applied_coupon', $coupon_code );
1906
1907 return true;
1908 }
1909
1910 /**
1911 * Get array of applied coupon objects and codes.
1912 *
1913 * @param null $deprecated No longer used.
1914 * @return array of applied coupons
1915 */
1916 public function get_coupons( $deprecated = null ) {
1917 $coupons = array();
1918
1919 if ( 'order' === $deprecated ) {
1920 return $coupons;
1921 }
1922
1923 foreach ( $this->get_applied_coupons() as $code ) {
1924 $coupon = new WC_Coupon( $code );
1925 $coupons[ $code ] = $coupon;
1926 }
1927
1928 return $coupons;
1929 }
1930
1931 /**
1932 * Get the discount amount for a used coupon.
1933 *
1934 * @param string $code coupon code.
1935 * @param bool $ex_tax inc or ex tax.
1936 * @return float discount amount
1937 */
1938 public function get_coupon_discount_amount( $code, $ex_tax = true ) {
1939 $totals = $this->get_coupon_discount_totals();
1940 $discount_amount = isset( $totals[ $code ] ) ? $totals[ $code ] : 0;
1941
1942 if ( ! $ex_tax ) {
1943 $discount_amount += $this->get_coupon_discount_tax_amount( $code );
1944 }
1945
1946 return wc_cart_round_discount( $discount_amount, wc_get_price_decimals() );
1947 }
1948
1949 /**
1950 * Get the discount tax amount for a used coupon (for tax inclusive prices).
1951 *
1952 * @param string $code coupon code.
1953 * @return float discount amount
1954 */
1955 public function get_coupon_discount_tax_amount( $code ) {
1956 $totals = $this->get_coupon_discount_tax_totals();
1957 return wc_cart_round_discount( isset( $totals[ $code ] ) ? $totals[ $code ] : 0, wc_get_price_decimals() );
1958 }
1959
1960 /**
1961 * Remove coupons from the cart of a defined type. Type 1 is before tax, type 2 is after tax.
1962 *
1963 * @param null $deprecated No longer used.
1964 */
1965 public function remove_coupons( $deprecated = null ) {
1966 $this->set_coupon_discount_totals( array() );
1967 $this->set_coupon_discount_tax_totals( array() );
1968 $this->set_applied_coupons( array() );
1969 $this->session->set_session();
1970 }
1971
1972 /**
1973 * Remove a single coupon by code.
1974 *
1975 * @param string $coupon_code Code of the coupon to remove.
1976 * @return bool
1977 */
1978 public function remove_coupon( $coupon_code ) {
1979 $coupon_code = wc_format_coupon_code( $coupon_code );
1980 $position = array_search(
1981 wc_strtolower( $coupon_code ),
1982 array_map(
1983 function ( $code ) {
1984 return wc_strtolower( wc_format_coupon_code( $code ) );
1985 },
1986 $this->get_applied_coupons()
1987 ),
1988 true
1989 );
1990
1991 if ( false !== $position ) {
1992 unset( $this->applied_coupons[ $position ] );
1993 }
1994
1995 WC()->session->set( 'refresh_totals', true );
1996
1997 do_action( 'woocommerce_removed_coupon', $coupon_code );
1998
1999 return true;
2000 }
2001
2002 /**
2003 * Trigger an action so 3rd parties can add custom fees.
2004 *
2005 * @since 2.0.0
2006 */
2007 public function calculate_fees() {
2008 do_action( 'woocommerce_cart_calculate_fees', $this );
2009 }
2010
2011 /**
2012 * Return reference to fees API.
2013 *
2014 * @since 3.2.0
2015 * @return WC_Cart_Fees
2016 */
2017 public function fees_api() {
2018 return $this->fees_api;
2019 }
2020
2021 /**
2022 * Add additional fee to the cart.
2023 *
2024 * This method should be called on a callback attached to the
2025 * woocommerce_cart_calculate_fees action during cart/checkout. Fees do not
2026 * persist.
2027 *
2028 * @uses WC_Cart_Fees::add_fee
2029 * @param string $name Unique name for the fee. Multiple fees of the same name cannot be added.
2030 * @param float $amount Fee amount (do not enter negative amounts).
2031 * @param bool $taxable Is the fee taxable? (default: false).
2032 * @param string $tax_class The tax class for the fee if taxable. A blank string is standard tax class. (default: '').
2033 */
2034 public function add_fee( $name, $amount, $taxable = false, $tax_class = '' ) {
2035 $this->fees_api()->add_fee(
2036 array(
2037 'name' => $name,
2038 'amount' => (float) $amount,
2039 'taxable' => $taxable,
2040 'tax_class' => $tax_class,
2041 )
2042 );
2043 }
2044
2045 /**
2046 * Return all added fees from the Fees API.
2047 *
2048 * @uses WC_Cart_Fees::get_fees
2049 * @return array
2050 */
2051 public function get_fees() {
2052 $fees = $this->fees_api()->get_fees();
2053
2054 if ( property_exists( $this, 'fees' ) ) {
2055 $fees = $fees + (array) $this->fees;
2056 }
2057 return $fees;
2058 }
2059
2060 /**
2061 * Gets the total excluding taxes.
2062 *
2063 * @return string formatted price
2064 */
2065 public function get_total_ex_tax() {
2066 return apply_filters( 'woocommerce_cart_total_ex_tax', wc_price( max( 0, $this->get_total( 'edit' ) - $this->get_total_tax() ) ) );
2067 }
2068
2069 /**
2070 * Gets the cart contents total (after calculation).
2071 *
2072 * @return string formatted price
2073 */
2074 public function get_cart_total() {
2075 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() ) );
2076 }
2077
2078 /**
2079 * Gets the sub total (after calculation).
2080 *
2081 * @param bool $compound whether to include compound taxes.
2082 * @return string formatted price
2083 */
2084 public function get_cart_subtotal( $compound = false ) {
2085 /**
2086 * If the cart has compound tax, we want to show the subtotal as cart + shipping + non-compound taxes (after discount).
2087 */
2088 if ( $compound ) {
2089 $cart_subtotal = wc_price( $this->get_cart_contents_total() + $this->get_shipping_total() + $this->get_taxes_total( false, false ) );
2090
2091 } elseif ( $this->display_prices_including_tax() ) {
2092 $cart_subtotal = wc_price( $this->get_subtotal() + $this->get_subtotal_tax() );
2093
2094 if ( $this->get_subtotal_tax() > 0 && ! wc_prices_include_tax() ) {
2095 $cart_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
2096 }
2097 } else {
2098 $cart_subtotal = wc_price( $this->get_subtotal() );
2099
2100 if ( $this->get_subtotal_tax() > 0 && wc_prices_include_tax() ) {
2101 $cart_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
2102 }
2103 }
2104
2105 return apply_filters( 'woocommerce_cart_subtotal', $cart_subtotal, $compound, $this );
2106 }
2107
2108 /**
2109 * Get the product row price per item.
2110 *
2111 * @param WC_Product $product Product object.
2112 * @return string formatted price
2113 */
2114 public function get_product_price( $product ) {
2115 if ( $this->display_prices_including_tax() ) {
2116 $product_price = wc_get_price_including_tax( $product );
2117 } else {
2118 $product_price = wc_get_price_excluding_tax( $product );
2119 }
2120 return apply_filters( 'woocommerce_cart_product_price', wc_price( $product_price ), $product );
2121 }
2122
2123 /**
2124 * Get the product row subtotal.
2125 *
2126 * Gets the tax etc to avoid rounding issues.
2127 *
2128 * When on the checkout (review order), this will get the subtotal based on the customer's tax rate rather than the base rate.
2129 *
2130 * @param WC_Product $product Product object.
2131 * @param int $quantity Quantity being purchased.
2132 * @return string formatted price
2133 */
2134 public function get_product_subtotal( $product, $quantity ) {
2135 $price = $product->get_price();
2136
2137 if ( $product->is_taxable() ) {
2138
2139 if ( $this->display_prices_including_tax() ) {
2140 $row_price = wc_get_price_including_tax( $product, array( 'qty' => $quantity ) );
2141 $product_subtotal = wc_price( $row_price );
2142
2143 if ( ! wc_prices_include_tax() && $this->get_subtotal_tax() > 0 ) {
2144 $product_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
2145 }
2146 } else {
2147 $row_price = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
2148 $product_subtotal = wc_price( $row_price );
2149
2150 if ( wc_prices_include_tax() && $this->get_subtotal_tax() > 0 ) {
2151 $product_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
2152 }
2153 }
2154 } else {
2155 $row_price = (float) $price * (float) $quantity;
2156 $product_subtotal = wc_price( $row_price );
2157 }
2158
2159 return apply_filters( 'woocommerce_cart_product_subtotal', $product_subtotal, $product, $quantity, $this );
2160 }
2161
2162 /**
2163 * Gets the cart tax (after calculation).
2164 *
2165 * @return string formatted price
2166 */
2167 public function get_cart_tax() {
2168 $cart_total_tax = wc_round_tax_total( $this->get_cart_contents_tax() + $this->get_shipping_tax() + $this->get_fee_tax() );
2169
2170 return apply_filters( 'woocommerce_get_cart_tax', $cart_total_tax ? wc_price( $cart_total_tax ) : '' );
2171 }
2172
2173 /**
2174 * Get a tax amount.
2175 *
2176 * @param string $tax_rate_id ID of the tax rate to get taxes for.
2177 * @return float amount
2178 */
2179 public function get_tax_amount( $tax_rate_id ) {
2180 $taxes = wc_array_merge_recursive_numeric( $this->get_cart_contents_taxes(), $this->get_fee_taxes() );
2181 return isset( $taxes[ $tax_rate_id ] ) ? $taxes[ $tax_rate_id ] : 0;
2182 }
2183
2184 /**
2185 * Get a tax amount.
2186 *
2187 * @param string $tax_rate_id ID of the tax rate to get taxes for.
2188 * @return float amount
2189 */
2190 public function get_shipping_tax_amount( $tax_rate_id ) {
2191 $taxes = $this->get_shipping_taxes();
2192 return isset( $taxes[ $tax_rate_id ] ) ? $taxes[ $tax_rate_id ] : 0;
2193 }
2194
2195 /**
2196 * Get tax row amounts with or without compound taxes includes.
2197 *
2198 * @param bool $compound True if getting compound taxes.
2199 * @param bool $display True if getting total to display.
2200 * @return float price
2201 */
2202 public function get_taxes_total( $compound = true, $display = true ) {
2203 $total = 0;
2204 $taxes = $this->get_taxes();
2205 foreach ( $taxes as $key => $tax ) {
2206 if ( ! $compound && WC_Tax::is_compound( $key ) ) {
2207 continue;
2208 }
2209 $total += $tax;
2210 }
2211 if ( $display ) {
2212 $total = wc_format_decimal( $total, wc_get_price_decimals() );
2213 }
2214 return apply_filters( 'woocommerce_cart_taxes_total', $total, $compound, $display, $this );
2215 }
2216
2217 /**
2218 * Gets the total discount amount.
2219 *
2220 * @return mixed formatted price or false if there are none
2221 */
2222 public function get_total_discount() {
2223 return apply_filters( 'woocommerce_cart_total_discount', $this->get_discount_total() ? wc_price( $this->get_discount_total() ) : false, $this );
2224 }
2225
2226 /**
2227 * Reset cart totals to the defaults. Useful before running calculations.
2228 */
2229 private function reset_totals() {
2230 $this->totals = $this->default_totals;
2231 $this->fees_api->remove_all_fees();
2232 do_action( 'woocommerce_cart_reset', $this, false );
2233 }
2234
2235 /**
2236 * Returns 'incl' if tax should be included in cart, otherwise returns 'excl'.
2237 *
2238 * @return string
2239 */
2240 public function get_tax_price_display_mode() {
2241 if ( $this->get_customer() && $this->get_customer()->get_is_vat_exempt() ) {
2242 return 'excl';
2243 }
2244
2245 return get_option( 'woocommerce_tax_display_cart' );
2246 }
2247
2248 /**
2249 * Returns the hash based on cart contents.
2250 *
2251 * @since 3.6.0
2252 * @return string hash for cart content
2253 */
2254 public function get_cart_hash() {
2255 $cart_session = $this->session->get_cart_for_session();
2256 $hash = $cart_session ? md5( wp_json_encode( $cart_session ) . $this->get_total( 'edit' ) ) : '';
2257 $hash = apply_filters_deprecated( 'woocommerce_add_to_cart_hash', array( $hash, $cart_session ), '3.6.0', 'woocommerce_cart_hash' );
2258
2259 return apply_filters( 'woocommerce_cart_hash', $hash, $cart_session );
2260 }
2261 }
2262