class-wc-cart-session.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * Cart session handling class.
  4. *
  5. * @package WooCommerce/Classes
  6. * @version 3.2.0
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. /**
  12. * WC_Cart_Session class.
  13. *
  14. * @since 3.2.0
  15. */
  16. final class WC_Cart_Session {
  17. /**
  18. * Reference to cart object.
  19. *
  20. * @since 3.2.0
  21. * @var WC_Cart
  22. */
  23. protected $cart;
  24. /**
  25. * Sets up the items provided, and calculate totals.
  26. *
  27. * @since 3.2.0
  28. * @throws Exception If missing WC_Cart object.
  29. * @param WC_Cart $cart Cart object to calculate totals for.
  30. */
  31. public function __construct( &$cart ) {
  32. if ( ! is_a( $cart, 'WC_Cart' ) ) {
  33. throw new Exception( 'A valid WC_Cart object is required' );
  34. }
  35. $this->cart = $cart;
  36. }
  37. /**
  38. * Register methods for this object on the appropriate WordPress hooks.
  39. */
  40. public function init() {
  41. add_action( 'wp_loaded', array( $this, 'get_cart_from_session' ) );
  42. add_action( 'woocommerce_cart_emptied', array( $this, 'destroy_cart_session' ) );
  43. add_action( 'wp', array( $this, 'maybe_set_cart_cookies' ), 99 );
  44. add_action( 'shutdown', array( $this, 'maybe_set_cart_cookies' ), 0 );
  45. add_action( 'woocommerce_add_to_cart', array( $this, 'maybe_set_cart_cookies' ) );
  46. add_action( 'woocommerce_after_calculate_totals', array( $this, 'set_session' ) );
  47. add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'set_session' ) );
  48. add_action( 'woocommerce_removed_coupon', array( $this, 'set_session' ) );
  49. add_action( 'woocommerce_cart_updated', array( $this, 'persistent_cart_update' ) );
  50. }
  51. /**
  52. * Get the cart data from the PHP session and store it in class variables.
  53. *
  54. * @since 3.2.0
  55. */
  56. public function get_cart_from_session() {
  57. // Flag to indicate the stored cart should be updated.
  58. $update_cart_session = false;
  59. $totals = WC()->session->get( 'cart_totals', null );
  60. $cart = WC()->session->get( 'cart', null );
  61. $this->cart->set_totals( $totals );
  62. $this->cart->set_applied_coupons( WC()->session->get( 'applied_coupons', array() ) );
  63. $this->cart->set_coupon_discount_totals( WC()->session->get( 'coupon_discount_totals', array() ) );
  64. $this->cart->set_coupon_discount_tax_totals( WC()->session->get( 'coupon_discount_tax_totals', array() ) );
  65. $this->cart->set_removed_cart_contents( WC()->session->get( 'removed_cart_contents', array() ) );
  66. if ( apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
  67. $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true );
  68. } else {
  69. $saved_cart = false;
  70. }
  71. if ( is_null( $cart ) && $saved_cart ) {
  72. $cart = $saved_cart['cart'];
  73. $update_cart_session = true;
  74. } elseif ( is_null( $cart ) ) {
  75. $cart = array();
  76. } elseif ( is_array( $cart ) && $saved_cart ) {
  77. $cart = array_merge( $saved_cart['cart'], $cart );
  78. $update_cart_session = true;
  79. }
  80. if ( is_array( $cart ) ) {
  81. $cart_contents = array();
  82. // Prime caches to reduce future queries.
  83. if ( is_callable( '_prime_post_caches' ) ) {
  84. _prime_post_caches( wp_list_pluck( $cart, 'product_id' ) );
  85. }
  86. foreach ( $cart as $key => $values ) {
  87. $product = wc_get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] );
  88. if ( ! is_customize_preview() && 'customize-preview' === $key ) {
  89. continue;
  90. }
  91. if ( ! empty( $product ) && $product->exists() && $values['quantity'] > 0 ) {
  92. if ( ! $product->is_purchasable() ) {
  93. $update_cart_session = true;
  94. /* translators: %s: product name */
  95. wc_add_notice( 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() ), 'error' );
  96. do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );
  97. } elseif ( ! empty( $values['data_hash'] ) && ! hash_equals( $values['data_hash'], wc_get_cart_item_data_hash( $product ) ) ) { // phpcs:ignore PHPCompatibility.PHP.NewFunctions.hash_equalsFound
  98. $update_cart_session = true;
  99. /* translators: %1$s: product name. %2$s product permalink */
  100. wc_add_notice( 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() ), 'notice' );
  101. do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );
  102. } else {
  103. // Put session data into array. Run through filter so other plugins can load their own session data.
  104. $session_data = array_merge(
  105. $values, array(
  106. 'data' => $product,
  107. )
  108. );
  109. $cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $session_data, $values, $key );
  110. // Add to cart right away so the product is visible in woocommerce_get_cart_item_from_session hook.
  111. $this->cart->set_cart_contents( $cart_contents );
  112. }
  113. }
  114. }
  115. }
  116. do_action( 'woocommerce_cart_loaded_from_session', $this->cart );
  117. if ( $update_cart_session || is_null( WC()->session->get( 'cart_totals', null ) ) ) {
  118. WC()->session->set( 'cart', $this->get_cart_for_session() );
  119. $this->cart->calculate_totals();
  120. }
  121. }
  122. /**
  123. * Destroy cart session data.
  124. *
  125. * @since 3.2.0
  126. */
  127. public function destroy_cart_session() {
  128. WC()->session->set( 'cart', null );
  129. WC()->session->set( 'cart_totals', null );
  130. WC()->session->set( 'applied_coupons', null );
  131. WC()->session->set( 'coupon_discount_totals', null );
  132. WC()->session->set( 'coupon_discount_tax_totals', null );
  133. WC()->session->set( 'removed_cart_contents', null );
  134. WC()->session->set( 'order_awaiting_payment', null );
  135. }
  136. /**
  137. * Will set cart cookies if needed and when possible.
  138. *
  139. * @since 3.2.0
  140. */
  141. public function maybe_set_cart_cookies() {
  142. if ( ! headers_sent() && did_action( 'wp_loaded' ) ) {
  143. if ( ! $this->cart->is_empty() ) {
  144. $this->set_cart_cookies( true );
  145. } elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { // WPCS: input var ok.
  146. $this->set_cart_cookies( false );
  147. }
  148. }
  149. }
  150. /**
  151. * Sets the php session data for the cart and coupons.
  152. */
  153. public function set_session() {
  154. WC()->session->set( 'cart', $this->get_cart_for_session() );
  155. WC()->session->set( 'cart_totals', $this->cart->get_totals() );
  156. WC()->session->set( 'applied_coupons', $this->cart->get_applied_coupons() );
  157. WC()->session->set( 'coupon_discount_totals', $this->cart->get_coupon_discount_totals() );
  158. WC()->session->set( 'coupon_discount_tax_totals', $this->cart->get_coupon_discount_tax_totals() );
  159. WC()->session->set( 'removed_cart_contents', $this->cart->get_removed_cart_contents() );
  160. do_action( 'woocommerce_cart_updated' );
  161. }
  162. /**
  163. * Returns the contents of the cart in an array without the 'data' element.
  164. *
  165. * @return array contents of the cart
  166. */
  167. public function get_cart_for_session() {
  168. $cart_session = array();
  169. foreach ( $this->cart->get_cart() as $key => $values ) {
  170. $cart_session[ $key ] = $values;
  171. unset( $cart_session[ $key ]['data'] ); // Unset product object.
  172. }
  173. return $cart_session;
  174. }
  175. /**
  176. * Save the persistent cart when the cart is updated.
  177. */
  178. public function persistent_cart_update() {
  179. if ( get_current_user_id() && apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
  180. update_user_meta(
  181. get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), array(
  182. 'cart' => $this->get_cart_for_session(),
  183. )
  184. );
  185. }
  186. }
  187. /**
  188. * Delete the persistent cart permanently.
  189. */
  190. public function persistent_cart_destroy() {
  191. if ( get_current_user_id() && apply_filters( 'woocommerce_persistent_cart_enabled', true ) ) {
  192. delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id() );
  193. }
  194. }
  195. /**
  196. * Set cart hash cookie and items in cart.
  197. *
  198. * @access private
  199. * @param bool $set Should cookies be set (true) or unset.
  200. */
  201. private function set_cart_cookies( $set = true ) {
  202. if ( $set ) {
  203. wc_setcookie( 'woocommerce_items_in_cart', 1 );
  204. wc_setcookie( 'woocommerce_cart_hash', md5( wp_json_encode( $this->get_cart_for_session() ) ) );
  205. } elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { // WPCS: input var ok.
  206. wc_setcookie( 'woocommerce_items_in_cart', 0, time() - HOUR_IN_SECONDS );
  207. wc_setcookie( 'woocommerce_cart_hash', '', time() - HOUR_IN_SECONDS );
  208. }
  209. do_action( 'woocommerce_set_cart_cookies', $set );
  210. }
  211. }