|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Les fonctions principales de la session du panier. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Eoxia <[email protected]> |
|
6
|
|
|
* @copyright (c) 2011-2018 Eoxia <[email protected]>. |
|
7
|
|
|
* |
|
8
|
|
|
* @license AGPLv3 <https://spdx.org/licenses/AGPL-3.0-or-later.html> |
|
9
|
|
|
* |
|
10
|
|
|
* @package WPshop\Classes |
|
11
|
|
|
* |
|
12
|
|
|
* @since 2.0.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace wpshop; |
|
16
|
|
|
|
|
17
|
|
|
defined( 'ABSPATH' ) || exit; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Cart Session Class. |
|
21
|
|
|
*/ |
|
22
|
|
|
class Cart_Session extends \eoxia\Singleton_Util { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Les données externe. |
|
26
|
|
|
* |
|
27
|
|
|
* @since 2.0.0 |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
public $external_data = array(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Un tableau de produit |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
public $cart_contents = array(); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Le prix total HT. |
|
42
|
|
|
* |
|
43
|
|
|
* @since 2.0.0 |
|
44
|
|
|
* |
|
45
|
|
|
* @var float |
|
46
|
|
|
*/ |
|
47
|
|
|
public $total_price; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Le prix total HT sans frais de port. |
|
51
|
|
|
* |
|
52
|
|
|
* @since 2.0.0 |
|
53
|
|
|
* |
|
54
|
|
|
* @var float |
|
55
|
|
|
*/ |
|
56
|
|
|
public $total_price_no_shipping; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Le prix total TTC. |
|
60
|
|
|
* |
|
61
|
|
|
* @since 2.0.0 |
|
62
|
|
|
* |
|
63
|
|
|
* @var float |
|
64
|
|
|
*/ |
|
65
|
|
|
public $total_price_ttc; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* L'ID du devis. |
|
69
|
|
|
* |
|
70
|
|
|
* @since 2.0.0 |
|
71
|
|
|
* |
|
72
|
|
|
* @var integer |
|
73
|
|
|
*/ |
|
74
|
|
|
public $proposal_id; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* L'ID de la commande |
|
78
|
|
|
* |
|
79
|
|
|
* @since 2.0.0 |
|
80
|
|
|
* |
|
81
|
|
|
* @var integer |
|
82
|
|
|
*/ |
|
83
|
|
|
public $order_id; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Le constructeur. |
|
87
|
|
|
* |
|
88
|
|
|
* @since 2.0.0 |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function construct() { |
|
91
|
|
|
$this->cart_contents = isset( $_SESSION['wps_cart'] ) ? $_SESSION['wps_cart'] : array(); |
|
|
|
|
|
|
92
|
|
|
$this->total_price = isset( $_SESSION['wps_total_price'] ) ? $_SESSION['wps_total_price'] : null; |
|
|
|
|
|
|
93
|
|
|
$this->total_price_no_shipping = isset( $_SESSION['wps_total_price_no_shipping'] ) ? $_SESSION['wps_total_price_no_shipping'] : null; |
|
|
|
|
|
|
94
|
|
|
$this->total_price_ttc = isset( $_SESSION['wps_total_price_ttc'] ) ? $_SESSION['wps_total_price_ttc'] : null; |
|
|
|
|
|
|
95
|
|
|
$this->proposal_id = isset( $_SESSION['wps_proposal_id'] ) ? $_SESSION['wps_proposal_id'] : null; |
|
|
|
|
|
|
96
|
|
|
$this->order_id = isset( $_SESSION['wps_order_id'] ) ? $_SESSION['wps_order_id'] : null; |
|
|
|
|
|
|
97
|
|
|
$this->external_data = isset( $_SESSION['wps_external_data'] ) ? $_SESSION['wps_external_data'] : array(); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Ajoutes une donnée external |
|
102
|
|
|
* |
|
103
|
|
|
* @since 2.0.0 |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $property Le nom. |
|
106
|
|
|
* @param mixed $value La valeur. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function add_external_data( $property, $value ) { |
|
109
|
|
|
$this->external_data[ $property ] = $value; |
|
110
|
|
|
$this->update_session(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Met à jour la SESSION |
|
115
|
|
|
* |
|
116
|
|
|
* @since 2.0.0 |
|
117
|
|
|
*/ |
|
118
|
|
|
public function update_session() { |
|
119
|
|
|
$_SESSION['wps_cart'] = $this->cart_contents; |
|
|
|
|
|
|
120
|
|
|
$_SESSION['wps_total_price'] = $this->total_price; |
|
|
|
|
|
|
121
|
|
|
$_SESSION['wps_total_price_no_shipping'] = $this->total_price_no_shipping; |
|
|
|
|
|
|
122
|
|
|
$_SESSION['wps_total_price_ttc'] = $this->total_price_ttc; |
|
|
|
|
|
|
123
|
|
|
$_SESSION['wps_proposal_id'] = $this->proposal_id; |
|
|
|
|
|
|
124
|
|
|
$_SESSION['wps_order_id'] = $this->order_id; |
|
|
|
|
|
|
125
|
|
|
$_SESSION['wps_external_data'] = $this->external_data; |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Supprimes toutes les données de la SESSION |
|
130
|
|
|
* |
|
131
|
|
|
* @since 2.0.0 |
|
132
|
|
|
*/ |
|
133
|
|
|
public function destroy() { |
|
134
|
|
|
unset( $_SESSION['wps_cart'] ); |
|
|
|
|
|
|
135
|
|
|
unset( $_SESSION['wps_total_price'] ); |
|
|
|
|
|
|
136
|
|
|
unset( $_SESSION['wps_total_price_no_shipping'] ); |
|
|
|
|
|
|
137
|
|
|
unset( $_SESSION['wps_total_price_ttc'] ); |
|
|
|
|
|
|
138
|
|
|
unset( $_SESSION['wps_proposal_id'] ); |
|
|
|
|
|
|
139
|
|
|
unset( $_SESSION['wps_order_id'] ); |
|
|
|
|
|
|
140
|
|
|
unset( $_SESSION['wps_external_data'] ); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Ajoutes un produit dans le panier. |
|
145
|
|
|
* |
|
146
|
|
|
* @since 2.0.0 |
|
147
|
|
|
* |
|
148
|
|
|
* @param Product $data Les données du produit. |
|
149
|
|
|
*/ |
|
150
|
|
|
public function add_product( $data ) { |
|
151
|
|
|
$this->cart_contents[] = $data; |
|
152
|
|
|
$this->update_session(); |
|
153
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Met à jour un produit dans le panier. |
|
158
|
|
|
* |
|
159
|
|
|
* @since 2.0.0 |
|
160
|
|
|
* |
|
161
|
|
|
* @param integer $index L'index du produit dans le tableau cart_contents. |
|
162
|
|
|
* @param Product $data Les données du produit. |
|
163
|
|
|
*/ |
|
164
|
|
|
public function update_product( $index, $data ) { |
|
165
|
|
|
$this->cart_contents[ $index ] = $data; |
|
166
|
|
|
$this->update_session(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Met à jour une propriété et met à jour la SESSION. |
|
171
|
|
|
* |
|
172
|
|
|
* @since 2.0.0 |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $property Le nom de la propriété. |
|
175
|
|
|
* @param mixed $value La valeur de la propriété. |
|
176
|
|
|
*/ |
|
177
|
|
|
public function update( $property, $value ) { |
|
178
|
|
|
$this->$property = $value; |
|
179
|
|
|
$this->update_session(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Recherches le produit dans le panier correspondant à l'ID. |
|
184
|
|
|
* |
|
185
|
|
|
* @since 2.0.0 |
|
186
|
|
|
* |
|
187
|
|
|
* @param integer $id L'id du produit. |
|
188
|
|
|
* |
|
189
|
|
|
* @return boolean True si trouvé. Sinon false. |
|
190
|
|
|
*/ |
|
191
|
|
|
public function has_product( $id ) { |
|
192
|
|
|
if ( ! empty( $this->cart_contents ) ) { |
|
193
|
|
|
foreach ( $this->cart_contents as $cart_content ) { |
|
194
|
|
|
if ( $cart_content['id'] === $id ) { |
|
195
|
|
|
return true; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return false; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Supprimes un produit du panier selon son $id. |
|
205
|
|
|
* |
|
206
|
|
|
* @since 2.0.0 |
|
207
|
|
|
* |
|
208
|
|
|
* @param integer $id L'ID du produit. |
|
209
|
|
|
*/ |
|
210
|
|
|
public function remove_product( $id ) { |
|
211
|
|
|
if ( ! empty( $this->cart_contents ) ) { |
|
212
|
|
|
foreach ( $this->cart_contents as $key => $cart_content ) { |
|
213
|
|
|
if ( $cart_content['id'] === $id ) { |
|
214
|
|
|
array_splice( $this->cart_contents, $key, 1 ); |
|
215
|
|
|
break; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$this->update_session(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Supprimes un produit depuis $key qui est son index dans le tableau |
|
225
|
|
|
* cart_contents. |
|
226
|
|
|
* |
|
227
|
|
|
* @since 2.0.0 |
|
228
|
|
|
* |
|
229
|
|
|
* @param integer $key L'index dans le tableau cart_contents. |
|
230
|
|
|
*/ |
|
231
|
|
|
public function remove_product_by_key( $key ) { |
|
232
|
|
|
array_splice( $this->cart_contents, $key, 1 ); |
|
233
|
|
|
|
|
234
|
|
|
$this->update_session(); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
Cart_Session::g(); |
|
239
|
|
|
|