|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\shop\components\GoogleMerchants; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use app\models\Property; |
|
7
|
|
|
use app\modules\shop\helpers\CurrencyHelper; |
|
8
|
|
|
use app\modules\shop\models\Category; |
|
9
|
|
|
use app\modules\shop\models\GoogleFeed; |
|
10
|
|
|
use app\modules\shop\models\Product; |
|
11
|
|
|
use yii\console\Exception; |
|
12
|
|
|
use yii\helpers\Url; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class DefaultHandler implements ModificationDataInterface |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
protected static $breadcrumbsData = []; |
|
19
|
|
|
|
|
20
|
|
|
protected static $modelSetting = null; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
public static function processData(ModificationDataEvent $event) |
|
24
|
|
|
{ |
|
25
|
|
|
if (!self::$modelSetting) { |
|
26
|
|
|
self::$modelSetting = new GoogleFeed(); |
|
27
|
|
|
self::$modelSetting->loadConfig(); |
|
28
|
|
|
} |
|
29
|
|
|
$event->dataArray = [ |
|
30
|
|
|
'title' => self::getRelation($event->model, 'item_title', $event->model->name), |
|
31
|
|
|
'description' => self::getRelation($event->model, 'item_description', $event->model->announce), |
|
32
|
|
|
'link' => $event->sender->host . htmlspecialchars(Url::toRoute(['@product', 'model' => $event->model])), |
|
33
|
|
|
'g:id' => $event->model->id, |
|
34
|
|
|
'g:condition' => self::$modelSetting->item_condition, |
|
35
|
|
|
'g:product_type' => self::getProductType($event->model), |
|
36
|
|
|
'g:availability' => static::getAvailability($event->model) |
|
37
|
|
|
]; |
|
38
|
|
|
if ($manufacturer = self::getRelation($event->model, 'item_brand', false)) { |
|
|
|
|
|
|
39
|
|
|
$event->dataArray['g:brand'] = $manufacturer; |
|
40
|
|
|
} |
|
41
|
|
|
if ($gin = self::getRelation($event->model, 'item_gtin', false)) { |
|
|
|
|
|
|
42
|
|
|
$event->dataArray['g:gtin'] = $gin; |
|
43
|
|
|
} |
|
44
|
|
|
if ($mpn = self::getRelation($event->model, 'item_mpn', false)) { |
|
|
|
|
|
|
45
|
|
|
$event->dataArray['g:mpn'] = $mpn; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($item_google_product_category = self::getRelation($event->model, 'item_google_product_category', false)) { |
|
|
|
|
|
|
49
|
|
|
$event->dataArray['g:google_product_category'] = $item_google_product_category; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!empty($event->model->old_price) && $event->model->old_price > $event->model->price) { |
|
53
|
|
|
$event->dataArray['g:price'] = static::getPrice( |
|
54
|
|
|
$event->model, |
|
55
|
|
|
$event->sender->mainCurrency, |
|
56
|
|
|
$event->model->old_price |
|
57
|
|
|
); |
|
58
|
|
|
$event->dataArray['g:sale_price'] = static::getPrice( |
|
59
|
|
|
$event->model, |
|
60
|
|
|
$event->sender->mainCurrency, |
|
61
|
|
|
$event->model->price |
|
62
|
|
|
); |
|
63
|
|
|
} else { |
|
64
|
|
|
|
|
65
|
|
|
$event->dataArray['g:price'] = static::getPrice( |
|
66
|
|
|
$event->model, |
|
67
|
|
|
$event->sender->mainCurrency, |
|
68
|
|
|
$event->model->price |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
$imageObject = $event->model->image; |
|
72
|
|
|
if ($imageObject) { |
|
73
|
|
|
$event->dataArray['g:image_link'] = htmlspecialchars( |
|
74
|
|
|
$event->sender->host . $event->model->image->getOriginalUrl() |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
if ($event->model->parent_id !== 0) { |
|
78
|
|
|
$event->dataArray['g:item_group_id'] = $event->model->parent_id; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
protected static function getRelation($model, $relationName, $default_value = '') |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
$result = $default_value; |
|
86
|
|
|
$relation = self::$modelSetting->$relationName; |
|
87
|
|
|
|
|
88
|
|
|
if ($relation['type'] === 'field' && !empty($relation['key'])) { |
|
89
|
|
|
try { |
|
90
|
|
|
$result = $model->{$relation['key']}; |
|
91
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
} elseif ($relation['type'] === 'property' && !empty($relation['key'])) { |
|
94
|
|
|
try { |
|
95
|
|
|
$propertyKey = Property::find()->select('key')->where(['id' => $relation['key']])->asArray()->scalar(); |
|
96
|
|
|
$result = $model->property($propertyKey); |
|
97
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
} elseif ($relation['type'] === 'relation' && !empty($relation['key']) && !empty($relation['value'])) { |
|
100
|
|
|
$relModel = $model->{$relation['key']}(); |
|
101
|
|
|
$result = $relModel->$relation['value']; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $result; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
protected static function getProductType(Product $model) |
|
109
|
|
|
{ |
|
110
|
|
|
if (!isset(self::$breadcrumbsData[$model->main_category_id])) { |
|
111
|
|
|
$parentIds = $model->getMainCategory()->getParentIds(); |
|
112
|
|
|
$breadcrumbs = []; |
|
113
|
|
|
foreach ($parentIds as $id) { |
|
114
|
|
|
$breadcrumbs[] = Category::find()->select(['name'])->where(['id' => $id])->asArray()->scalar(); |
|
115
|
|
|
} |
|
116
|
|
|
$breadcrumbs[] = $model->getMainCategory()->name; |
|
117
|
|
|
self::$breadcrumbsData[$model->main_category_id] = $breadcrumbs; |
|
118
|
|
|
} |
|
119
|
|
|
return htmlspecialchars(implode(' > ', self::$breadcrumbsData[$model->main_category_id])); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
protected static function getPrice(Product $model, $mainCurrency, $price) |
|
123
|
|
|
{ |
|
124
|
|
|
return number_format( |
|
125
|
|
|
CurrencyHelper::convertCurrencies( |
|
126
|
|
|
$price, |
|
127
|
|
|
$model->currency, |
|
128
|
|
|
$mainCurrency |
|
129
|
|
|
), |
|
130
|
|
|
2, |
|
131
|
|
|
'.', |
|
132
|
|
|
'' |
|
133
|
|
|
) . ' ' . $mainCurrency->iso_code; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
protected static function getAvailability(Product $model) |
|
137
|
|
|
{ |
|
138
|
|
|
if ($model->unlimited_count === 1) { |
|
139
|
|
|
$inStock = 'in stock'; |
|
140
|
|
|
} else { |
|
141
|
|
|
$inStock = 'out of stock'; |
|
142
|
|
|
foreach ($model->getWarehousesState() as $warehouse) { |
|
143
|
|
|
if ($warehouse->in_warehouse > 0) { |
|
144
|
|
|
$inStock = 'in stock'; |
|
145
|
|
|
break; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
return $inStock; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: