1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Les fonctions principales 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 Class. |
21
|
|
|
*/ |
22
|
|
|
class Cart extends \eoxia\Singleton_Util { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructeur pour la classe Cart. Charge les options et les actions. |
26
|
|
|
* |
27
|
|
|
* @since 2.0.0 |
28
|
|
|
*/ |
29
|
|
|
protected function construct() {} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Ajout d'un produit dans le panier |
33
|
|
|
* |
34
|
|
|
* @since 2.0.0 |
35
|
|
|
* |
36
|
|
|
* @param Product_Model $product Les données du produit. |
37
|
|
|
*/ |
38
|
|
|
public function add_to_cart( $product ) { |
39
|
|
|
$data = array_merge( |
40
|
|
|
array( 'qty' => 1 ), |
41
|
|
|
$product->data |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$index = -1; |
45
|
|
|
|
46
|
|
View Code Duplication |
if ( ! empty( Cart_Session::g()->cart_contents ) ) { |
|
|
|
|
47
|
|
|
foreach ( Cart_Session::g()->cart_contents as $key => $line ) { |
48
|
|
|
if ( $line['id'] == $product->data['id'] ) { |
49
|
|
|
$data['qty'] = $line['qty'] + 1; |
50
|
|
|
$index = $key; |
51
|
|
|
break; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ( -1 === $index ) { |
57
|
|
|
Cart_Session::g()->add_product( $data ); |
58
|
|
|
} else { |
59
|
|
|
Cart_Session::g()->update_product( $index, $data ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
do_action( 'wps_add_to_cart' ); |
63
|
|
|
do_action( 'wps_before_calculate_totals' ); |
64
|
|
|
do_action( 'wps_calculate_totals' ); |
65
|
|
|
do_action( 'wps_after_calculate_totals' ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Met à jour le contenu du panier |
70
|
|
|
* |
71
|
|
|
* @since 2.0.0 |
72
|
|
|
* |
73
|
|
|
* @param Product_Model $product Les données du produit. |
74
|
|
|
*/ |
75
|
|
|
public function update_cart( $product ) { |
76
|
|
View Code Duplication |
if ( ! empty( Cart_Session::g()->cart_contents ) ) { |
|
|
|
|
77
|
|
|
foreach ( Cart_Session::g()->cart_contents as $key => $line ) { |
78
|
|
|
if ( $line['id'] == $product['id'] ) { |
79
|
|
|
$line['qty'] = $product['qty']; |
80
|
|
|
Cart_Session::g()->update_product( $key, $line ); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
do_action( 'wps_update_cart' ); |
86
|
|
|
do_action( 'wps_before_calculate_totals' ); |
87
|
|
|
do_action( 'wps_calculate_totals' ); |
88
|
|
|
do_action( 'wps_after_calculate_totals' ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Supprimes un produit du panier. |
93
|
|
|
* |
94
|
|
|
* @since 2.0.0 |
95
|
|
|
* |
96
|
|
|
* @param integer $key La clé du produit dans le tableau. |
97
|
|
|
*/ |
98
|
|
|
public function delete_product( $key ) { |
99
|
|
|
Cart_Session::g()->remove_product_by_key( $key ); |
100
|
|
|
|
101
|
|
|
do_action( 'wps_delete_to_cart', $key ); |
102
|
|
|
do_action( 'wps_before_calculate_totals' ); |
103
|
|
|
do_action( 'wps_calculate_totals' ); |
104
|
|
|
do_action( 'wps_after_calculate_totals' ); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
Cart::g(); |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.