1
|
|
|
<?php |
2
|
|
|
namespace app\modules\seo\handlers; |
3
|
|
|
|
4
|
|
|
use app\components\Controller; |
5
|
|
|
use app\modules\core\events\ViewEvent; |
6
|
|
|
use app\modules\seo\assets\YandexAnalyticsAssets; |
7
|
|
|
use app\modules\shop\controllers\CartController; |
8
|
|
|
use app\modules\shop\events\CartActionEvent; |
9
|
|
|
use app\modules\shop\helpers\CurrencyHelper; |
10
|
|
|
use app\modules\shop\models\Currency; |
11
|
|
|
use app\modules\shop\models\Order; |
12
|
|
|
use app\modules\shop\models\Product; |
13
|
|
|
use yii\base\ActionEvent; |
14
|
|
|
use yii\base\Event; |
15
|
|
|
use yii\base\BaseObject; |
16
|
|
|
use yii\helpers\Json; |
17
|
|
|
use yii\web\View; |
18
|
|
|
|
19
|
|
|
class YandexEcommerceHandler extends BaseObject |
20
|
|
|
{ |
21
|
|
|
/** @var Currency $currency */ |
22
|
|
|
static protected $currency = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Install handlers |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
static public function installHandlers(ActionEvent $event) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$currency = \Yii::$app->getModule('seo')->analytics['ecYandex']['currency']; |
30
|
|
|
if (AnalyticsHandler::CURRENCY_MAIN === intval($currency)) { |
31
|
|
|
static::$currency = CurrencyHelper::getMainCurrency(); |
32
|
|
|
} elseif (AnalyticsHandler::CURRENCY_USER === intval($currency)) { |
33
|
|
|
static::$currency = CurrencyHelper::getUserCurrency(); |
34
|
|
|
} else { |
35
|
|
|
static::$currency = CurrencyHelper::findCurrencyByIso($currency); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$route = implode('/', [ |
39
|
|
|
$event->action->controller->module->id, |
40
|
|
|
$event->action->controller->id, |
41
|
|
|
$event->action->id |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
Event::on( |
45
|
|
|
CartController::className(), |
|
|
|
|
46
|
|
|
CartController::EVENT_ACTION_ADD, |
47
|
|
|
[self::className(), 'handleCartAdd'], |
|
|
|
|
48
|
|
|
false |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
Event::on( |
52
|
|
|
CartController::className(), |
|
|
|
|
53
|
|
|
CartController::EVENT_ACTION_REMOVE, |
54
|
|
|
[self::className(), 'handleRemoveFromCart'], |
|
|
|
|
55
|
|
|
false |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
Event::on( |
59
|
|
|
CartController::className(), |
|
|
|
|
60
|
|
|
CartController::EVENT_ACTION_QUANTITY, |
61
|
|
|
[self::className(), 'handleChangeQuantity'], |
|
|
|
|
62
|
|
|
false |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
Event::on( |
66
|
|
|
CartController::className(), |
|
|
|
|
67
|
|
|
CartController::EVENT_ACTION_CLEAR, |
68
|
|
|
[self::className(), 'handleClearCart'], |
|
|
|
|
69
|
|
|
false |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
Event::on( |
73
|
|
|
Controller::className(), |
|
|
|
|
74
|
|
|
Controller::EVENT_PRE_DECORATOR, |
75
|
|
|
[self::className(), 'handleProductShow'] |
|
|
|
|
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
if ('shop/cart/index' === $route) { |
79
|
|
|
self::handleCartIndex(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
YandexAnalyticsAssets::register(\Yii::$app->getView()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param CartActionEvent $event |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
static public function handleCartAdd(CartActionEvent $event) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$result = $event->getEventData(); |
91
|
|
|
/** @var Currency $currency */ |
92
|
|
|
$currency = static::$currency; |
93
|
|
|
|
94
|
|
|
$ya = []; |
95
|
|
|
|
96
|
|
|
$ya['currency'] = $currency->iso_code; |
97
|
|
|
$ya['products'] = array_reduce($event->getProducts(), function($res, $item) use ($currency) { |
98
|
|
|
$quantity = $item['quantity']; |
99
|
|
|
/** @var Product $item */ |
100
|
|
|
$item = $item['model']; |
101
|
|
|
|
102
|
|
|
$res[] = [ |
103
|
|
|
'id' => $item->id, |
104
|
|
|
'name' => $item->name, |
105
|
|
|
'category' => self::getCategories($item), |
106
|
|
|
'price' => CurrencyHelper::convertCurrencies($item->price, $item->currency, $currency), |
|
|
|
|
107
|
|
|
'quantity' => $quantity, |
108
|
|
|
]; |
109
|
|
|
return $res; |
110
|
|
|
}, []); |
111
|
|
|
|
112
|
|
|
$result['ecYandex'] = $ya; |
113
|
|
|
$event->setEventData($result); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param CartActionEvent $event |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
static public function handleRemoveFromCart(CartActionEvent $event) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$result = $event->getEventData(); |
122
|
|
|
/** @var Currency $currency */ |
123
|
|
|
$currency = static::$currency; |
124
|
|
|
|
125
|
|
|
$ya = []; |
126
|
|
|
|
127
|
|
|
$ya['currency'] = $currency->iso_code; |
128
|
|
|
$ya['products'] = array_reduce($event->getProducts(), function($res, $item) use ($currency) { |
129
|
|
|
$quantity = $item['quantity']; |
130
|
|
|
/** @var Product $item */ |
131
|
|
|
$item = $item['model']; |
132
|
|
|
|
133
|
|
|
$res[] = [ |
134
|
|
|
'id' => $item->id, |
135
|
|
|
'name' => $item->name, |
136
|
|
|
'category' => self::getCategories($item), |
137
|
|
|
'price' => CurrencyHelper::convertCurrencies($item->price, $item->currency, $currency), |
|
|
|
|
138
|
|
|
'quantity' => $quantity, |
139
|
|
|
]; |
140
|
|
|
return $res; |
141
|
|
|
}, []); |
142
|
|
|
|
143
|
|
|
$result['ecYandex'] = $ya; |
144
|
|
|
$event->setEventData($result); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param CartActionEvent $event |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
static public function handleChangeQuantity(CartActionEvent $event) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$result = $event->getEventData(); |
153
|
|
|
/** @var Currency $currency */ |
154
|
|
|
$currency = static::$currency; |
155
|
|
|
|
156
|
|
|
$ya = []; |
157
|
|
|
|
158
|
|
|
$ya['currency'] = $currency->iso_code; |
159
|
|
|
$ya['products'] = array_reduce($event->getProducts(), function($res, $item) use ($currency) { |
160
|
|
|
$quantity = $item['quantity']; |
161
|
|
|
/** @var Product $item */ |
162
|
|
|
$item = $item['model']; |
163
|
|
|
|
164
|
|
|
$res[] = [ |
165
|
|
|
'id' => $item->id, |
166
|
|
|
'name' => $item->name, |
167
|
|
|
'category' => self::getCategories($item), |
168
|
|
|
'price' => CurrencyHelper::convertCurrencies($item->price, $item->currency, $currency), |
|
|
|
|
169
|
|
|
'quantity' => $quantity, |
170
|
|
|
]; |
171
|
|
|
return $res; |
172
|
|
|
}, []); |
173
|
|
|
|
174
|
|
|
$result['ecYandex'] = $ya; |
175
|
|
|
$event->setEventData($result); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param CartActionEvent $event |
180
|
|
|
*/ |
181
|
|
View Code Duplication |
static public function handleClearCart(CartActionEvent $event) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
$result = $event->getEventData(); |
184
|
|
|
/** @var Currency $currency */ |
185
|
|
|
$currency = static::$currency; |
186
|
|
|
|
187
|
|
|
$ya = []; |
188
|
|
|
|
189
|
|
|
$ya['currency'] = $currency->iso_code; |
190
|
|
|
$ya['products'] = array_reduce($event->getProducts(), function($res, $item) use ($currency) { |
191
|
|
|
$quantity = $item['quantity']; |
192
|
|
|
/** @var Product $item */ |
193
|
|
|
$item = $item['model']; |
194
|
|
|
|
195
|
|
|
$res[] = [ |
196
|
|
|
'id' => $item->id, |
197
|
|
|
'name' => $item->name, |
198
|
|
|
'category' => self::getCategories($item), |
199
|
|
|
'price' => CurrencyHelper::convertCurrencies($item->price, $item->currency, $currency), |
|
|
|
|
200
|
|
|
'quantity' => $quantity, |
201
|
|
|
]; |
202
|
|
|
return $res; |
203
|
|
|
}, []); |
204
|
|
|
|
205
|
|
|
$result['ecYandex'] = $ya; |
206
|
|
|
$event->setEventData($result); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param ViewEvent $event |
211
|
|
|
*/ |
212
|
|
View Code Duplication |
static public function handleProductShow(ViewEvent $event) |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
if ('shop/product/show' !== trim(\Yii::$app->requestedRoute, '/')) { |
215
|
|
|
return ; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** @var Product $model */ |
219
|
|
|
$model = isset($event->params['model']) ? $event->params['model'] : null; |
220
|
|
|
if (false === $model instanceof Product) { |
221
|
|
|
return ; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** @var Currency $currency */ |
225
|
|
|
$currency = static::$currency; |
226
|
|
|
|
227
|
|
|
$ya = [ |
228
|
|
|
'action' => 'detail', |
229
|
|
|
'currency' => $currency->iso_code, |
230
|
|
|
'products' => [ |
231
|
|
|
[ |
232
|
|
|
'id' => $model->id, |
233
|
|
|
'name' => $model->name, |
234
|
|
|
'category' => self::getCategories($model), |
235
|
|
|
'price' => CurrencyHelper::convertCurrencies($model->price, $model->currency, $currency), |
|
|
|
|
236
|
|
|
'quantity' => null === $model->measure ? 1 : $model->measure->nominal, |
237
|
|
|
] |
238
|
|
|
] |
239
|
|
|
]; |
240
|
|
|
|
241
|
|
|
$js = 'window.DotPlantParams = window.DotPlantParams || {};'; |
242
|
|
|
$js .= 'window.DotPlantParams.ecYandex = ' . Json::encode($ya) . ';'; |
243
|
|
|
\Yii::$app->getView()->registerJs($js, View::POS_BEGIN); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* |
248
|
|
|
*/ |
249
|
|
|
static public function handleCartIndex() |
|
|
|
|
250
|
|
|
{ |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* |
255
|
|
|
*/ |
256
|
|
|
static public function handlePurchase() |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
if (null === $order = Order::getOrder()) { |
259
|
|
|
return ; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** @var Currency $currency */ |
263
|
|
|
$currency = static::$currency; |
264
|
|
|
|
265
|
|
|
$ya = [ |
266
|
|
|
'action' => 'purchase', |
267
|
|
|
'currency' => $currency->iso_code, |
268
|
|
|
'orderId' => $order->id, |
269
|
|
|
'products' => [], |
270
|
|
|
]; |
271
|
|
|
|
272
|
|
View Code Duplication |
foreach ($order->items as $item) { |
273
|
|
|
$ya['products'][] = [ |
274
|
|
|
'id' => $item->product->id, |
275
|
|
|
'name' => $item->product->name, |
276
|
|
|
'category' => self::getCategories($item->product), |
|
|
|
|
277
|
|
|
'price' => CurrencyHelper::convertCurrencies($item->product->price, $item->product->currency, $currency), |
278
|
|
|
'quantity' => $item->quantity, |
279
|
|
|
]; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$js = 'window.DotPlantParams = window.DotPlantParams || {};'; |
283
|
|
|
$js .= 'window.DotPlantParams.ecYandex = ' . Json::encode($ya) . ';'; |
284
|
|
|
\Yii::$app->getView()->registerJs($js, View::POS_BEGIN); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param Product $model |
289
|
|
|
* @return string |
290
|
|
|
*/ |
291
|
|
View Code Duplication |
static private function getCategories(Product $model) |
|
|
|
|
292
|
|
|
{ |
293
|
|
|
$categories = []; |
294
|
|
|
$category = $model->category; |
295
|
|
|
while (null !== $category) { |
296
|
|
|
array_unshift($categories, $category->name); |
297
|
|
|
$category = $category->parent; |
298
|
|
|
} |
299
|
|
|
return implode('/', array_slice($categories, 0, 5)); |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|