|
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
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ShoppingCart. |
|
19
|
|
|
* @property AbstractCartPosition[] $positions |
|
20
|
|
|
*/ |
|
21
|
|
|
class ShoppingCart extends \yz\shoppingcart\ShoppingCart |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var AbstractCartPosition[] |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $_positions = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The cart module. |
|
30
|
|
|
*/ |
|
31
|
|
|
public $module; |
|
32
|
|
|
|
|
33
|
|
|
public $currency = 'usd'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return integer |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function getCount() |
|
39
|
|
|
{ |
|
40
|
1 |
|
return count($this->_positions); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
public function getQuantity() |
|
44
|
|
|
{ |
|
45
|
1 |
|
$count = 0; |
|
46
|
1 |
|
foreach ($this->_positions as $position) { |
|
47
|
1 |
|
$count += $position->getQuantity(); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
return $count; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
public function getSubtotal() |
|
54
|
|
|
{ |
|
55
|
2 |
|
return $this->getCost(false); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
public function getTotal() |
|
59
|
|
|
{ |
|
60
|
2 |
|
return $this->getCost(true); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function getDiscount() |
|
64
|
|
|
{ |
|
65
|
1 |
|
return $this->getTotal() - $this->getSubtotal(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
public function formatCurrency($sum, $currency = null) |
|
69
|
|
|
{ |
|
70
|
1 |
|
return Yii::$app->formatter->format($sum, ['currency', $currency ?: $this->currency]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Checks whether any of cart positions has error in `id` attribute. |
|
75
|
|
|
* @return boolean |
|
76
|
|
|
*/ |
|
77
|
|
|
public function hasErrors() |
|
78
|
|
|
{ |
|
79
|
|
|
foreach ($this->_positions as $position) { |
|
80
|
|
|
if ($position->hasErrors('id')) { |
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|