corpsepk/yii2-yandex-market-yml

Yii2 module for automatically generation Yandex.Market YML

Installs: 8 130

Dependents: 0

Suggesters: 0

Security: 0

Stars: 15

Watchers: 7

Forks: 5

Open Issues: 0

Type:yii2-extension

0.8 2024-04-09 13:24 UTC

This package is auto-updated.

Last update: 2024-05-09 13:37:38 UTC


README

Yii2 module for automatically generation Yandex.Market YML.

Latest Version Build Status Quality Score Minimum PHP Version

Installation

The preferred way to install this extension is through composer.

  • Either run
php composer.phar require --prefer-dist "corpsepk/yii2-yandex-market-yml" "~0.8"

or add

"corpsepk/yii2-yandex-market-yml": "~0.8"

to the require section of your application's composer.json file.

Configure config

Configure the cache component of your application's configuration file, for example:

'components' => [
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
]

Add a new module in modules section of your application's configuration file, for example:

'modules' => [
    'YandexMarketYml' => [
        'class' => 'corpsepk\yml\YandexMarketYml',
        'cacheExpire' => 1, // 1 second. Default is 24 hours
        'categoryModel' => 'app\models\Category',
        'shopOptions' => [
            'name' => 'MyCompanyName',
            'company' => 'LTD MyCompanyName',
            'url' => 'http://example.com',
            'currencies' => [
                [
                    'id' => 'RUR',
                    'rate' => 1
                ]
            ],
        ],
        'offerModels' => [
            ['class' => 'app\models\Item'],
        ],
    ],
],

Add a new rule for urlManager of your application's configuration file, for example:

'urlManager' => [
    'rules' => [
        ['pattern' => 'yandex-market', 'route' => 'YandexMarketYml/default/index', 'suffix' => '.yml'],
    ],
],

Configure Category model

https://yandex.ru/support/partnermarket/elements/categories.html

Add behavior in the AR category model, for example:

use corpsepk\yml\behaviors\YmlCategoryBehavior;

public function behaviors()
{
    return [
        'ymlCategory' => [
            'class' => YmlCategoryBehavior::className(),
            'scope' => function ($model) {
                /** @var \yii\db\ActiveQuery $model */
                $model->select(['id', 'name', 'parent_id']);
            },
            'dataClosure' => function ($model) {
                /** @var self $model */
                return [
                    'id' => $model->id,
                    'name' => $model->name,
                    'parentId' => $model->parent_id
                ];
            }
        ],
    ];
}

Configure Offer models

https://yandex.ru/support/products/offers.html

Add behavior in the AR models, for example:

use corpsepk\yml\behaviors\YmlOfferBehavior;
use corpsepk\yml\models\Offer;

public function behaviors()
{
    return [
        'ymlOffer' => [
            'class' => YmlOfferBehavior::className(),
            'scope' => function ($model) {
                /** @var \yii\db\ActiveQuery $model */
                $model->andWhere(['is_deleted' => false]);
            },
            'dataClosure' => function ($model) {
                /** @var self $model */
                return new Offer([
                    'id' => $model->id,
                    'url' => $model->getUrl(true), // absolute url e.g. http://example.com/item/1256
                    'price' => $model->getPrice(),
                    'currencyId' => 'RUR',
                    'categoryId' => $model->category_id,
                    'picture' => $model->cover ? $model->cover->getUrl() : null,
                    /**
                     * Or as array
                     * don't forget that yandex-market accepts 10 pictures max
                     * @see https://yandex.ru/support/partnermarket/picture.xml
                     */
                    'picture' => ArrayHelper::map($model->images, 'id', function ($image) {
                        return $image->getUrl();
                    }),
                    'name' => $model->name,
                    'vendor' => $model->brand ? $model->brand->name : null,
                    'description' => $model->description,
                    'customElements' => [
                        [
                            'outlets' => '<outlet id="1" instock="30" />'
                        ]
                    ],
                    'condition' => new \corpsepk\yml\dto\Condition(
                        type: \corpsepk\yml\enums\ConditionType::PREOWNED,
                        quality: \corpsepk\yml\enums\ConditionQuality::EXCELLENT,
                        reason: 'Some scratches',
                    )
                ]);
            }
        ],
    ];
}

Testing

./vendor/bin/phpunit

Howto

Use console command to build yml

Useful links

Yandex XML validator - https://webmaster.yandex.ru/tools/xml-validator/