|
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
|
|
|
|
|
15
|
|
|
protected static $breadcrumbsData = []; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
public static function processData(ModificationDataEvent $event) |
|
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:condition' => 'new', |
|
26
|
|
|
'g:product_type' => self::getProductType($event->model), |
|
27
|
|
|
'g:availability' => static::getAvailability($event->model), |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
if ($manufacturer = $event->model->property($event->sender->brand_property)) { |
|
32
|
|
|
$event->data['g:brand'] = htmlspecialchars($manufacturer); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (!empty($event->model->old_price) && $event->model->old_price > $event->model->price) { |
|
36
|
|
|
$event->data['g:price'] = static::getPrice($event->model, |
|
37
|
|
|
$event->sender->mainCurrency, |
|
38
|
|
|
$event->model->old_price |
|
39
|
|
|
); |
|
40
|
|
|
$event->data['g:g:sale_price'] = static::getPrice($event->model, |
|
41
|
|
|
$event->sender->mainCurrency, |
|
42
|
|
|
$event->model->price |
|
43
|
|
|
); |
|
44
|
|
|
} else { |
|
45
|
|
|
$event->data['g:price'] = static::getPrice($event->model, |
|
46
|
|
|
$event->sender->mainCurrency, |
|
47
|
|
|
$event->model->price |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
$imageObject = $event->model->image; |
|
51
|
|
|
if ($imageObject) { |
|
52
|
|
|
$event->data['g:image_link'] = htmlspecialchars( |
|
53
|
|
|
$event->sender->host . $event->model->image->getOriginalUrl() |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
if ($event->model->parent_id !== 0) { |
|
57
|
|
|
$event->data['g:item_group_id'] = $event->model->parent_id; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
protected static function getProductType(Product $model) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!isset(self::$breadcrumbsData[$model->main_category_id])) { |
|
65
|
|
|
$parentIds = $model->getMainCategory()->getParentIds(); |
|
66
|
|
|
$breadcrumbs = []; |
|
67
|
|
|
foreach ($parentIds as $id) { |
|
68
|
|
|
$breadcrumbs[] = Category::find()->select(['name'])->where(['id' => $id])->asArray()->scalar(); |
|
69
|
|
|
} |
|
70
|
|
|
$breadcrumbs[] = $model->getMainCategory()->name; |
|
71
|
|
|
self::$breadcrumbsData[$model->main_category_id] = $breadcrumbs; |
|
72
|
|
|
} |
|
73
|
|
|
return htmlspecialchars(implode(' > ', self::$breadcrumbsData[$model->main_category_id])); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected static function getPrice(Product $model, $mainCurrency, $price) |
|
77
|
|
|
{ |
|
78
|
|
|
return number_format( |
|
79
|
|
|
CurrencyHelper::convertCurrencies($price, $model->currency, |
|
80
|
|
|
$mainCurrency), |
|
81
|
|
|
2, |
|
82
|
|
|
'.', |
|
83
|
|
|
'' |
|
84
|
|
|
) . ' ' . $mainCurrency->iso_code; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected static function getAvailability(Product $model) |
|
88
|
|
|
{ |
|
89
|
|
|
if ($model->unlimited_count === 1) { |
|
90
|
|
|
$inStock = 'in stock'; |
|
91
|
|
|
} else { |
|
92
|
|
|
$inStock = 'out of stock'; |
|
93
|
|
|
foreach ($model->getWarehousesState() as $warehouse) { |
|
94
|
|
|
if ($warehouse->in_warehouse > 0) { |
|
95
|
|
|
$inStock = 'in stock'; |
|
96
|
|
|
break; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
return $inStock; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
} |