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\base\InvalidConfigException; |
17
|
|
|
use yii\helpers\Url; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Cart Module. |
21
|
|
|
* |
22
|
|
|
* Example application configuration: |
23
|
|
|
* |
24
|
|
|
* ```php |
25
|
|
|
* 'modules' => [ |
26
|
|
|
* 'cart' => [ |
27
|
|
|
* 'class' => 'hiqdev\yii2\cart\Module', |
28
|
|
|
* ], |
29
|
|
|
* ], |
30
|
|
|
* ``` |
31
|
|
|
*/ |
32
|
|
|
class Module extends \yii\base\Module |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var array array of the options that will be passed to [[set]] |
36
|
|
|
*/ |
37
|
|
|
public $shoppingCartOptions = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Cart component ID. |
41
|
|
|
*/ |
42
|
|
|
const CART_COMPONENT_ID = 'cart'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
5 |
|
* @throws \yii\base\InvalidConfigException |
47
|
|
|
*/ |
48
|
5 |
|
public function init() |
49
|
5 |
|
{ |
50
|
5 |
|
parent::init(); |
51
|
5 |
|
if (!$this->has(static::CART_COMPONENT_ID)) { |
52
|
5 |
|
$this->set(static::CART_COMPONENT_ID, array_merge([ |
53
|
|
|
'class' => 'hiqdev\yii2\cart\ShoppingCart', |
54
|
5 |
|
], $this->shoppingCartOptions)); |
55
|
5 |
|
} |
56
|
5 |
|
$this->get(static::CART_COMPONENT_ID)->module = $this; |
57
|
|
|
$this->registerTranslations(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Registers translations. |
62
|
5 |
|
* @void |
63
|
|
|
*/ |
64
|
5 |
|
public function registerTranslations() |
65
|
|
|
{ |
66
|
|
|
Yii::$app->i18n->translations['cart'] = [ |
67
|
|
|
'class' => 'yii\i18n\PhpMessageSource', |
68
|
|
|
'sourceLanguage' => 'en-US', |
69
|
|
|
'basePath' => __DIR__ . '/messages', |
70
|
|
|
'fileMap' => [ |
71
|
|
|
'merchant' => 'cart.php', |
72
|
5 |
|
], |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var string the module name |
78
|
|
|
*/ |
79
|
|
|
public static $name = 'cart'; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Finds cart module. |
83
|
1 |
|
* TODO think of how to find NOT by name. |
84
|
|
|
*/ |
85
|
1 |
|
public static function getInstance() |
86
|
|
|
{ |
87
|
|
|
return Yii::$app->getModule(static::$name); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @throws \yii\base\InvalidConfigException |
92
|
1 |
|
* @return null|\yz\shoppingcart\ShoppingCart|ShoppingCart |
93
|
|
|
*/ |
94
|
1 |
|
public function getCart() |
95
|
|
|
{ |
96
|
|
|
return $this->get(static::CART_COMPONENT_ID); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
public function createUrl($route = null) |
100
|
1 |
|
{ |
101
|
|
|
$params = is_array($route) ? $route : [$route]; |
102
|
1 |
|
$params[0] = '/' . $this->id . '/' . (strpos($params[0], '/') !== false ? $params[0] : 'cart/' . ($params[0] ?: 'index')); |
103
|
|
|
|
104
|
|
|
return Url::toRoute($params); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @var string|array route to the terms of use page, suitable for Url::to(). |
109
|
|
|
*/ |
110
|
|
|
public $termsPage; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @var string|array route to the order page, suitable for Url::to(). |
114
|
|
|
*/ |
115
|
|
|
public $orderPage = ['order']; |
116
|
|
|
|
117
|
1 |
|
protected $_orderButton; |
118
|
|
|
|
119
|
1 |
|
public function setOrderButton($value) |
120
|
1 |
|
{ |
121
|
|
|
$this->_orderButton = $value; |
122
|
1 |
|
} |
123
|
|
|
|
124
|
1 |
|
public function getOrderButton() |
125
|
|
|
{ |
126
|
|
|
return $this->_orderButton instanceof Closure ? call_user_func($this->_orderButton, $this) : $this->_orderButton; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
/** |
130
|
|
|
* @var PaymentMethodsProviderInterface|null |
131
|
1 |
|
*/ |
132
|
1 |
|
public $paymentMethodsProvider; |
133
|
|
|
|
134
|
1 |
|
public function getPaymentMethods() |
135
|
|
|
{ |
136
|
1 |
|
if (empty($this->paymentMethodsProvider)) { |
137
|
|
|
return new InvalidConfigException('Payment methods provider must be set'); |
138
|
|
|
} |
139
|
|
|
if (!($this->paymentMethodsProvider instanceof PaymentMethodsProviderInterface)) { |
140
|
|
|
return new InvalidConfigException('Payment methods provider are bad configured'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this->paymentMethodsProvider->getPaymentMethods(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|