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
|
|
|
public $currency = 'usd'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
1 |
|
* @return integer |
39
|
|
|
*/ |
40
|
1 |
|
public function getCount() |
41
|
|
|
{ |
42
|
|
|
return count($this->_positions); |
43
|
1 |
|
} |
44
|
|
|
|
45
|
1 |
|
public function getQuantity() |
46
|
1 |
|
{ |
47
|
1 |
|
$count = 0; |
48
|
1 |
|
foreach ($this->_positions as $position) { |
49
|
|
|
$count += $position->getQuantity(); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
return $count; |
53
|
2 |
|
} |
54
|
|
|
|
55
|
2 |
|
public function getSubtotal() |
56
|
|
|
{ |
57
|
|
|
return $this->getCost(false); |
58
|
2 |
|
} |
59
|
|
|
|
60
|
2 |
|
public function getTotal() |
61
|
|
|
{ |
62
|
|
|
return $this->getCost(true); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
public function getDiscount() |
66
|
|
|
{ |
67
|
|
|
return $this->getTotal() - $this->getSubtotal(); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
1 |
|
public function formatCurrency($sum, $currency = null) |
71
|
|
|
{ |
72
|
|
|
return Yii::$app->formatter->format($sum, ['currency', $currency ?: $this->currency]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sets cart from serialized string |
77
|
|
|
* @param string $serialized |
78
|
|
|
*/ |
79
|
|
|
public function setSerialized($serialized) |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
|
|
parent::setSerialized($serialized); |
83
|
|
|
} catch (\Exception $e) { |
84
|
|
|
Yii::error('Failed to unserlialize cart: ' . $e->getMessage(), __METHOD__); |
85
|
|
|
$this->_positions = []; |
86
|
|
|
$this->saveToSession(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Checks whether any of cart positions has error in `id` attribute. |
92
|
|
|
* @return boolean |
93
|
|
|
*/ |
94
|
|
|
public function hasErrors() |
95
|
|
|
{ |
96
|
|
|
foreach ($this->_positions as $position) { |
97
|
|
|
if ($position->hasErrors('id')) { |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param CartPositionInterface[] $positions |
107
|
|
|
*/ |
108
|
|
|
public function putPositions($positions) |
109
|
|
|
{ |
110
|
|
|
foreach ($positions as $position) { |
111
|
|
|
if (isset($this->_positions[$position->getId()])) { |
112
|
|
|
if ($position instanceof ImmutableQuantityInterface) { |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
$existingPosition = $this->_positions[$position->getId()]; |
116
|
|
|
$existingPosition->setQuantity($existingPosition->getQuantity() + 1); |
117
|
|
|
} else { |
118
|
|
|
if ($position->getQuantity() <= 0) { |
119
|
|
|
$position->setQuantity(1); |
120
|
|
|
} |
121
|
|
|
$this->_positions[$position->getId()] = $position; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([ |
126
|
|
|
'action' => CartActionEvent::ACTION_POSITION_PUT, |
127
|
|
|
])); |
128
|
|
|
if ($this->storeInSession) |
129
|
|
|
$this->saveToSession(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|