Перейти до основного вмісту
Версія: 1.x

← Попередня · Назад до README · Наступна →

Yandex

Створення файлів​

php artisan make:feed Yandex

Наповнення фіду​

У згенерованому класі замініть extends Feed на extends YandexFeedPreset:

<?php

declare(strict_types=1);

namespace App\Feeds;

use App\Models\Product;
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use DragonCode\LaravelFeed\Presets\YandexFeedPreset;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

use function config;

class YandexFeed extends YandexFeedPreset
{
public function builder(): Builder
{
return Product::query();
}

public function info(): FeedInfo
{
return parent::info()
->name('My App') // By default, config('app.name')
->company('My Company') // By default, config('app.name')
->platform('My Platform') // By default, config('app.name')
->url(config('app.url')) // By default, config('app.url')
->email(config('app.email', '[email protected]'))
->currencies(['RUB' => 1]) // By default, ['RUB' => 1]
->categories([
1 => 'Foo',
2 => 'Bar',
])
->additional([
'foo' => 'bar',
]);
}

public function item(Model $model): FeedItem
{
return parent::item($model)
->attributeId($model->getKey()) // By default, $model->getKey()
->attributeAvailable($model->quantity > 0) // By default, true
->attributeType('vendor.model') // By default, 'vendor.model'
->barcode($model->article) // By default, null
->url($model->url)
->title($model->title)
->description($model->description)
->price($model->price)
->currencyId('RUB') // By default, 'RUB'
->vendor($model->brand) // By default, null
->images($model->images)
->additional([
'foo' => 'bar',
]);
}

public function filename(): string
{
return 'yandex.xml';
}
}

Власна структура категорій​

Типово category() будує категорію з її ID та назви. Якщо потрібні додаткові властивості або інша структура, використовуйте rawCategory(). Масив додається до фіду без змін:

use DragonCode\LaravelFeed\Presets\Info\YandexFeedInfo;

$info = (new YandexFeedInfo)
->category(1, 'Electronics')
->rawCategory([
'@attributes' => [
'id' => 2,
'parentId' => 1,
],
'@value' => 'Smartphones',
]);

Метод categories() приймає обидві форми. Рядкові значення він передає до category(), а масиви — до rawCategory().

примітка

Власні масиви категорій не перевіряються й не перетворюються. Будуйте їх за допомогою підтримуваних XML-директив.

Активація​

Перевірте згенерований файл операції або міграції, а потім виконайте відповідну консольну команду:

# For Laravel Deploy Operations
php artisan operations

# For Laravel Migrations
php artisan migrate

Генерування фіду​

Згенеруйте фід такою консольною командою:

php artisan feed:generate

Результат​

<!DOCTYPE yml_catalog SYSTEM "shops.dtd">
<yml_catalog date="2025-09-04T04:08:12+00:00">
<shop>
<name>My App</name>
<company>My Company</company>
<platform>My Platform</platform>
<url>https://example.com</url>
<email>[email protected]</email>
<currencies>
<currency id="RUB" rate="1"/>
</currencies>
<categories>
<category id="1">Foo</category>
<category id="2">Bar</category>
</categories>
<foo>bar</foo>

<offers>

<offer id="1" available="true" type="vendor.model">
<url>https://example.com/products/ea-voluptatum-fuga-odio-expedita</url>
<barcode>GD-PRDCT-1</barcode>
<name>Some 1</name>
<description>Some description 1</description>
<price>100</price>
<currencyId>RUB</currencyId>
<vendor>The Best</vendor>
<picture>https://via.placeholder.com/640x480.png/008877?text=repudiandae</picture>
<foo>bar</foo>
</offer>
<offer id="2" available="true" type="vendor.model">
<url>https://example.com/products/dolores-rerum-ut-consequatur-in</url>
<barcode>GD-PRDCT-2</barcode>
<name>Some 2</name>
<description>Some description 2</description>
<price>250</price>
<currencyId>RUB</currencyId>
<vendor>The Best</vendor>
<picture>https://via.placeholder.com/640x480.png/009966?text=beatae</picture>
<picture>https://via.placeholder.com/640x480.png/000011?text=deleniti</picture>
<picture>https://via.placeholder.com/640x480.png/009999?text=voluptates</picture>
<foo>bar</foo>
</offer>

</offers>
</shop>
</yml_catalog>

Дивіться також​