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 Closure; |
15
|
|
|
use Yii; |
16
|
|
|
use yii\helpers\Url; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Cart Module. |
20
|
|
|
* |
21
|
|
|
* Example application configuration: |
22
|
|
|
* |
23
|
|
|
* ```php |
24
|
|
|
* 'modules' => [ |
25
|
|
|
* 'cart' => [ |
26
|
|
|
* 'class' => 'hiqdev\yii2\cart\Module', |
27
|
|
|
* ], |
28
|
|
|
* ], |
29
|
|
|
* ``` |
30
|
|
|
*/ |
31
|
|
|
class Module extends \yii\base\Module |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var array array of the options that will be passed to [[set]] |
35
|
|
|
*/ |
36
|
|
|
public $shoppingCartOptions = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Cart component ID. |
40
|
|
|
*/ |
41
|
|
|
const CART_COMPONENT_ID = 'cart'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
* @throws \yii\base\InvalidConfigException |
46
|
5 |
|
*/ |
47
|
|
|
public function init() |
48
|
5 |
|
{ |
49
|
5 |
|
parent::init(); |
50
|
5 |
|
if (!$this->has(static::CART_COMPONENT_ID)) { |
51
|
5 |
|
$this->set(static::CART_COMPONENT_ID, array_merge([ |
52
|
5 |
|
'class' => 'hiqdev\yii2\cart\ShoppingCart', |
53
|
|
|
], $this->shoppingCartOptions)); |
54
|
5 |
|
} |
55
|
5 |
|
$this->get(static::CART_COMPONENT_ID)->module = $this; |
56
|
5 |
|
$this->registerTranslations(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Registers translations. |
61
|
|
|
* @void |
62
|
5 |
|
*/ |
63
|
|
|
public function registerTranslations() |
64
|
5 |
|
{ |
65
|
|
|
Yii::$app->i18n->translations['cart'] = [ |
66
|
|
|
'class' => 'yii\i18n\PhpMessageSource', |
67
|
|
|
'sourceLanguage' => 'en-US', |
68
|
|
|
'basePath' => '@hiqdev/yii2/cart/messages', |
69
|
|
|
'fileMap' => [ |
70
|
|
|
'merchant' => 'cart.php', |
71
|
|
|
], |
72
|
5 |
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var string the module name |
77
|
|
|
*/ |
78
|
|
|
public static $name = 'cart'; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Finds cart module. |
82
|
|
|
* TODO think of how to find NOT by name. |
83
|
1 |
|
*/ |
84
|
|
|
public static function getInstance() |
85
|
1 |
|
{ |
86
|
|
|
return Yii::$app->getModule(static::$name); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @throws \yii\base\InvalidConfigException |
91
|
|
|
* @return null|\yz\shoppingcart\ShoppingCart|ShoppingCart |
92
|
1 |
|
*/ |
93
|
|
|
public function getCart() |
94
|
1 |
|
{ |
95
|
|
|
return $this->get(static::CART_COMPONENT_ID); |
96
|
|
|
} |
97
|
1 |
|
|
98
|
|
|
public function createUrl($route = null) |
99
|
1 |
|
{ |
100
|
1 |
|
$params = is_array($route) ? $route : [$route]; |
101
|
|
|
$params[0] = '/' . $this->id . '/' . (strpos($params[0], '/') !== false ? $params[0] : 'cart/' . ($params[0] ?: 'index')); |
102
|
1 |
|
|
103
|
|
|
return Url::toRoute($params); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var string|array route to the terms of use page, suitable for Url::to(). |
108
|
|
|
*/ |
109
|
|
|
public $termsPage; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @var string|array route to the order page, suitable for Url::to(). |
113
|
|
|
*/ |
114
|
|
|
public $orderPage = ['order']; |
115
|
|
|
|
116
|
|
|
protected $_orderButton; |
117
|
1 |
|
|
118
|
|
|
public function setOrderButton($value) |
119
|
1 |
|
{ |
120
|
1 |
|
$this->_orderButton = $value; |
121
|
|
|
} |
122
|
1 |
|
|
123
|
|
|
public function getOrderButton() |
124
|
1 |
|
{ |
125
|
|
|
return $this->_orderButton instanceof Closure ? call_user_func($this->_orderButton, $this) : $this->_orderButton; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected $_paymentMethods; |
129
|
1 |
|
|
130
|
|
|
public function setPaymentMethods($value) |
131
|
1 |
|
{ |
132
|
1 |
|
$this->_paymentMethods = $value; |
133
|
|
|
} |
134
|
1 |
|
|
135
|
|
|
public function getPaymentMethods() |
136
|
1 |
|
{ |
137
|
|
|
return $this->_paymentMethods instanceof Closure ? call_user_func($this->_paymentMethods, $this) : $this->_paymentMethods; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getBuyMoreLinks(): array |
141
|
|
|
{ |
142
|
|
|
$links = []; |
143
|
|
|
$positions = $this->getCart()->getPositions(); |
144
|
|
|
if (empty($positions)) { |
145
|
|
|
return $links; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
foreach ($positions as $position) { |
149
|
|
|
[$url, $label] = $position->getBuyMoreLink(); |
|
|
|
|
150
|
|
|
if ($url && $label && !isset($links[$url])) { |
151
|
|
|
$links[$url] = $label; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $links; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.