|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\shop\components\GoogleMerchants; |
|
4
|
|
|
|
|
5
|
|
|
use app\modules\shop\helpers\CurrencyHelper; |
|
6
|
|
|
use app\modules\shop\models\Currency; |
|
7
|
|
|
use app\modules\shop\models\Product; |
|
8
|
|
|
use yii\helpers\Html; |
|
9
|
|
|
use yii\helpers\Url; |
|
10
|
|
|
use yii\helpers\VarDumper; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class GoogleMerchants |
|
14
|
|
|
* This implementation uses a rss 2.0 template |
|
15
|
|
|
* @package app\modules\shop\components\GoogleMerchants |
|
16
|
|
|
*/ |
|
17
|
|
|
class GoogleMerchants |
|
18
|
|
|
{ |
|
19
|
|
|
protected $host = 'https://english-brands.ru'; |
|
20
|
|
|
protected $title = 'Site title'; |
|
21
|
|
|
protected $description = 'Site description'; |
|
22
|
|
|
protected $mainCurrency; |
|
23
|
|
|
|
|
24
|
|
|
protected function getHeader() |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
return <<<HEADER |
|
27
|
|
|
<?xml version="1.0"?> |
|
28
|
|
|
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"> |
|
29
|
|
|
<channel> |
|
30
|
|
|
<title>{$this->title}</title> |
|
31
|
|
|
<link>{$this->host}</link> |
|
32
|
|
|
<description>{$this->description}</description> |
|
33
|
|
|
|
|
34
|
|
|
HEADER; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function getFooter() |
|
38
|
|
|
{ |
|
39
|
|
|
return <<<FOOTER |
|
40
|
|
|
</channel> |
|
41
|
|
|
</rss> |
|
42
|
|
|
FOOTER; |
|
43
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param Product $product |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function getItem($product) |
|
51
|
|
|
{ |
|
52
|
|
|
$url = htmlspecialchars(Url::toRoute(['@product', 'model' => $product])); |
|
53
|
|
|
$image = htmlspecialchars($product->image->getOriginalUrl()); |
|
54
|
|
|
if ($product->unlimited_count === 1) { |
|
55
|
|
|
$inStock = 'in stock'; |
|
56
|
|
|
} else { |
|
57
|
|
|
$inStock = 'out of stock'; |
|
58
|
|
|
foreach ($product->getWarehousesState() as $warehouse) { |
|
59
|
|
|
if ($warehouse->in_warehouse > 0) { |
|
60
|
|
|
$inStock = 'in stock'; |
|
61
|
|
|
break; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
$price = number_format( |
|
66
|
|
|
CurrencyHelper::convertCurrencies($product->price, $product->currency, $this->mainCurrency), |
|
67
|
|
|
2, |
|
68
|
|
|
'.', |
|
69
|
|
|
'' |
|
70
|
|
|
) . ' ' . $this->mainCurrency->iso_code; |
|
71
|
|
|
return <<<ITEM |
|
72
|
|
|
<item> |
|
73
|
|
|
<title>{$product['name']}</title> |
|
74
|
|
|
<description>{$product['content']}</description> |
|
75
|
|
|
<link>{$this->host}{$url}</link> |
|
76
|
|
|
<g:id>{$product['id']}</g:id> |
|
77
|
|
|
<g:image_link>{$image}</g:image_link> |
|
78
|
|
|
<g:condition>new</g:condition> |
|
79
|
|
|
<g:availability>{$inStock}</g:availability> |
|
80
|
|
|
<g:price>{$price}</g:price> |
|
81
|
|
|
<g:item_group_id>{$product->parent_id}</g:item_group_id> |
|
82
|
|
|
</item> |
|
83
|
|
|
|
|
84
|
|
|
ITEM; |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function generate() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->mainCurrency = Currency::getMainCurrency(); |
|
91
|
|
|
$s = $this->getHeader(); |
|
92
|
|
|
foreach (Product::find()->where(['active' => 1])->limit(2)->all() as $product) { |
|
93
|
|
|
$s .= $this->getItem($product); |
|
94
|
|
|
} |
|
95
|
|
|
$s .= $this->getFooter(); |
|
96
|
|
|
file_put_contents('/home/fps/feed.xml', $s); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.