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