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 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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7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-cart-session.php
woocommerce / includes Last commit date
abstracts 4 days ago admin 4 days ago blocks 1 month ago cli 2 weeks ago customizer 6 months ago data-stores 4 days ago emails 2 weeks ago export 2 weeks ago gateways 2 weeks ago import 1 week ago integrations 1 month ago interfaces 6 months ago legacy 1 month ago libraries 2 weeks ago log-handlers 1 year ago payment-tokens 6 years ago product-usage 1 year ago queue 6 months ago react-admin 1 week ago rest-api 2 days ago shipping 5 months ago shortcodes 2 weeks ago theme-support 2 years ago tracks 2 weeks ago traits 5 years ago walkers 5 years ago wccom-site 2 months ago widgets 1 month ago class-wc-ajax.php 2 days ago class-wc-auth.php 2 years ago class-wc-autoloader.php 10 months ago class-wc-background-emailer.php 3 months ago class-wc-background-updater.php 6 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 2 weeks ago class-wc-breadcrumb.php 6 months ago class-wc-cache-helper.php 2 months ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 2 weeks ago class-wc-cart-totals.php 1 year ago class-wc-cart.php 1 month ago class-wc-checkout.php 1 month ago class-wc-cli.php 2 weeks ago class-wc-comments.php 1 week ago class-wc-countries.php 3 months ago class-wc-coupon.php 2 weeks ago class-wc-customer-download-log.php 6 years ago class-wc-customer-download.php 2 years ago class-wc-customer.php 1 month 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 5 months ago class-wc-discounts.php 2 weeks ago class-wc-download-handler.php 2 weeks ago class-wc-emails.php 1 month ago class-wc-embed.php 3 weeks ago class-wc-form-handler.php 2 weeks ago class-wc-frontend-scripts.php 1 month ago class-wc-geo-ip.php 10 months ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 2 weeks ago class-wc-https.php 3 years ago class-wc-install.php 1 week ago class-wc-integrations.php 6 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 6 months ago class-wc-meta-data.php 2 months ago class-wc-order-factory.php 3 months ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 2 weeks ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 2 weeks ago class-wc-order-item-shipping.php 7 months ago class-wc-order-item-tax.php 2 months ago class-wc-order-item.php 1 month ago class-wc-order-query.php 2 months ago class-wc-order-refund.php 1 year ago class-wc-order.php 1 month ago class-wc-payment-gateways.php 2 weeks ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 1 month ago class-wc-post-types.php 2 weeks ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 1 year ago class-wc-privacy-exporters.php 5 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 6 months ago class-wc-product-download.php 1 month ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 5 months ago class-wc-product-grouped.php 1 month ago class-wc-product-query.php 6 months ago class-wc-product-simple.php 1 year ago class-wc-product-variable.php 1 week ago class-wc-product-variation.php 2 weeks ago class-wc-query.php 1 month ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 2 weeks ago class-wc-regenerate-images.php 2 weeks ago class-wc-register-wp-admin-settings.php 5 years ago class-wc-rest-authentication.php 2 days ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 3 weeks ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 2 weeks ago class-wc-shipping.php 1 month ago class-wc-shortcodes.php 2 weeks ago class-wc-structured-data.php 2 weeks ago class-wc-tax.php 1 month ago class-wc-template-loader.php 9 months ago class-wc-tracker.php 1 month ago class-wc-validation.php 1 month ago class-wc-webhook.php 2 weeks ago class-woocommerce.php 1 day ago wc-account-functions.php 2 months ago wc-attribute-functions.php 1 month ago wc-brands-functions.php 1 year ago wc-cart-functions.php 2 weeks ago wc-conditional-functions.php 2 weeks ago wc-core-functions.php 2 weeks ago wc-coupon-functions.php 7 months ago wc-deprecated-functions.php 2 weeks ago wc-formatting-functions.php 2 weeks ago wc-interactivity-api-functions.php 3 months ago wc-notice-functions.php 7 months ago wc-order-functions.php 2 weeks ago wc-order-item-functions.php 3 years ago wc-order-step-logger-functions.php 3 weeks ago wc-page-functions.php 1 month ago wc-product-functions.php 2 weeks ago wc-rest-functions.php 9 months ago wc-stock-functions.php 1 month ago wc-template-functions.php 1 week ago wc-template-hooks.php 1 year ago wc-term-functions.php 2 weeks ago wc-update-functions.php 1 week ago wc-user-functions.php 1 month ago wc-webhook-functions.php 2 months ago wc-widget-functions.php 6 years ago
class-wc-cart-session.php
752 lines
1 <?php
2 /**
3 * Cart session handling class.
4 *
5 * @package WooCommerce\Classes
6 * @version 3.2.0
7 */
8
9 use Automattic\WooCommerce\Enums\OrderStatus;
10 use Automattic\WooCommerce\Enums\ProductType;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * WC_Cart_Session class.
18 *
19 * @since 3.2.0
20 */
21 final class WC_Cart_Session {
22
23 /**
24 * Reference to cart object.
25 *
26 * @since 3.2.0
27 * @var WC_Cart
28 */
29 protected $cart;
30
31 /**
32 * Sets up the items provided, and calculate totals.
33 *
34 * @since 3.2.0
35 * @throws Exception If missing WC_Cart object.
36 *
37 * @param WC_Cart $cart Cart object to calculate totals for.
38 */
39 public function __construct( $cart ) {
40 if ( ! is_a( $cart, 'WC_Cart' ) ) {
41 throw new Exception( 'A valid WC_Cart object is required' );
42 }
43
44 $this->set_cart( $cart );
45 }
46
47 /**
48 * Sets the cart instance.
49 *
50 * @param WC_Cart $cart Cart object.
51 */
52 public function set_cart( WC_Cart $cart ) {
53 $this->cart = $cart;
54 }
55
56
57 /**
58 * Register methods for this object on the appropriate WordPress hooks.
59 */
60 public function init() {
61 /**
62 * Filters whether hooks should be initialized for the current cart session.
63 *
64 * @param bool $must_initialize Will be passed as true, meaning that the cart hooks should be initialized.
65 * @param bool $session The WC_Cart_Session object that is being initialized.
66 * @returns bool True if the cart hooks should be actually initialized, false if not.
67 *
68 * @since 6.9.0
69 */
70 if ( ! apply_filters( 'woocommerce_cart_session_initialize', true, $this ) ) {
71 return;
72 }
73
74 add_action( 'wp_loaded', array( $this, 'get_cart_from_session' ) );
75 add_action( 'woocommerce_cart_emptied', array( $this, 'destroy_cart_session' ) );
76 add_action( 'woocommerce_after_calculate_totals', array( $this, 'set_session' ), 1000 );
77 add_action( 'woocommerce_removed_coupon', array( $this, 'set_session' ) );
78
79 // Persistent cart stored to usermeta.
80 add_action( 'woocommerce_add_to_cart', array( $this, 'persistent_cart_update' ) );
81 add_action( 'woocommerce_cart_item_removed', array( $this, 'persistent_cart_update' ) );
82 add_action( 'woocommerce_cart_item_restored', array( $this, 'persistent_cart_update' ) );
83 add_action( 'woocommerce_cart_item_set_quantity', array( $this, 'persistent_cart_update' ) );
84
85 // Cookie events - cart cookies need to be set before headers are sent.
86 add_action( 'woocommerce_add_to_cart', array( $this, 'maybe_set_cart_cookies' ) );
87 add_action( 'wp', array( $this, 'maybe_set_cart_cookies' ), 99 );
88 add_action( 'shutdown', array( $this, 'maybe_set_cart_cookies' ), 0 );
89
90 add_action( 'template_redirect', array( $this, 'clean_up_removed_cart_contents' ) );
91 }
92
93 /**
94 * Get the cart data from the PHP session and store it in class variables.
95 *
96 * @since 3.2.0
97 */
98 public function get_cart_from_session() {
99 /**
100 * Fires when cart is loaded from session.
101 *
102 * @since 3.2.0
103 */
104 do_action( 'woocommerce_load_cart_from_session' );
105
106 $this->cart->set_totals( WC()->session->get( 'cart_totals', null ) );
107 $this->cart->set_applied_coupons( WC()->session->get( 'applied_coupons', array() ) );
108 $this->cart->set_coupon_discount_totals( WC()->session->get( 'coupon_discount_totals', array() ) );
109 $this->cart->set_coupon_discount_tax_totals( WC()->session->get( 'coupon_discount_tax_totals', array() ) );
110 $this->cart->set_removed_cart_contents( WC()->session->get( 'removed_cart_contents', array() ) );
111
112 $update_cart_session = false; // Flag to indicate the stored cart should be updated.
113 $order_again = false; // Flag to indicate whether this is a re-order.
114 $cart = WC()->session->get( 'cart', null );
115 $merge_saved_cart = (bool) get_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login', true );
116
117 // Merge saved cart with current cart.
118 if ( is_null( $cart ) || $merge_saved_cart ) {
119 $saved_cart = $this->get_saved_cart();
120 $cart = is_null( $cart ) ? array() : $cart;
121 $cart = array_merge( $saved_cart, $cart );
122 $update_cart_session = true;
123
124 delete_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login' );
125 }
126
127 // Populate cart from order.
128 if ( isset( $_GET['order_again'], $_GET['_wpnonce'] ) && is_user_logged_in() && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), 'woocommerce-order_again' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
129 $cart = $this->populate_cart_from_order( absint( $_GET['order_again'] ), $cart );
130 $order_again = true;
131 $update_cart_session = true;
132 }
133
134 if ( ! empty( $cart ) ) {
135 // Prime caches to reduce future queries.
136 $product_ids = array_filter( array_column( $cart, 'product_id' ) );
137 $variations_ids = array_filter( array_column( $cart, 'variation_id' ) );
138 _prime_post_caches( array_merge( $product_ids, $variations_ids ) );
139 }
140
141 $cart_contents = array();
142
143 foreach ( $cart as $key => $values ) {
144 if ( ! is_customize_preview() && 'customize-preview' === $key ) {
145 continue;
146 }
147
148 $product = wc_get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] );
149
150 if ( empty( $product ) || ! $product->exists() || 0 >= $values['quantity'] ) {
151 continue;
152 }
153
154 /**
155 * Allow 3rd parties to validate this item before it's added to cart and add their own notices.
156 *
157 * @since 3.6.0
158 *
159 * @param bool $remove_cart_item_from_session If true, the item will not be added to the cart. Default: false.
160 * @param string $key Cart item key.
161 * @param array $values Cart item values e.g. quantity and product_id.
162 * @param WC_Product $product The product being added to the cart.
163 */
164 if ( apply_filters( 'woocommerce_pre_remove_cart_item_from_session', false, $key, $values, $product ) ) {
165 $update_cart_session = true;
166 /**
167 * Fires when cart item is removed from the session.
168 *
169 * @since 3.6.0
170 *
171 * @param string $key Cart item key.
172 * @param array $values Cart item values e.g. quantity and product_id.
173 * @param WC_Product $product The product being added to the cart.
174 */
175 do_action( 'woocommerce_remove_cart_item_from_session', $key, $values, $product );
176
177 /**
178 * Allow 3rd parties to override this item's is_purchasable() result with cart item data.
179 *
180 * @param bool $is_purchasable If false, the item will not be added to the cart. Default: product's is_purchasable() status.
181 * @param string $key Cart item key.
182 * @param array $values Cart item values e.g. quantity and product_id.
183 * @param WC_Product $product The product being added to the cart.
184 *
185 * @since 7.0.0
186 */
187 } elseif ( ! apply_filters( 'woocommerce_cart_item_is_purchasable', $product->is_purchasable(), $key, $values, $product ) ) {
188 $update_cart_session = true;
189 /* translators: %s: product name */
190 $message = sprintf( __( '%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.', 'woocommerce' ), $product->get_name() );
191 /**
192 * Filter message about item removed from the cart.
193 *
194 * @since 3.8.0
195 * @param string $message Message.
196 * @param WC_Product $product Product data.
197 */
198 $message = apply_filters( 'woocommerce_cart_item_removed_message', $message, $product );
199 wc_add_notice( $message, 'error' );
200 /**
201 * Fires when cart item is removed from the session.
202 *
203 * @since 3.6.0
204 *
205 * @param string $key Cart item key.
206 * @param array $values Cart item values e.g. quantity and product_id.
207 */
208 do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );
209 } elseif ( ! empty( $values['data_hash'] ) && ! hash_equals( $values['data_hash'], wc_get_cart_item_data_hash( $product ) ) ) { // phpcs:ignore PHPCompatibility.PHP.NewFunctions.hash_equalsFound
210 $update_cart_session = true;
211 /* translators: %1$s: product name. %2$s product permalink */
212 $message = sprintf( __( '%1$s has been removed from your cart because it has since been modified. You can add it back to your cart <a href="%2$s">here</a>.', 'woocommerce' ), $product->get_name(), $product->get_permalink() );
213
214 /**
215 * Filter message about item removed from the cart because it has since been modified.
216 *
217 * @since 3.8.0
218 * @param string $message Message.
219 * @param WC_Product $product Product data.
220 */
221 $message = apply_filters( 'woocommerce_cart_item_removed_because_modified_message', $message, $product );
222 wc_add_notice( $message, 'notice' );
223
224 /**
225 * Fires when cart item is removed from the session.
226 *
227 * @since 3.6.0
228 *
229 * @param string $key Cart item key.
230 * @param array $values Cart item values e.g. quantity and product_id.
231 */
232 do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );
233
234 } else {
235 // Put session data into array. Run through filter so other plugins can load their own session data.
236 $session_data = array_merge(
237 $values,
238 array(
239 'data' => $product,
240 )
241 );
242
243 /**
244 * Filter to modify or add session data to the cart contents.
245 *
246 * @since 3.2.0
247 *
248 * @param array $session_data Data for an item in the cart.
249 * @param array $values Data for an item in the cart, without the product object.
250 * @param string $key The cart item hash.
251 */
252 $cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $session_data, $values, $key );
253 if ( ! isset( $cart_contents[ $key ]['data'] ) || ! $cart_contents[ $key ]['data'] instanceof WC_Product ) {
254 // If the cart contents is missing the product object after filtering, something is wrong.
255 wc_doing_it_wrong(
256 __METHOD__,
257 'When filtering cart items with woocommerce_get_cart_item_from_session, each item must have a data key containing a product object.',
258 '9.8.0'
259 );
260
261 // Add the product back in.
262 $cart_contents[ $key ]['data'] = $product;
263 }
264
265 // Add to cart right away so the product is visible in woocommerce_get_cart_item_from_session hook.
266 $this->cart->set_cart_contents( $cart_contents );
267 }
268 }
269
270 // If it's not empty, it's been already populated by the loop above.
271 if ( ! empty( $cart_contents ) ) {
272 /**
273 * Filter the cart contents.
274 *
275 * @since 3.2.0
276 *
277 * @param array $cart_contents The cart contents.
278 */
279 $this->cart->set_cart_contents( apply_filters( 'woocommerce_cart_contents_changed', $cart_contents ) );
280 }
281
282 /**
283 * Fires when cart is loaded from session.
284 *
285 * @since 3.2.0
286 *
287 * @param WC_Cart $cart The cart object.
288 */
289 do_action( 'woocommerce_cart_loaded_from_session', $this->cart );
290
291 if ( $update_cart_session || is_null( WC()->session->get( 'cart_totals', null ) ) ) {
292 $cart_for_session = $this->get_cart_for_session();
293 WC()->session->set( 'cart', empty( $cart_for_session ) ? null : $cart_for_session );
294 $this->cart->calculate_totals();
295
296 if ( $merge_saved_cart || $update_cart_session ) {
297 $this->persistent_cart_update();
298 }
299 }
300
301 // If this is a re-order, redirect to the cart page to get rid of the `order_again` query string.
302 if ( $order_again ) {
303 wp_safe_redirect( wc_get_cart_url() );
304 exit;
305 }
306 }
307
308 /**
309 * Destroy cart session data.
310 *
311 * @since 3.2.0
312 */
313 public function destroy_cart_session() {
314 $wc_session = WC()->session;
315
316 $wc_session->set( 'cart', null );
317 $wc_session->set( 'cart_totals', null );
318 $wc_session->set( 'applied_coupons', null );
319 $wc_session->set( 'coupon_discount_totals', null );
320 $wc_session->set( 'coupon_discount_tax_totals', null );
321 $wc_session->set( 'removed_cart_contents', null );
322 $wc_session->set( 'order_awaiting_payment', null );
323 $wc_session->set( 'store_api_draft_order', null );
324 $wc_session->set( 'shipping_method_counts', null );
325 $wc_session->set( 'previous_shipping_methods', null );
326 $wc_session->set( 'chosen_shipping_methods', null );
327 $this->remove_shipping_for_package_from_session();
328 }
329
330 /**
331 * Will set cart cookies if needed and when possible.
332 *
333 * Headers are only updated if headers have not yet been sent.
334 *
335 * @since 3.2.0
336 */
337 public function maybe_set_cart_cookies() {
338 if ( headers_sent() || ! did_action( 'wp_loaded' ) ) {
339 return;
340 }
341 if ( ! $this->cart->is_empty() ) {
342 $this->set_cart_cookies( true );
343 } elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { // WPCS: input var ok.
344 $this->set_cart_cookies( false );
345 }
346 $this->dedupe_cookies();
347 }
348
349 /**
350 * Remove duplicate cookies from the response.
351 */
352 private function dedupe_cookies() {
353 $all_cookies = array_filter(
354 headers_list(),
355 function ( $header ) {
356 return stripos( $header, 'Set-Cookie:' ) !== false;
357 }
358 );
359 $final_cookies = array();
360 $update_cookies = false;
361 foreach ( $all_cookies as $cookie ) {
362
363 list(, $cookie_value) = explode( ':', $cookie, 2 );
364 list($cookie_name, $cookie_value) = explode( '=', trim( $cookie_value ), 2 );
365
366 if ( stripos( $cookie_name, 'woocommerce_' ) !== false ) {
367 $key = $this->find_cookie_by_name( $cookie_name, $final_cookies );
368 if ( false !== $key ) {
369 $update_cookies = true;
370 unset( $final_cookies[ $key ] );
371 }
372 }
373 $final_cookies[] = $cookie;
374 }
375
376 if ( $update_cookies ) {
377 header_remove( 'Set-Cookie' );
378 foreach ( $final_cookies as $cookie ) {
379 // Using header here preserves previous cookie args.
380 header( $cookie, false );
381 }
382 }
383 }
384
385 /**
386 * Find a cookie by name in an array of cookies.
387 *
388 * @param string $cookie_name Name of the cookie to find.
389 * @param array $cookies Array of cookies to search.
390 * @return mixed Key of the cookie if found, false if not.
391 */
392 private function find_cookie_by_name( $cookie_name, $cookies ) {
393 foreach ( $cookies as $key => $cookie ) {
394 if ( strpos( $cookie, $cookie_name ) !== false ) {
395 return $key;
396 }
397 }
398 return false;
399 }
400
401 /**
402 * Sets the php session data for the cart and coupons.
403 */
404 public function set_session() {
405 $wc_session = WC()->session;
406
407 $cart = $this->get_cart_for_session();
408 $applied_coupons = $this->cart->get_applied_coupons();
409 $coupon_discount_totals = $this->cart->get_coupon_discount_totals();
410 $coupon_discount_tax_totals = $this->cart->get_coupon_discount_tax_totals();
411 $removed_cart_contents = $this->cart->get_removed_cart_contents();
412
413 /*
414 * We want to clear out any empty/default data from the session that have no value in being stored so the session
415 * can be forgotten if empty.
416 */
417 $wc_session->set( 'cart_totals', empty( $cart ) ? null : $this->cart->get_totals() );
418 $wc_session->set( 'cart', empty( $cart ) ? null : $cart );
419 $wc_session->set( 'applied_coupons', empty( $applied_coupons ) ? null : $applied_coupons );
420 $wc_session->set( 'coupon_discount_totals', empty( $coupon_discount_totals ) ? null : $coupon_discount_totals );
421 $wc_session->set( 'coupon_discount_tax_totals', empty( $coupon_discount_tax_totals ) ? null : $coupon_discount_tax_totals );
422 $wc_session->set( 'removed_cart_contents', empty( $removed_cart_contents ) ? null : $removed_cart_contents );
423 if ( ! $this->cart_has_shippable_products() ) {
424 $wc_session->set( 'shipping_method_counts', null );
425 $wc_session->set( 'previous_shipping_methods', null );
426 $wc_session->set( 'chosen_shipping_methods', null );
427 $this->remove_shipping_for_package_from_session();
428 }
429 if ( empty( $cart ) ) {
430 $wc_session->set( 'store_api_draft_order', null );
431 }
432
433 /**
434 * Fires when cart is updated.
435 *
436 * @since 3.2.0
437 */
438 do_action( 'woocommerce_cart_updated' );
439 }
440
441 /**
442 * Returns the contents of the cart in an array without the 'data' element.
443 *
444 * @return array contents of the cart
445 */
446 public function get_cart_for_session() {
447 $cart_session = array();
448
449 foreach ( $this->cart->get_cart() as $key => $values ) {
450 $cart_session[ $key ] = $values;
451 unset( $cart_session[ $key ]['data'] ); // Unset product object.
452 }
453
454 return $cart_session;
455 }
456
457 /**
458 * Save the persistent cart when the cart is updated.
459 */
460 public function persistent_cart_update() {
461 /**
462 * Filters whether the persistent cart is enabled.
463 *
464 * @since 3.4.0
465 * @param bool $enabled Whether the persistent cart is enabled. Default true.
466 */
467 if ( get_current_user_id() && apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
468 update_user_meta(
469 get_current_user_id(),
470 '_woocommerce_persistent_cart_' . get_current_blog_id(),
471 array(
472 'cart' => $this->get_cart_for_session(),
473 )
474 );
475 }
476 }
477
478 /**
479 * Delete the persistent cart permanently.
480 */
481 public function persistent_cart_destroy() {
482 /**
483 * Filters whether the persistent cart is enabled.
484 *
485 * @since 3.4.0
486 * @param bool $enabled Whether the persistent cart is enabled. Default true.
487 */
488 if ( get_current_user_id() && apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
489 delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id() );
490 }
491 }
492
493 /**
494 * Set cart hash cookie and items in cart if not already set.
495 *
496 * @param bool $set Should cookies be set (true) or unset.
497 */
498 private function set_cart_cookies( $set = true ) {
499 if ( $set ) {
500 $setcookies = array(
501 'woocommerce_items_in_cart' => '1',
502 'woocommerce_cart_hash' => WC()->cart->get_cart_hash(),
503 );
504 foreach ( $setcookies as $name => $value ) {
505 if ( ! isset( $_COOKIE[ $name ] ) || $_COOKIE[ $name ] !== $value ) {
506 wc_setcookie( $name, $value );
507 $_COOKIE[ $name ] = $value;
508 }
509 }
510 } else {
511 $unsetcookies = array(
512 'woocommerce_items_in_cart',
513 'woocommerce_cart_hash',
514 );
515 foreach ( $unsetcookies as $name ) {
516 if ( isset( $_COOKIE[ $name ] ) ) {
517 wc_setcookie( $name, 0, time() - HOUR_IN_SECONDS );
518 unset( $_COOKIE[ $name ] );
519 }
520 }
521 }
522
523 do_action( 'woocommerce_set_cart_cookies', $set );
524 }
525
526 /**
527 * Get the persistent cart from the database.
528 *
529 * @since 3.5.0
530 * @return array
531 */
532 private function get_saved_cart() {
533 $saved_cart = array();
534
535 /**
536 * Filters whether the persistent cart is enabled.
537 *
538 * @since 3.4.0
539 * @param bool $enabled Whether the persistent cart is enabled. Default true.
540 */
541 if ( apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
542 $saved_cart_meta = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true );
543
544 if ( is_array( $saved_cart_meta ) && isset( $saved_cart_meta['cart'] ) ) {
545 $saved_cart = array_filter( (array) $saved_cart_meta['cart'] );
546 }
547 }
548
549 return $saved_cart;
550 }
551
552 /**
553 * Get a cart from an order, if user has permission.
554 *
555 * @since 3.5.0
556 *
557 * @param int $order_id Order ID to try to load.
558 * @param array $cart Current cart array.
559 *
560 * @return array
561 */
562 private function populate_cart_from_order( $order_id, $cart ) {
563 $order = wc_get_order( $order_id );
564
565 /**
566 * Filter the valid order statuses for reordering.
567 *
568 * @since 3.6.0
569 *
570 * @param array $valid_statuses Array of valid order statuses.
571 */
572 $valid_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_order_again', array( OrderStatus::COMPLETED ) );
573 if ( ! $order->get_id() || ! $order->has_status( $valid_statuses ) || ! current_user_can( 'order_again', $order->get_id() ) ) {
574 return;
575 }
576
577 if ( apply_filters( 'woocommerce_empty_cart_when_order_again', true ) ) {
578 $cart = array();
579 }
580
581 $inital_cart_size = count( $cart );
582 $order_items = $order->get_items();
583
584 foreach ( $order_items as $item ) {
585 $product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $item->get_product_id() );
586 $quantity = $item->get_quantity();
587 $variation_id = (int) $item->get_variation_id();
588 $variations = array();
589 $cart_item_data = apply_filters( 'woocommerce_order_again_cart_item_data', array(), $item, $order );
590 $product = $item->get_product();
591
592 if ( ! $product ) {
593 continue;
594 }
595
596 // Prevent reordering variable products if no selected variation.
597 if ( ! $variation_id && $product->is_type( ProductType::VARIABLE ) ) {
598 continue;
599 }
600
601 // Prevent reordering items specifically out of stock.
602 if ( ! $product->is_in_stock() ) {
603 continue;
604 }
605
606 foreach ( $item->get_meta_data() as $meta ) {
607 if ( taxonomy_is_product_attribute( $meta->key ) || meta_is_product_attribute( $meta->key, $meta->value, $product_id ) ) {
608 $attribute_key = 'attribute_' . sanitize_title( $meta->key );
609 if ( taxonomy_is_product_attribute( $meta->key ) ) {
610 $variations[ $attribute_key ] = sanitize_title( $meta->value );
611 } else {
612 $variations[ $attribute_key ] = html_entity_decode( wc_clean( $meta->value ), ENT_QUOTES, get_bloginfo( 'charset' ) );
613 }
614 }
615 }
616
617 if ( ! apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data ) ) {
618 continue;
619 }
620
621 $product_data = wc_get_product( $variation_id ? $variation_id : $product_id );
622 if ( $product_data instanceof WC_Product && $product_data->is_sold_individually() ) {
623 // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- Filter documented in CartController::validate_add_to_cart().
624 $quantity = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data );
625
626 $cart_id = WC()->cart->generate_cart_id( $product_id, $variation_id, $variations, $cart_item_data );
627 // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- Filter first added in WC_Cart::add_to_cart(), add documentation there.
628 $found_in_cart = apply_filters( 'woocommerce_add_to_cart_sold_individually_found_in_cart', isset( $cart[ $cart_id ] ) && isset( $cart[ $cart_id ]['quantity'] ) && $cart[ $cart_id ]['quantity'] > 0, $product_id, $variation_id, $cart_item_data, $cart_id );
629 if ( $found_in_cart ) {
630 /* translators: %s: product name */
631 $message = sprintf( __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product_data->get_name() );
632 // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- Filter documented in WC_Cart::add_to_cart().
633 $message = apply_filters( 'woocommerce_cart_product_cannot_add_another_message', $message, $product_data );
634 $wp_button_class = wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '';
635 $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' ) );
636 wc_add_notice( $message, 'error' );
637 continue;
638 }
639 }
640
641 // Add to cart directly.
642 $cart_id = WC()->cart->generate_cart_id( $product_id, $variation_id, $variations, $cart_item_data );
643 $product_data = wc_get_product( $variation_id ? $variation_id : $product_id );
644 $cart[ $cart_id ] = apply_filters(
645 'woocommerce_add_order_again_cart_item',
646 array_merge(
647 $cart_item_data,
648 array(
649 'key' => $cart_id,
650 'product_id' => $product_id,
651 'variation_id' => $variation_id,
652 'variation' => $variations,
653 'quantity' => $quantity,
654 'data' => $product_data,
655 'data_hash' => wc_get_cart_item_data_hash( $product_data ),
656 )
657 ),
658 $cart_id
659 );
660 }
661
662 do_action_ref_array( 'woocommerce_ordered_again', array( $order->get_id(), $order_items, &$cart ) );
663
664 $num_items_in_cart = count( $cart );
665 $num_items_in_original_order = count( $order_items );
666 $num_items_added = $num_items_in_cart - $inital_cart_size;
667
668 if ( $num_items_in_original_order > $num_items_added ) {
669 wc_add_notice(
670 sprintf(
671 /* translators: %d item count */
672 _n(
673 '%d item from your previous order is currently unavailable and could not be added to your cart.',
674 '%d items from your previous order are currently unavailable and could not be added to your cart.',
675 $num_items_in_original_order - $num_items_added,
676 'woocommerce'
677 ),
678 $num_items_in_original_order - $num_items_added
679 ),
680 'error'
681 );
682 }
683
684 if ( 0 < $num_items_added ) {
685 wc_add_notice( __( 'The cart has been filled with the items from your previous order.', 'woocommerce' ) );
686 }
687
688 return $cart;
689 }
690
691 /**
692 * Remove shipping data for all packages from session.
693 *
694 * @return void
695 */
696 private function remove_shipping_for_package_from_session() {
697 $wc_session = WC()->session;
698
699 if ( ! is_a( $wc_session, 'WC_Session_Handler' ) ) {
700 return;
701 }
702
703 foreach ( array_keys( $wc_session->get_session_data() ) as $key ) {
704 if ( 0 === strpos( $key, 'shipping_for_package_' ) ) {
705 $wc_session->set( $key, null );
706 }
707 }
708 }
709
710 /**
711 * Check if the cart has shippable products.
712 *
713 * @return bool
714 */
715 private function cart_has_shippable_products() {
716 foreach ( $this->cart->get_cart() as $cart_item ) {
717 if ( $cart_item['data']->needs_shipping() ) {
718 return true;
719 }
720 }
721 return false;
722 }
723
724 /**
725 * Removes items from the removed cart contents on next user initiated request.
726 *
727 * @return void
728 */
729 public function clean_up_removed_cart_contents() {
730 // Limit to page requests initiated by the user.
731 $is_page = is_singular() || is_archive() || is_search();
732
733 if ( is_404() || ! $is_page ) {
734 return;
735 }
736
737 // Don't cleanup if user just removed an item (undo link is being displayed).
738 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
739 if ( isset( $_GET['removed_item'] ) ) {
740 return;
741 }
742
743 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
744 if ( isset( $_GET['undo_item'] ) ) {
745 return;
746 }
747
748 WC()->session->set( 'removed_cart_contents', null );
749 $this->cart->set_removed_cart_contents( array() );
750 }
751 }
752