|
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
|
|
|
*/ |
|
47
|
5 |
|
public function init() |
|
48
|
|
|
{ |
|
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
|
5 |
|
], $this->shoppingCartOptions)); |
|
54
|
5 |
|
} |
|
55
|
5 |
|
$this->get(static::CART_COMPONENT_ID)->module = $this; |
|
56
|
5 |
|
$this->registerTranslations(); |
|
57
|
5 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Registers translations. |
|
61
|
|
|
* @void |
|
62
|
|
|
*/ |
|
63
|
5 |
|
public function registerTranslations() |
|
64
|
|
|
{ |
|
65
|
5 |
|
Yii::$app->i18n->translations['cart'] = [ |
|
66
|
5 |
|
'class' => 'yii\i18n\PhpMessageSource', |
|
67
|
5 |
|
'sourceLanguage' => 'en-US', |
|
68
|
5 |
|
'basePath' => '@hiqdev/yii2/cart/messages', |
|
69
|
|
|
'fileMap' => [ |
|
70
|
5 |
|
'merchant' => 'cart.php', |
|
71
|
5 |
|
], |
|
72
|
|
|
]; |
|
73
|
5 |
|
} |
|
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
|
|
|
*/ |
|
84
|
1 |
|
public static function getInstance() |
|
85
|
|
|
{ |
|
86
|
1 |
|
return Yii::$app->getModule(static::$name); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @throws \yii\base\InvalidConfigException |
|
91
|
|
|
* @return null|\yz\shoppingcart\ShoppingCart|ShoppingCart |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function getCart() |
|
94
|
|
|
{ |
|
95
|
1 |
|
return $this->get(static::CART_COMPONENT_ID); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
public function createUrl($route = null) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$params = is_array($route) ? $route : [$route]; |
|
101
|
1 |
|
$params[0] = '/' . $this->id . '/' . (strpos($params[0], '/') !== false ? $params[0] : 'cart/' . ($params[0] ?: 'index')); |
|
102
|
|
|
|
|
103
|
1 |
|
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
|
|
|
|
|
118
|
1 |
|
public function setOrderButton($value) |
|
119
|
|
|
{ |
|
120
|
1 |
|
$this->_orderButton = $value; |
|
121
|
1 |
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
public function getOrderButton() |
|
124
|
|
|
{ |
|
125
|
1 |
|
return $this->_orderButton instanceof Closure ? call_user_func($this->_orderButton, $this) : $this->_orderButton; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected $_paymentMethods; |
|
129
|
|
|
|
|
130
|
1 |
|
public function setPaymentMethods($value) |
|
131
|
|
|
{ |
|
132
|
1 |
|
$this->_paymentMethods = $value; |
|
133
|
1 |
|
} |
|
134
|
|
|
|
|
135
|
1 |
|
public function getPaymentMethods() |
|
136
|
|
|
{ |
|
137
|
1 |
|
return $this->_paymentMethods instanceof Closure ? call_user_func($this->_paymentMethods, $this) : $this->_paymentMethods; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|