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