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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-cart-session.php
woocommerce / includes Last commit date
abstracts 4 years ago admin 4 years ago blocks 5 years ago cli 4 years ago customizer 4 years ago data-stores 4 years ago emails 4 years ago export 4 years ago gateways 4 years ago import 4 years ago integrations 5 years ago interfaces 4 years ago legacy 4 years ago libraries 6 years ago log-handlers 5 years ago payment-tokens 5 years ago queue 4 years ago react-admin 4 years ago rest-api 4 years ago shipping 4 years ago shortcodes 5 years ago theme-support 4 years ago tracks 4 years ago traits 5 years ago walkers 5 years ago wccom-site 5 years ago widgets 4 years ago class-wc-ajax.php 4 years ago class-wc-api.php 4 years ago class-wc-auth.php 4 years ago class-wc-autoloader.php 5 years ago class-wc-background-emailer.php 5 years ago class-wc-background-updater.php 5 years ago class-wc-breadcrumb.php 5 years ago class-wc-cache-helper.php 4 years ago class-wc-cart-fees.php 5 years ago class-wc-cart-session.php 4 years ago class-wc-cart-totals.php 4 years ago class-wc-cart.php 4 years ago class-wc-checkout.php 4 years ago class-wc-cli.php 4 years ago class-wc-comments.php 4 years ago class-wc-countries.php 4 years ago class-wc-coupon.php 4 years ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 4 years ago class-wc-customer.php 4 years ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 6 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 8 years ago class-wc-deprecated-filter-hooks.php 4 years ago class-wc-discounts.php 4 years ago class-wc-download-handler.php 4 years ago class-wc-emails.php 5 years ago class-wc-embed.php 5 years ago class-wc-form-handler.php 4 years ago class-wc-frontend-scripts.php 4 years ago class-wc-geo-ip.php 4 years ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 4 years ago class-wc-https.php 4 years ago class-wc-install.php 4 years ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 5 years ago class-wc-logger.php 4 years ago class-wc-meta-data.php 4 years ago class-wc-order-factory.php 5 years ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 4 years ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 4 years ago class-wc-order-item-shipping.php 4 years ago class-wc-order-item-tax.php 4 years ago class-wc-order-item.php 4 years ago class-wc-order-query.php 4 years ago class-wc-order-refund.php 4 years ago class-wc-order.php 4 years ago class-wc-payment-gateways.php 4 years ago class-wc-payment-tokens.php 5 years ago class-wc-post-data.php 4 years ago class-wc-post-types.php 4 years ago class-wc-privacy-background-process.php 5 years ago class-wc-privacy-erasers.php 4 years ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 4 years ago class-wc-product-attribute.php 4 years ago class-wc-product-download.php 4 years ago class-wc-product-external.php 5 years ago class-wc-product-factory.php 5 years ago class-wc-product-grouped.php 8 years ago class-wc-product-query.php 5 years ago class-wc-product-simple.php 5 years ago class-wc-product-variable.php 4 years ago class-wc-product-variation.php 4 years ago class-wc-query.php 4 years ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 5 years ago class-wc-regenerate-images.php 5 years ago class-wc-register-wp-admin-settings.php 4 years ago class-wc-rest-authentication.php 5 years ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 4 years ago class-wc-shipping-rate.php 4 years ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 4 years ago class-wc-shortcodes.php 5 years ago class-wc-structured-data.php 5 years ago class-wc-tax.php 4 years ago class-wc-template-loader.php 4 years ago class-wc-tracker.php 4 years ago class-wc-validation.php 5 years ago class-wc-webhook.php 5 years ago class-woocommerce.php 4 years ago wc-account-functions.php 5 years ago wc-attribute-functions.php 4 years ago wc-cart-functions.php 4 years ago wc-conditional-functions.php 4 years ago wc-core-functions.php 4 years ago wc-coupon-functions.php 5 years ago wc-deprecated-functions.php 4 years ago wc-formatting-functions.php 4 years ago wc-notice-functions.php 5 years ago wc-order-functions.php 4 years ago wc-order-item-functions.php 5 years ago wc-page-functions.php 5 years ago wc-product-functions.php 4 years ago wc-rest-functions.php 4 years ago wc-stock-functions.php 4 years ago wc-template-functions.php 4 years ago wc-template-hooks.php 5 years ago wc-term-functions.php 4 years ago wc-update-functions.php 4 years ago wc-user-functions.php 4 years ago wc-webhook-functions.php 4 years ago wc-widget-functions.php 5 years ago
class-wc-cart-session.php
438 lines
1 <?php
2 /**
3 * Cart session handling class.
4 *
5 * @package WooCommerce\Classes
6 * @version 3.2.0
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * WC_Cart_Session class.
15 *
16 * @since 3.2.0
17 */
18 final class WC_Cart_Session {
19
20 /**
21 * Reference to cart object.
22 *
23 * @since 3.2.0
24 * @var WC_Cart
25 */
26 protected $cart;
27
28 /**
29 * Sets up the items provided, and calculate totals.
30 *
31 * @since 3.2.0
32 * @throws Exception If missing WC_Cart object.
33 *
34 * @param WC_Cart $cart Cart object to calculate totals for.
35 */
36 public function __construct( &$cart ) {
37 if ( ! is_a( $cart, 'WC_Cart' ) ) {
38 throw new Exception( 'A valid WC_Cart object is required' );
39 }
40
41 $this->cart = $cart;
42 }
43
44 /**
45 * Register methods for this object on the appropriate WordPress hooks.
46 */
47 public function init() {
48 add_action( 'wp_loaded', array( $this, 'get_cart_from_session' ) );
49 add_action( 'woocommerce_cart_emptied', array( $this, 'destroy_cart_session' ) );
50 add_action( 'woocommerce_after_calculate_totals', array( $this, 'set_session' ), 1000 );
51 add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'set_session' ) );
52 add_action( 'woocommerce_removed_coupon', array( $this, 'set_session' ) );
53
54 // Persistent cart stored to usermeta.
55 add_action( 'woocommerce_add_to_cart', array( $this, 'persistent_cart_update' ) );
56 add_action( 'woocommerce_cart_item_removed', array( $this, 'persistent_cart_update' ) );
57 add_action( 'woocommerce_cart_item_restored', array( $this, 'persistent_cart_update' ) );
58 add_action( 'woocommerce_cart_item_set_quantity', array( $this, 'persistent_cart_update' ) );
59
60 // Cookie events - cart cookies need to be set before headers are sent.
61 add_action( 'woocommerce_add_to_cart', array( $this, 'maybe_set_cart_cookies' ) );
62 add_action( 'wp', array( $this, 'maybe_set_cart_cookies' ), 99 );
63 add_action( 'shutdown', array( $this, 'maybe_set_cart_cookies' ), 0 );
64 }
65
66 /**
67 * Get the cart data from the PHP session and store it in class variables.
68 *
69 * @since 3.2.0
70 */
71 public function get_cart_from_session() {
72 do_action( 'woocommerce_load_cart_from_session' );
73 $this->cart->set_totals( WC()->session->get( 'cart_totals', null ) );
74 $this->cart->set_applied_coupons( WC()->session->get( 'applied_coupons', array() ) );
75 $this->cart->set_coupon_discount_totals( WC()->session->get( 'coupon_discount_totals', array() ) );
76 $this->cart->set_coupon_discount_tax_totals( WC()->session->get( 'coupon_discount_tax_totals', array() ) );
77 $this->cart->set_removed_cart_contents( WC()->session->get( 'removed_cart_contents', array() ) );
78
79 $update_cart_session = false; // Flag to indicate the stored cart should be updated.
80 $order_again = false; // Flag to indicate whether this is a re-order.
81 $cart = WC()->session->get( 'cart', null );
82 $merge_saved_cart = (bool) get_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login', true );
83
84 // Merge saved cart with current cart.
85 if ( is_null( $cart ) || $merge_saved_cart ) {
86 $saved_cart = $this->get_saved_cart();
87 $cart = is_null( $cart ) ? array() : $cart;
88 $cart = array_merge( $saved_cart, $cart );
89 $update_cart_session = true;
90
91 delete_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login' );
92 }
93
94 // Populate cart from order.
95 if ( isset( $_GET['order_again'], $_GET['_wpnonce'] ) && is_user_logged_in() && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), 'woocommerce-order_again' ) ) { // WPCS: input var ok, sanitization ok.
96 $cart = $this->populate_cart_from_order( absint( $_GET['order_again'] ), $cart ); // WPCS: input var ok.
97 $order_again = true;
98 }
99
100 // Prime caches to reduce future queries.
101 if ( is_callable( '_prime_post_caches' ) ) {
102 _prime_post_caches( wp_list_pluck( $cart, 'product_id' ) );
103 }
104
105 $cart_contents = array();
106
107 foreach ( $cart as $key => $values ) {
108 if ( ! is_customize_preview() && 'customize-preview' === $key ) {
109 continue;
110 }
111
112 $product = wc_get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] );
113
114 if ( empty( $product ) || ! $product->exists() || 0 >= $values['quantity'] ) {
115 continue;
116 }
117
118 /**
119 * Allow 3rd parties to validate this item before it's added to cart and add their own notices.
120 *
121 * @since 3.6.0
122 *
123 * @param bool $remove_cart_item_from_session If true, the item will not be added to the cart. Default: false.
124 * @param string $key Cart item key.
125 * @param array $values Cart item values e.g. quantity and product_id.
126 */
127 if ( apply_filters( 'woocommerce_pre_remove_cart_item_from_session', false, $key, $values ) ) {
128 $update_cart_session = true;
129 do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );
130
131 } elseif ( ! $product->is_purchasable() ) {
132 $update_cart_session = true;
133 /* translators: %s: product name */
134 $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() );
135 /**
136 * Filter message about item removed from the cart.
137 *
138 * @since 3.8.0
139 * @param string $message Message.
140 * @param WC_Product $product Product data.
141 */
142 $message = apply_filters( 'woocommerce_cart_item_removed_message', $message, $product );
143 wc_add_notice( $message, 'error' );
144 do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );
145
146 } elseif ( ! empty( $values['data_hash'] ) && ! hash_equals( $values['data_hash'], wc_get_cart_item_data_hash( $product ) ) ) { // phpcs:ignore PHPCompatibility.PHP.NewFunctions.hash_equalsFound
147 $update_cart_session = true;
148 /* translators: %1$s: product name. %2$s product permalink */
149 $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() );
150 $message = apply_filters( 'woocommerce_cart_item_removed_because_modified_message', $message, $product );
151 wc_add_notice( $message, 'notice' );
152 do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );
153
154 } else {
155 // Put session data into array. Run through filter so other plugins can load their own session data.
156 $session_data = array_merge(
157 $values,
158 array(
159 'data' => $product,
160 )
161 );
162
163 $cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $session_data, $values, $key );
164
165 // Add to cart right away so the product is visible in woocommerce_get_cart_item_from_session hook.
166 $this->cart->set_cart_contents( $cart_contents );
167 }
168 }
169
170 // If it's not empty, it's been already populated by the loop above.
171 if ( ! empty( $cart_contents ) ) {
172 $this->cart->set_cart_contents( apply_filters( 'woocommerce_cart_contents_changed', $cart_contents ) );
173 }
174
175 do_action( 'woocommerce_cart_loaded_from_session', $this->cart );
176
177 if ( $update_cart_session || is_null( WC()->session->get( 'cart_totals', null ) ) ) {
178 WC()->session->set( 'cart', $this->get_cart_for_session() );
179 $this->cart->calculate_totals();
180
181 if ( $merge_saved_cart ) {
182 $this->persistent_cart_update();
183 }
184 }
185
186 // If this is a re-order, redirect to the cart page to get rid of the `order_again` query string.
187 if ( $order_again ) {
188 wp_safe_redirect( wc_get_cart_url() );
189 exit;
190 }
191 }
192
193 /**
194 * Destroy cart session data.
195 *
196 * @since 3.2.0
197 */
198 public function destroy_cart_session() {
199 WC()->session->set( 'cart', null );
200 WC()->session->set( 'cart_totals', null );
201 WC()->session->set( 'applied_coupons', null );
202 WC()->session->set( 'coupon_discount_totals', null );
203 WC()->session->set( 'coupon_discount_tax_totals', null );
204 WC()->session->set( 'removed_cart_contents', null );
205 WC()->session->set( 'order_awaiting_payment', null );
206 }
207
208 /**
209 * Will set cart cookies if needed and when possible.
210 *
211 * @since 3.2.0
212 */
213 public function maybe_set_cart_cookies() {
214 if ( ! headers_sent() && did_action( 'wp_loaded' ) ) {
215 if ( ! $this->cart->is_empty() ) {
216 $this->set_cart_cookies( true );
217 } elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { // WPCS: input var ok.
218 $this->set_cart_cookies( false );
219 }
220 }
221 }
222
223 /**
224 * Sets the php session data for the cart and coupons.
225 */
226 public function set_session() {
227 WC()->session->set( 'cart', $this->get_cart_for_session() );
228 WC()->session->set( 'cart_totals', $this->cart->get_totals() );
229 WC()->session->set( 'applied_coupons', $this->cart->get_applied_coupons() );
230 WC()->session->set( 'coupon_discount_totals', $this->cart->get_coupon_discount_totals() );
231 WC()->session->set( 'coupon_discount_tax_totals', $this->cart->get_coupon_discount_tax_totals() );
232 WC()->session->set( 'removed_cart_contents', $this->cart->get_removed_cart_contents() );
233
234 do_action( 'woocommerce_cart_updated' );
235 }
236
237 /**
238 * Returns the contents of the cart in an array without the 'data' element.
239 *
240 * @return array contents of the cart
241 */
242 public function get_cart_for_session() {
243 $cart_session = array();
244
245 foreach ( $this->cart->get_cart() as $key => $values ) {
246 $cart_session[ $key ] = $values;
247 unset( $cart_session[ $key ]['data'] ); // Unset product object.
248 }
249
250 return $cart_session;
251 }
252
253 /**
254 * Save the persistent cart when the cart is updated.
255 */
256 public function persistent_cart_update() {
257 if ( get_current_user_id() && apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
258 update_user_meta(
259 get_current_user_id(),
260 '_woocommerce_persistent_cart_' . get_current_blog_id(),
261 array(
262 'cart' => $this->get_cart_for_session(),
263 )
264 );
265 }
266 }
267
268 /**
269 * Delete the persistent cart permanently.
270 */
271 public function persistent_cart_destroy() {
272 if ( get_current_user_id() && apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
273 delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id() );
274 }
275 }
276
277 /**
278 * Set cart hash cookie and items in cart if not already set.
279 *
280 * @param bool $set Should cookies be set (true) or unset.
281 */
282 private function set_cart_cookies( $set = true ) {
283 if ( $set ) {
284 $setcookies = array(
285 'woocommerce_items_in_cart' => '1',
286 'woocommerce_cart_hash' => WC()->cart->get_cart_hash(),
287 );
288 foreach ( $setcookies as $name => $value ) {
289 if ( ! isset( $_COOKIE[ $name ] ) || $_COOKIE[ $name ] !== $value ) {
290 wc_setcookie( $name, $value );
291 }
292 }
293 } else {
294 $unsetcookies = array(
295 'woocommerce_items_in_cart',
296 'woocommerce_cart_hash',
297 );
298 foreach ( $unsetcookies as $name ) {
299 if ( isset( $_COOKIE[ $name ] ) ) {
300 wc_setcookie( $name, 0, time() - HOUR_IN_SECONDS );
301 unset( $_COOKIE[ $name ] );
302 }
303 }
304 }
305
306 do_action( 'woocommerce_set_cart_cookies', $set );
307 }
308
309 /**
310 * Get the persistent cart from the database.
311 *
312 * @since 3.5.0
313 * @return array
314 */
315 private function get_saved_cart() {
316 $saved_cart = array();
317
318 if ( apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
319 $saved_cart_meta = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true );
320
321 if ( isset( $saved_cart_meta['cart'] ) ) {
322 $saved_cart = array_filter( (array) $saved_cart_meta['cart'] );
323 }
324 }
325
326 return $saved_cart;
327 }
328
329 /**
330 * Get a cart from an order, if user has permission.
331 *
332 * @since 3.5.0
333 *
334 * @param int $order_id Order ID to try to load.
335 * @param array $cart Current cart array.
336 *
337 * @return array
338 */
339 private function populate_cart_from_order( $order_id, $cart ) {
340 $order = wc_get_order( $order_id );
341
342 if ( ! $order->get_id() || ! $order->has_status( apply_filters( 'woocommerce_valid_order_statuses_for_order_again', array( 'completed' ) ) ) || ! current_user_can( 'order_again', $order->get_id() ) ) {
343 return;
344 }
345
346 if ( apply_filters( 'woocommerce_empty_cart_when_order_again', true ) ) {
347 $cart = array();
348 }
349
350 $inital_cart_size = count( $cart );
351 $order_items = $order->get_items();
352
353 foreach ( $order_items as $item ) {
354 $product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $item->get_product_id() );
355 $quantity = $item->get_quantity();
356 $variation_id = (int) $item->get_variation_id();
357 $variations = array();
358 $cart_item_data = apply_filters( 'woocommerce_order_again_cart_item_data', array(), $item, $order );
359 $product = $item->get_product();
360
361 if ( ! $product ) {
362 continue;
363 }
364
365 // Prevent reordering variable products if no selected variation.
366 if ( ! $variation_id && $product->is_type( 'variable' ) ) {
367 continue;
368 }
369
370 // Prevent reordering items specifically out of stock.
371 if ( ! $product->is_in_stock() ) {
372 continue;
373 }
374
375 foreach ( $item->get_meta_data() as $meta ) {
376 if ( taxonomy_is_product_attribute( $meta->key ) ) {
377 $term = get_term_by( 'slug', $meta->value, $meta->key );
378 $variations[ $meta->key ] = $term ? $term->name : $meta->value;
379 } elseif ( meta_is_product_attribute( $meta->key, $meta->value, $product_id ) ) {
380 $variations[ $meta->key ] = $meta->value;
381 }
382 }
383
384 if ( ! apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data ) ) {
385 continue;
386 }
387
388 // Add to cart directly.
389 $cart_id = WC()->cart->generate_cart_id( $product_id, $variation_id, $variations, $cart_item_data );
390 $product_data = wc_get_product( $variation_id ? $variation_id : $product_id );
391 $cart[ $cart_id ] = apply_filters(
392 'woocommerce_add_order_again_cart_item',
393 array_merge(
394 $cart_item_data,
395 array(
396 'key' => $cart_id,
397 'product_id' => $product_id,
398 'variation_id' => $variation_id,
399 'variation' => $variations,
400 'quantity' => $quantity,
401 'data' => $product_data,
402 'data_hash' => wc_get_cart_item_data_hash( $product_data ),
403 )
404 ),
405 $cart_id
406 );
407 }
408
409 do_action_ref_array( 'woocommerce_ordered_again', array( $order->get_id(), $order_items, &$cart ) );
410
411 $num_items_in_cart = count( $cart );
412 $num_items_in_original_order = count( $order_items );
413 $num_items_added = $num_items_in_cart - $inital_cart_size;
414
415 if ( $num_items_in_original_order > $num_items_added ) {
416 wc_add_notice(
417 sprintf(
418 /* translators: %d item count */
419 _n(
420 '%d item from your previous order is currently unavailable and could not be added to your cart.',
421 '%d items from your previous order are currently unavailable and could not be added to your cart.',
422 $num_items_in_original_order - $num_items_added,
423 'woocommerce'
424 ),
425 $num_items_in_original_order - $num_items_added
426 ),
427 'error'
428 );
429 }
430
431 if ( 0 < $num_items_added ) {
432 wc_add_notice( __( 'The cart has been filled with the items from your previous order.', 'woocommerce' ) );
433 }
434
435 return $cart;
436 }
437 }
438