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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 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-session.php
567 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_cart_loaded_from_session', array( $this, 'set_session' ) );
78 add_action( 'woocommerce_removed_coupon', array( $this, 'set_session' ) );
79
80 // Persistent cart stored to usermeta.
81 add_action( 'woocommerce_add_to_cart', array( $this, 'persistent_cart_update' ) );
82 add_action( 'woocommerce_cart_item_removed', array( $this, 'persistent_cart_update' ) );
83 add_action( 'woocommerce_cart_item_restored', array( $this, 'persistent_cart_update' ) );
84 add_action( 'woocommerce_cart_item_set_quantity', array( $this, 'persistent_cart_update' ) );
85
86 // Cookie events - cart cookies need to be set before headers are sent.
87 add_action( 'woocommerce_add_to_cart', array( $this, 'maybe_set_cart_cookies' ) );
88 add_action( 'wp', array( $this, 'maybe_set_cart_cookies' ), 99 );
89 add_action( 'shutdown', array( $this, 'maybe_set_cart_cookies' ), 0 );
90 }
91
92 /**
93 * Get the cart data from the PHP session and store it in class variables.
94 *
95 * @since 3.2.0
96 */
97 public function get_cart_from_session() {
98 do_action( 'woocommerce_load_cart_from_session' );
99 $this->cart->set_totals( WC()->session->get( 'cart_totals', null ) );
100 $this->cart->set_applied_coupons( WC()->session->get( 'applied_coupons', array() ) );
101 $this->cart->set_coupon_discount_totals( WC()->session->get( 'coupon_discount_totals', array() ) );
102 $this->cart->set_coupon_discount_tax_totals( WC()->session->get( 'coupon_discount_tax_totals', array() ) );
103 $this->cart->set_removed_cart_contents( WC()->session->get( 'removed_cart_contents', array() ) );
104
105 $update_cart_session = false; // Flag to indicate the stored cart should be updated.
106 $order_again = false; // Flag to indicate whether this is a re-order.
107 $cart = WC()->session->get( 'cart', null );
108 $merge_saved_cart = (bool) get_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login', true );
109
110 // Merge saved cart with current cart.
111 if ( is_null( $cart ) || $merge_saved_cart ) {
112 $saved_cart = $this->get_saved_cart();
113 $cart = is_null( $cart ) ? array() : $cart;
114 $cart = array_merge( $saved_cart, $cart );
115 $update_cart_session = true;
116
117 delete_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login' );
118 }
119
120 // Populate cart from order.
121 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.
122 $cart = $this->populate_cart_from_order( absint( $_GET['order_again'] ), $cart ); // WPCS: input var ok.
123 $order_again = true;
124 $update_cart_session = true;
125 }
126
127 // Prime caches to reduce future queries.
128 if ( is_callable( '_prime_post_caches' ) ) {
129 _prime_post_caches( wp_list_pluck( $cart, 'product_id' ) );
130 }
131
132 $cart_contents = array();
133
134 foreach ( $cart as $key => $values ) {
135 if ( ! is_customize_preview() && 'customize-preview' === $key ) {
136 continue;
137 }
138
139 $product = wc_get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] );
140
141 if ( empty( $product ) || ! $product->exists() || 0 >= $values['quantity'] ) {
142 continue;
143 }
144
145 /**
146 * Allow 3rd parties to validate this item before it's added to cart and add their own notices.
147 *
148 * @since 3.6.0
149 *
150 * @param bool $remove_cart_item_from_session If true, the item will not be added to the cart. Default: false.
151 * @param string $key Cart item key.
152 * @param array $values Cart item values e.g. quantity and product_id.
153 * @param WC_Product $product The product being added to the cart.
154 */
155 if ( apply_filters( 'woocommerce_pre_remove_cart_item_from_session', false, $key, $values, $product ) ) {
156 $update_cart_session = true;
157 /**
158 * Fires when cart item is removed from the session.
159 *
160 * @since 3.6.0
161 *
162 * @param string $key Cart item key.
163 * @param array $values Cart item values e.g. quantity and product_id.
164 * @param WC_Product $product The product being added to the cart.
165 */
166 do_action( 'woocommerce_remove_cart_item_from_session', $key, $values, $product );
167
168 /**
169 * Allow 3rd parties to override this item's is_purchasable() result with cart item data.
170 *
171 * @param bool $is_purchasable If false, the item will not be added to the cart. Default: product's is_purchasable() status.
172 * @param string $key Cart item key.
173 * @param array $values Cart item values e.g. quantity and product_id.
174 * @param WC_Product $product The product being added to the cart.
175 *
176 * @since 7.0.0
177 */
178 } elseif ( ! apply_filters( 'woocommerce_cart_item_is_purchasable', $product->is_purchasable(), $key, $values, $product ) ) {
179 $update_cart_session = true;
180 /* translators: %s: product name */
181 $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() );
182 /**
183 * Filter message about item removed from the cart.
184 *
185 * @since 3.8.0
186 * @param string $message Message.
187 * @param WC_Product $product Product data.
188 */
189 $message = apply_filters( 'woocommerce_cart_item_removed_message', $message, $product );
190 wc_add_notice( $message, 'error' );
191 do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );
192
193 } elseif ( ! empty( $values['data_hash'] ) && ! hash_equals( $values['data_hash'], wc_get_cart_item_data_hash( $product ) ) ) { // phpcs:ignore PHPCompatibility.PHP.NewFunctions.hash_equalsFound
194 $update_cart_session = true;
195 /* translators: %1$s: product name. %2$s product permalink */
196 $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() );
197 $message = apply_filters( 'woocommerce_cart_item_removed_because_modified_message', $message, $product );
198 wc_add_notice( $message, 'notice' );
199 do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );
200
201 } else {
202 // Put session data into array. Run through filter so other plugins can load their own session data.
203 $session_data = array_merge(
204 $values,
205 array(
206 'data' => $product,
207 )
208 );
209
210 /**
211 * Filter to modify or add session data to the cart contents.
212 *
213 * @since 3.2.0
214 *
215 * @param array $session_data Data for an item in the cart.
216 * @param array $values Data for an item in the cart, without the product object.
217 * @param string $key The cart item hash.
218 */
219 $cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $session_data, $values, $key );
220 if ( ! isset( $cart_contents[ $key ]['data'] ) || ! $cart_contents[ $key ]['data'] instanceof WC_Product ) {
221 // If the cart contents is missing the product object after filtering, something is wrong.
222 wc_doing_it_wrong(
223 __METHOD__,
224 'When filtering cart items with woocommerce_get_cart_item_from_session, each item must have a data key containing a product object.',
225 '9.8.0'
226 );
227
228 // Add the product back in.
229 $cart_contents[ $key ]['data'] = $product;
230 }
231
232 // Add to cart right away so the product is visible in woocommerce_get_cart_item_from_session hook.
233 $this->cart->set_cart_contents( $cart_contents );
234 }
235 }
236
237 // If it's not empty, it's been already populated by the loop above.
238 if ( ! empty( $cart_contents ) ) {
239 $this->cart->set_cart_contents( apply_filters( 'woocommerce_cart_contents_changed', $cart_contents ) );
240 }
241
242 do_action( 'woocommerce_cart_loaded_from_session', $this->cart );
243
244 if ( $update_cart_session || is_null( WC()->session->get( 'cart_totals', null ) ) ) {
245 WC()->session->set( 'cart', $this->get_cart_for_session() );
246 $this->cart->calculate_totals();
247
248 if ( $merge_saved_cart ) {
249 $this->persistent_cart_update();
250 }
251 }
252
253 // If this is a re-order, redirect to the cart page to get rid of the `order_again` query string.
254 if ( $order_again ) {
255 wp_safe_redirect( wc_get_cart_url() );
256 exit;
257 }
258 }
259
260 /**
261 * Destroy cart session data.
262 *
263 * @since 3.2.0
264 */
265 public function destroy_cart_session() {
266 WC()->session->set( 'cart', null );
267 WC()->session->set( 'cart_totals', null );
268 WC()->session->set( 'applied_coupons', null );
269 WC()->session->set( 'coupon_discount_totals', null );
270 WC()->session->set( 'coupon_discount_tax_totals', null );
271 WC()->session->set( 'removed_cart_contents', null );
272 WC()->session->set( 'order_awaiting_payment', null );
273 }
274
275 /**
276 * Will set cart cookies if needed and when possible.
277 *
278 * Headers are only updated if headers have not yet been sent.
279 *
280 * @since 3.2.0
281 */
282 public function maybe_set_cart_cookies() {
283 if ( headers_sent() || ! did_action( 'wp_loaded' ) ) {
284 return;
285 }
286 if ( ! $this->cart->is_empty() ) {
287 $this->set_cart_cookies( true );
288 } elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { // WPCS: input var ok.
289 $this->set_cart_cookies( false );
290 }
291 $this->dedupe_cookies();
292 }
293
294 /**
295 * Remove duplicate cookies from the response.
296 */
297 private function dedupe_cookies() {
298 $all_cookies = array_filter(
299 headers_list(),
300 function( $header ) {
301 return stripos( $header, 'Set-Cookie:' ) !== false;
302 }
303 );
304 $final_cookies = array();
305 $update_cookies = false;
306 foreach ( $all_cookies as $cookie ) {
307
308 list(, $cookie_value) = explode( ':', $cookie, 2 );
309 list($cookie_name, $cookie_value) = explode( '=', trim( $cookie_value ), 2 );
310
311 if ( stripos( $cookie_name, 'woocommerce_' ) !== false ) {
312 $key = $this->find_cookie_by_name( $cookie_name, $final_cookies );
313 if ( false !== $key ) {
314 $update_cookies = true;
315 unset( $final_cookies[ $key ] );
316 }
317 }
318 $final_cookies[] = $cookie;
319 }
320
321 if ( $update_cookies ) {
322 header_remove( 'Set-Cookie' );
323 foreach ( $final_cookies as $cookie ) {
324 // Using header here preserves previous cookie args.
325 header( $cookie, false );
326 }
327 }
328 }
329
330 /**
331 * Find a cookie by name in an array of cookies.
332 *
333 * @param string $cookie_name Name of the cookie to find.
334 * @param array $cookies Array of cookies to search.
335 * @return mixed Key of the cookie if found, false if not.
336 */
337 private function find_cookie_by_name( $cookie_name, $cookies ) {
338 foreach ( $cookies as $key => $cookie ) {
339 if ( strpos( $cookie, $cookie_name ) !== false ) {
340 return $key;
341 }
342 }
343 return false;
344 }
345
346 /**
347 * Sets the php session data for the cart and coupons.
348 */
349 public function set_session() {
350 WC()->session->set( 'cart', $this->get_cart_for_session() );
351 WC()->session->set( 'cart_totals', $this->cart->get_totals() );
352 WC()->session->set( 'applied_coupons', $this->cart->get_applied_coupons() );
353 WC()->session->set( 'coupon_discount_totals', $this->cart->get_coupon_discount_totals() );
354 WC()->session->set( 'coupon_discount_tax_totals', $this->cart->get_coupon_discount_tax_totals() );
355 WC()->session->set( 'removed_cart_contents', $this->cart->get_removed_cart_contents() );
356
357 do_action( 'woocommerce_cart_updated' );
358 }
359
360 /**
361 * Returns the contents of the cart in an array without the 'data' element.
362 *
363 * @return array contents of the cart
364 */
365 public function get_cart_for_session() {
366 $cart_session = array();
367
368 foreach ( $this->cart->get_cart() as $key => $values ) {
369 $cart_session[ $key ] = $values;
370 unset( $cart_session[ $key ]['data'] ); // Unset product object.
371 }
372
373 return $cart_session;
374 }
375
376 /**
377 * Save the persistent cart when the cart is updated.
378 */
379 public function persistent_cart_update() {
380 if ( get_current_user_id() && apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
381 update_user_meta(
382 get_current_user_id(),
383 '_woocommerce_persistent_cart_' . get_current_blog_id(),
384 array(
385 'cart' => $this->get_cart_for_session(),
386 )
387 );
388 }
389 }
390
391 /**
392 * Delete the persistent cart permanently.
393 */
394 public function persistent_cart_destroy() {
395 if ( get_current_user_id() && apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
396 delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id() );
397 }
398 }
399
400 /**
401 * Set cart hash cookie and items in cart if not already set.
402 *
403 * @param bool $set Should cookies be set (true) or unset.
404 */
405 private function set_cart_cookies( $set = true ) {
406 if ( $set ) {
407 $setcookies = array(
408 'woocommerce_items_in_cart' => '1',
409 'woocommerce_cart_hash' => WC()->cart->get_cart_hash(),
410 );
411 foreach ( $setcookies as $name => $value ) {
412 if ( ! isset( $_COOKIE[ $name ] ) || $_COOKIE[ $name ] !== $value ) {
413 wc_setcookie( $name, $value );
414 $_COOKIE[ $name ] = $value;
415 }
416 }
417 } else {
418 $unsetcookies = array(
419 'woocommerce_items_in_cart',
420 'woocommerce_cart_hash',
421 );
422 foreach ( $unsetcookies as $name ) {
423 if ( isset( $_COOKIE[ $name ] ) ) {
424 wc_setcookie( $name, 0, time() - HOUR_IN_SECONDS );
425 unset( $_COOKIE[ $name ] );
426 }
427 }
428 }
429
430 do_action( 'woocommerce_set_cart_cookies', $set );
431 }
432
433 /**
434 * Get the persistent cart from the database.
435 *
436 * @since 3.5.0
437 * @return array
438 */
439 private function get_saved_cart() {
440 $saved_cart = array();
441
442 if ( apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
443 $saved_cart_meta = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true );
444
445 if ( isset( $saved_cart_meta['cart'] ) ) {
446 $saved_cart = array_filter( (array) $saved_cart_meta['cart'] );
447 }
448 }
449
450 return $saved_cart;
451 }
452
453 /**
454 * Get a cart from an order, if user has permission.
455 *
456 * @since 3.5.0
457 *
458 * @param int $order_id Order ID to try to load.
459 * @param array $cart Current cart array.
460 *
461 * @return array
462 */
463 private function populate_cart_from_order( $order_id, $cart ) {
464 $order = wc_get_order( $order_id );
465
466 /**
467 * Filter the valid order statuses for reordering.
468 *
469 * @since 3.6.0
470 *
471 * @param array $valid_statuses Array of valid order statuses.
472 */
473 $valid_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_order_again', array( OrderStatus::COMPLETED ) );
474 if ( ! $order->get_id() || ! $order->has_status( $valid_statuses ) || ! current_user_can( 'order_again', $order->get_id() ) ) {
475 return;
476 }
477
478 if ( apply_filters( 'woocommerce_empty_cart_when_order_again', true ) ) {
479 $cart = array();
480 }
481
482 $inital_cart_size = count( $cart );
483 $order_items = $order->get_items();
484
485 foreach ( $order_items as $item ) {
486 $product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $item->get_product_id() );
487 $quantity = $item->get_quantity();
488 $variation_id = (int) $item->get_variation_id();
489 $variations = array();
490 $cart_item_data = apply_filters( 'woocommerce_order_again_cart_item_data', array(), $item, $order );
491 $product = $item->get_product();
492
493 if ( ! $product ) {
494 continue;
495 }
496
497 // Prevent reordering variable products if no selected variation.
498 if ( ! $variation_id && $product->is_type( ProductType::VARIABLE ) ) {
499 continue;
500 }
501
502 // Prevent reordering items specifically out of stock.
503 if ( ! $product->is_in_stock() ) {
504 continue;
505 }
506
507 foreach ( $item->get_meta_data() as $meta ) {
508 if ( taxonomy_is_product_attribute( $meta->key ) || meta_is_product_attribute( $meta->key, $meta->value, $product_id ) ) {
509 $variations[ $meta->key ] = $meta->value;
510 }
511 }
512
513 if ( ! apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data ) ) {
514 continue;
515 }
516
517 // Add to cart directly.
518 $cart_id = WC()->cart->generate_cart_id( $product_id, $variation_id, $variations, $cart_item_data );
519 $product_data = wc_get_product( $variation_id ? $variation_id : $product_id );
520 $cart[ $cart_id ] = apply_filters(
521 'woocommerce_add_order_again_cart_item',
522 array_merge(
523 $cart_item_data,
524 array(
525 'key' => $cart_id,
526 'product_id' => $product_id,
527 'variation_id' => $variation_id,
528 'variation' => $variations,
529 'quantity' => $quantity,
530 'data' => $product_data,
531 'data_hash' => wc_get_cart_item_data_hash( $product_data ),
532 )
533 ),
534 $cart_id
535 );
536 }
537
538 do_action_ref_array( 'woocommerce_ordered_again', array( $order->get_id(), $order_items, &$cart ) );
539
540 $num_items_in_cart = count( $cart );
541 $num_items_in_original_order = count( $order_items );
542 $num_items_added = $num_items_in_cart - $inital_cart_size;
543
544 if ( $num_items_in_original_order > $num_items_added ) {
545 wc_add_notice(
546 sprintf(
547 /* translators: %d item count */
548 _n(
549 '%d item from your previous order is currently unavailable and could not be added to your cart.',
550 '%d items from your previous order are currently unavailable and could not be added to your cart.',
551 $num_items_in_original_order - $num_items_added,
552 'woocommerce'
553 ),
554 $num_items_in_original_order - $num_items_added
555 ),
556 'error'
557 );
558 }
559
560 if ( 0 < $num_items_added ) {
561 wc_add_notice( __( 'The cart has been filled with the items from your previous order.', 'woocommerce' ) );
562 }
563
564 return $cart;
565 }
566 }
567