|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
3
|
|
|
exit; |
|
4
|
|
|
} |
|
5
|
|
|
|
|
6
|
|
|
include_once( WC_ABSPATH . 'includes/legacy/class-wc-legacy-cart.php' ); |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Cart session class. |
|
10
|
|
|
* @version 2.7.0 |
|
11
|
|
|
* @package WooCommerce/Classes |
|
12
|
|
|
* @category Class |
|
13
|
|
|
* @author WooThemes |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class WC_Cart_Session extends WC_Legacy_Cart { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Cart items class. |
|
19
|
|
|
* @var WC_Cart_Items |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $items; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Cart coupons class. |
|
25
|
|
|
* @var WC_Cart_Coupons |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $coupons; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Cart fees class. |
|
31
|
|
|
* @var WC_Cart_Fees |
|
32
|
|
|
*/ |
|
33
|
|
|
public $fees; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct() { |
|
39
|
|
|
$this->items = new WC_Cart_Items; |
|
40
|
|
|
$this->coupons = new WC_Cart_Coupons; |
|
41
|
|
|
$this->fees = new WC_Cart_Fees; |
|
42
|
|
|
add_action( 'wp_loaded', array( $this, 'get_cart_from_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_cart_emptied', array( $this, 'destroy_cart_session' ) ); |
|
47
|
|
|
add_action( 'woocommerce_after_calculate_totals', array( $this, 'set_session' ) ); |
|
48
|
|
|
add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'set_session' ) ); |
|
49
|
|
|
add_action( 'woocommerce_removed_coupon', array( $this, 'set_session' ) ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Destroy cart session data. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function destroy_cart_session( $deprecated = true ) { |
|
|
|
|
|
|
56
|
|
|
WC()->session->set( 'cart', null ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Will set cart cookies if needed, once, during WP hook. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function maybe_set_cart_cookies() { |
|
63
|
|
|
if ( headers_sent() || ! did_action( 'wp_loaded' ) ) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
if ( ! $this->is_empty() ) { |
|
67
|
|
|
$this->set_cart_cookies( true ); |
|
68
|
|
|
} elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) { |
|
69
|
|
|
$this->set_cart_cookies( false ); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set cart hash cookie and items in cart. |
|
75
|
|
|
* |
|
76
|
|
|
* @access private |
|
77
|
|
|
* @param bool $set (default: true) |
|
78
|
|
|
*/ |
|
79
|
|
|
private function set_cart_cookies( $set = true ) { |
|
80
|
|
|
if ( $set ) { |
|
81
|
|
|
wc_setcookie( 'woocommerce_items_in_cart', 1 ); |
|
82
|
|
|
wc_setcookie( 'woocommerce_cart_hash', md5( json_encode( $this->get_cart_for_session() ) ) ); |
|
83
|
|
|
} else { |
|
84
|
|
|
wc_setcookie( 'woocommerce_items_in_cart', 0, time() - HOUR_IN_SECONDS ); |
|
85
|
|
|
wc_setcookie( 'woocommerce_cart_hash', '', time() - HOUR_IN_SECONDS ); |
|
86
|
|
|
} |
|
87
|
|
|
do_action( 'woocommerce_set_cart_cookies', $set ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns the contents of the cart in an array without the 'data' element. |
|
92
|
|
|
* |
|
93
|
|
|
* @return array contents of the cart |
|
94
|
|
|
*/ |
|
95
|
|
|
public function get_cart_for_session() { |
|
96
|
|
|
return wc_list_pluck( $this->items->get_items(), 'get_data' ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get the cart data from the PHP session and store it in class variables. |
|
101
|
|
|
*/ |
|
102
|
|
|
public function get_cart_from_session() { |
|
103
|
|
|
$cart = wp_parse_args( (array) WC()->session->get( 'cart', array() ), array( |
|
104
|
|
|
'items' => array(), |
|
105
|
|
|
'removed_items' => array(), |
|
106
|
|
|
'coupons' => array(), |
|
107
|
|
|
) ); |
|
108
|
|
|
|
|
109
|
|
|
foreach ( $cart['items'] as $key => $values ) { |
|
110
|
|
|
if ( ! isset( $values['product_id'], $values['quantity'] ) || ! ( $product = wc_get_product( $values['product_id'] ) ) ) { |
|
111
|
|
|
unset( $cart['items'][ $key ] ); |
|
112
|
|
|
continue; |
|
113
|
|
|
} |
|
114
|
|
|
// Put session data into array. Run through filter so other plugins can load their own session data. |
|
115
|
|
|
$cart['items'][ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $values, $values, $key ); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$this->items->set_items( $cart['items'] ); |
|
119
|
|
|
$this->items->set_removed_items( $cart['removed_items'] ); |
|
120
|
|
|
$this->coupons->set_coupons( $cart['coupons'] ); |
|
121
|
|
|
|
|
122
|
|
|
do_action( 'woocommerce_cart_loaded_from_session', $this ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Sets the php session data for the cart and coupons. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function set_session() { |
|
129
|
|
|
$session_data = array( |
|
130
|
|
|
'items' => $this->get_cart_for_session(), |
|
131
|
|
|
'removed_items' => wc_list_pluck( $this->items->get_removed_items(), 'get_data' ), |
|
132
|
|
|
'coupons' => $this->coupons->get_coupons(), |
|
133
|
|
|
); |
|
134
|
|
|
if ( WC()->session->set( 'cart', $session_data ) ) { |
|
135
|
|
|
do_action( 'woocommerce_cart_updated' ); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.