|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\shop\components\GoogleMerchants; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use app\modules\shop\helpers\CurrencyHelper; |
|
7
|
|
|
use app\modules\shop\models\Category; |
|
8
|
|
|
use app\modules\shop\models\Product; |
|
9
|
|
|
use yii\helpers\Url; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class DefaultHandler implements ModificationDataInterface |
|
13
|
|
|
{ |
|
14
|
|
|
public static function processData(ModificationDataEvent $event) |
|
15
|
|
|
{ |
|
16
|
|
|
$imageObject = $event->model->image; |
|
17
|
|
|
if (!$imageObject) { |
|
18
|
|
|
return; |
|
19
|
|
|
} |
|
20
|
|
|
$event->data = [ |
|
21
|
|
|
'title' => $event->model->name, |
|
22
|
|
|
'description' => $event->model->announce, |
|
23
|
|
|
'link' => $event->sender->host . htmlspecialchars(Url::toRoute(['@product', 'model' => $event->model])), |
|
24
|
|
|
'g:id' => $event->model->id, |
|
25
|
|
|
'g:image_link' => htmlspecialchars( |
|
26
|
|
|
$event->sender->host . $event->model->image->getOriginalUrl() |
|
27
|
|
|
), |
|
28
|
|
|
'g:condition' => 'new', |
|
29
|
|
|
'g:product_type' => self::getProductType($event->model), |
|
30
|
|
|
'g:availability' => static::getAvailability($event->model), |
|
31
|
|
|
'g:price' => static::getPrice($event->model, $event->sender->mainCurrency) |
|
32
|
|
|
]; |
|
33
|
|
|
if ($event->model->parent_id !== 0) { |
|
34
|
|
|
$event->data['g:item_group_id'] = $event->model->parent_id; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
protected static function getProductType(Product $model) |
|
40
|
|
|
{ |
|
41
|
|
|
$parentIds = $model->getMainCategory()->getParentIds(); |
|
42
|
|
|
|
|
43
|
|
|
$breadcrumbs = []; |
|
44
|
|
|
|
|
45
|
|
|
foreach ($parentIds as $id) { |
|
46
|
|
|
$breadcrumbs[] = Category::find()->select(['name'])->where(['id' => $id])->asArray()->scalar(); |
|
47
|
|
|
} |
|
48
|
|
|
$breadcrumbs[] = $model->getMainCategory()->name; |
|
49
|
|
|
|
|
50
|
|
|
return htmlspecialchars(implode(' > ', $breadcrumbs)); |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected static function getPrice(Product $model, $mainCurrency) |
|
56
|
|
|
{ |
|
57
|
|
|
return number_format( |
|
58
|
|
|
CurrencyHelper::convertCurrencies($model->price, $model->currency, |
|
59
|
|
|
$mainCurrency), |
|
60
|
|
|
2, |
|
61
|
|
|
'.', |
|
62
|
|
|
'' |
|
63
|
|
|
) . ' ' . $mainCurrency->iso_code; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected static function getAvailability(Product $model) |
|
67
|
|
|
{ |
|
68
|
|
|
if ($model->unlimited_count === 1) { |
|
69
|
|
|
$inStock = 'in stock'; |
|
70
|
|
|
} else { |
|
71
|
|
|
$inStock = 'out of stock'; |
|
72
|
|
|
foreach ($model->getWarehousesState() as $warehouse) { |
|
73
|
|
|
if ($warehouse->in_warehouse > 0) { |
|
74
|
|
|
$inStock = 'in stock'; |
|
75
|
|
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
return $inStock; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
} |