8a7e6ecf
Yarik
Namespaces
|
1
2
3
4
5
|
<?php
namespace artweb\artbox\ecommerce\models;
use yii\base\Component;
|
363d62a0
Yarik
Added basket cookies
|
6
7
|
use yii\helpers\Json;
use yii\web\Cookie;
|
8a7e6ecf
Yarik
Namespaces
|
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
use yii\web\NotFoundHttpException;
/**
* Class Basket to work with basket
*
* @package artweb\artbox\ecommerce\models
*/
class Basket extends Component
{
/**
* Session object
*
* @var \yii\web\Session
*/
public $session;
/**
* Basket constructor.
* Check for basket variable in session and set it to empty array if not exist.
*
* @param array $config
*/
public function __construct(array $config = [])
{
$this->session = \Yii::$app->session;
if (!$this->session->has('basket')) {
$this->session->set('basket', []);
|
363d62a0
Yarik
Added basket cookies
|
35
36
37
38
39
40
41
42
43
44
|
} elseif(!empty($this->session->get('basket'))) {
$cookies = \Yii::$app->response->cookies;
$cookies->add(
new Cookie(
[
'name' => 'basket',
'value' => Json::encode($this->session->get('basket')),
]
)
);
|
8a7e6ecf
Yarik
Namespaces
|
45
46
47
|
}
parent::__construct($config);
}
|
04e8796d
Yarik
Not in stock
|
48
|
|
8a7e6ecf
Yarik
Namespaces
|
49
50
51
52
|
/**
* Increment product variant with $product_variant_id count by 1
*
* @param int $product_variant_id
|
b213b850
Yarik
Basket
|
53
|
* @param int $count
|
8a7e6ecf
Yarik
Namespaces
|
54
|
*/
|
b213b850
Yarik
Basket
|
55
|
public function add(int $product_variant_id, int $count)
|
8a7e6ecf
Yarik
Namespaces
|
56
57
58
59
|
{
$data = $this->getData();
if (array_key_exists($product_variant_id, $data)) {
if ($data[ $product_variant_id ][ 'count' ] <= 0) {
|
b213b850
Yarik
Basket
|
60
|
$data[ $product_variant_id ][ 'count' ] = $count;
|
8a7e6ecf
Yarik
Namespaces
|
61
|
} else {
|
b213b850
Yarik
Basket
|
62
|
$data[ $product_variant_id ][ 'count' ] += $count;
|
8a7e6ecf
Yarik
Namespaces
|
63
64
65
66
|
}
} else {
if ($this->findModel($product_variant_id)) {
$data[ $product_variant_id ] = [
|
b213b850
Yarik
Basket
|
67
|
'count' => $count,
|
8a7e6ecf
Yarik
Namespaces
|
68
69
70
|
];
}
}
|
04e8796d
Yarik
Not in stock
|
71
72
|
if ($data[ $product_variant_id ][ 'count' ] <= 0) {
unset( $data[ $product_variant_id ] );
|
b213b850
Yarik
Basket
|
73
|
}
|
8a7e6ecf
Yarik
Namespaces
|
74
75
76
77
78
79
80
81
82
|
$this->setData($data);
}
/**
* Set product variant with $product_variant_id to $count
*
* @param int $product_variant_id
* @param int $count
*/
|
b213b850
Yarik
Basket
|
83
|
public function set(int $product_variant_id, int $count)
|
8a7e6ecf
Yarik
Namespaces
|
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
{
$data = $this->getData();
if (array_key_exists($product_variant_id, $data)) {
$data[ $product_variant_id ][ 'count' ] = $count;
if ($data[ $product_variant_id ][ 'count' ] <= 0) {
unset( $data[ $product_variant_id ] );
}
} elseif ($count > 0) {
if ($this->findModel($product_variant_id)) {
$data[ $product_variant_id ] = [
'count' => $count,
];
}
}
$this->setData($data);
}
/**
* Delete product variant with $product_variant_id from basket
*
* @param int $product_variant_id
*/
public function delete(int $product_variant_id)
{
$this->set($product_variant_id, 0);
}
/**
* Get basket data
*
* @return array
*/
public function getData(): array
{
return $this->session->get('basket');
}
/**
* Get basket item with $product_variant_id. Returns false if item not in basket.
*
* @param int $product_variant_id
*
* @return bool|array
*/
public function getItem(int $product_variant_id)
{
$data = $this->getData();
if (!empty( $data[ $product_variant_id ] )) {
return $data[ $product_variant_id ];
} else {
return false;
}
}
/**
* Set basket data
*
* @param array $data
*/
public function setData(array $data)
{
$this->session->set('basket', $data);
|
363d62a0
Yarik
Added basket cookies
|
146
147
148
149
150
151
152
153
154
155
156
157
158
|
$cookies = \Yii::$app->response->cookies;
if(empty($data)) {
$cookies->remove('basket');
} else {
$cookies->add(
new Cookie(
[
'name' => 'basket',
'value' => Json::encode($data),
]
)
);
}
|
8a7e6ecf
Yarik
Namespaces
|
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
}
/**
* Get count of product variants in basket
*
* @return int
*/
public function getCount(): int
{
$data = $this->getData();
return count($data);
}
/**
|
b213b850
Yarik
Basket
|
173
174
175
176
177
178
179
180
181
182
|
* Get sum of product variants in basket
*
* @return float
*/
public function getSum(): float
{
$data = $this->getData();
$models = $this->findModels(array_keys($data));
$sum = 0;
foreach ($models as $model) {
|
04e8796d
Yarik
Not in stock
|
183
|
$sum += $model->price * $data[ $model->id ][ 'count' ];
|
b213b850
Yarik
Basket
|
184
185
186
187
188
|
}
return $sum;
}
/**
|
8a7e6ecf
Yarik
Namespaces
|
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
* Find Product Variant by $product_variant_id
*
* @param int $product_variant_id
*
* @return \artweb\artbox\ecommerce\models\ProductVariant
* @throws \yii\web\NotFoundHttpException
*/
public function findModel(int $product_variant_id): ProductVariant
{
/**
* @var ProductVariant $model
*/
$model = ProductVariant::find()
->where([ 'product_variant.id' => $product_variant_id ])
|
04e8796d
Yarik
Not in stock
|
203
204
205
206
207
208
209
|
->andWhere(
[
'>',
'product_variant.stock',
0,
]
)
|
b213b850
Yarik
Basket
|
210
|
->joinWith('lang')
|
8a7e6ecf
Yarik
Namespaces
|
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
->one();
if (empty( $model )) {
throw new NotFoundHttpException(\Yii::t('app', 'Product not found'));
} else {
return $model;
}
}
/**
* Find all Product Variants filtered by $product_variant_ids
*
* @param array $product_variant_ids
*
* @return ProductVariant[]
*/
public function findModels(array $product_variant_ids)
{
|
b213b850
Yarik
Basket
|
228
229
230
|
if (empty( $product_variant_ids )) {
return [];
}
|
8a7e6ecf
Yarik
Namespaces
|
231
232
|
return ProductVariant::find()
->where([ 'product_variant.id' => $product_variant_ids ])
|
b213b850
Yarik
Basket
|
233
|
->joinWith('lang', 'product')
|
8a7e6ecf
Yarik
Namespaces
|
234
235
|
->with(
[
|
b213b850
Yarik
Basket
|
236
|
'product.lang',
|
8a7e6ecf
Yarik
Namespaces
|
237
238
239
|
'image',
]
)
|
b213b850
Yarik
Basket
|
240
|
->indexBy('id')
|
8a7e6ecf
Yarik
Namespaces
|
241
242
243
244
245
246
247
248
249
250
251
252
253
|
->all();
}
/**
* Clear basket
*/
public function clear()
{
$this->setData([]);
}
}
|