|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Cart module for Yii2 |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/hiqdev/yii2-cart |
|
7
|
|
|
* @package yii2-cart |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hiqdev\yii2\cart; |
|
13
|
|
|
|
|
14
|
|
|
use hipanel\modules\finance\cart\AbstractCartPosition; |
|
15
|
|
|
use Yii; |
|
16
|
|
|
use yz\shoppingcart\CartActionEvent; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class ShoppingCart. |
|
20
|
|
|
* @property AbstractCartPosition[] $positions |
|
21
|
|
|
*/ |
|
22
|
|
|
class ShoppingCart extends \yz\shoppingcart\ShoppingCart |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var AbstractCartPosition[] |
|
26
|
|
|
* TODO make local AbstractCartPosition |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $_positions = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The cart module. |
|
32
|
|
|
*/ |
|
33
|
|
|
public $module; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return integer |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getCount() |
|
39
|
1 |
|
{ |
|
40
|
|
|
return count($this->_positions); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getQuantity() |
|
44
|
1 |
|
{ |
|
45
|
|
|
$count = 0; |
|
46
|
1 |
|
foreach ($this->_positions as $position) { |
|
47
|
1 |
|
$count += $position->getQuantity(); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $count; |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getSubtotal() |
|
54
|
2 |
|
{ |
|
55
|
|
|
return $this->getCost(false); |
|
56
|
2 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getTotal() |
|
59
|
2 |
|
{ |
|
60
|
|
|
return $this->getCost(true); |
|
61
|
2 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getDiscount() |
|
64
|
1 |
|
{ |
|
65
|
|
|
return $this->getTotal() - $this->getSubtotal(); |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function formatCurrency($sum, $currency = null) |
|
69
|
1 |
|
{ |
|
70
|
|
|
return $sum !== null ? Yii::$app->formatter->format($sum, ['currency', $currency ?? $this->getCurrency()]) : '--'; |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Sets cart from serialized string |
|
75
|
|
|
* @param string $serialized |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setSerialized($serialized) |
|
78
|
|
|
{ |
|
79
|
|
|
try { |
|
80
|
|
|
parent::setSerialized($serialized); |
|
81
|
|
|
} catch (\Exception $e) { |
|
82
|
|
|
Yii::error('Failed to unserlialize cart: ' . $e->getMessage(), __METHOD__); |
|
83
|
|
|
$this->_positions = []; |
|
84
|
|
|
$this->saveToSession(); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Checks whether any of cart positions has error in `id` attribute. |
|
90
|
|
|
* @return boolean |
|
91
|
|
|
*/ |
|
92
|
|
|
public function hasErrors() |
|
93
|
|
|
{ |
|
94
|
|
|
foreach ($this->_positions as $position) { |
|
95
|
|
|
if ($position->hasErrors('id')) { |
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param CartPositionInterface[] $positions |
|
105
|
|
|
*/ |
|
106
|
|
|
public function putPositions($positions) |
|
107
|
|
|
{ |
|
108
|
|
|
foreach ($positions as $position) { |
|
109
|
|
|
if (isset($this->_positions[$position->getId()])) { |
|
110
|
|
|
if ($position instanceof DontIncrementQuantityWhenAlreadyInCart) { |
|
111
|
|
|
continue; |
|
112
|
|
|
} |
|
113
|
|
|
$existingPosition = $this->_positions[$position->getId()]; |
|
114
|
|
|
$existingPosition->setQuantity($existingPosition->getQuantity() + 1); |
|
115
|
|
|
} else { |
|
116
|
|
|
if ($position->getQuantity() <= 0) { |
|
117
|
|
|
$position->setQuantity(1); |
|
118
|
|
|
} |
|
119
|
|
|
$this->_positions[$position->getId()] = $position; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([ |
|
124
|
|
|
'action' => CartActionEvent::ACTION_POSITION_PUT, |
|
125
|
|
|
])); |
|
126
|
|
|
if ($this->storeInSession) |
|
127
|
|
|
$this->saveToSession(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getCurrency(): ?string |
|
131
|
|
|
{ |
|
132
|
|
|
if (!empty($this->_positions)) { |
|
133
|
|
|
return reset($this->_positions)->currency; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return Yii::$app->params['currency']; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getAdditionalLinks(): array |
|
143
|
|
|
{ |
|
144
|
|
|
$links = []; |
|
145
|
|
|
$positions = $this->_positions; |
|
146
|
|
|
if (empty($positions)) { |
|
147
|
|
|
return $links; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
foreach ($positions as $position) { |
|
151
|
|
|
$additionalLinks = $position->getAdditionalLinks(); |
|
152
|
|
|
if (!empty($additionalLinks)) { |
|
153
|
|
|
foreach ($additionalLinks as $url => $label) { |
|
154
|
|
|
if ($url && $label && !isset($links[$url])) { |
|
155
|
|
|
$links[$url] = $label; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $links; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|