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