corpsepk/yii2-yandex-market-yml Yii2 module for automatically generation Yandex.Market YML

moduleymlyandex.market

Yandex.Market YML Module for Yii2

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/

Changelog

0.8 (2024-04-09)

Added

  • Offer->condition

Changed

  • Minimum php version - 8.1
  • Temporary remove .github/workflows

0.7 (2020-12-22)

Added

  • Offer->customElements

Changed

  • Switch Travis-ci on GitHub actions

0.6 (2018-12-19)

Changed

  • Replace constant YmlOfferBehavior::BATCH_MAX_SIZE with public property batchMaxSize
  • Render items even if they contain errors (4c0292d)

0.5 (2018-12-19)

Added

  • Visualize errors on YII_ENV_DEV Resolve: #11 (4296d1f)

0.4 (2018-10-17)

Breaking changes

  • Rename param Offer->sale_notes => sales_notes. Close #9 (b0ca7bb)

Added

  • .travis.yml added php7.1 (17a8c5b)

0.3.2 (2017-06-01)

Changed

  • "vendor" is not required element (e84880d)

Fixed

  • fixed OfferModelTest::testValidateVendor() (0064aa3)
  • YandexMarketYmlModuleTest fixed (c06204d)

Statistics

Downloads
GitHub Stars
GitHub Forks

Releases

  • 0.809 April 2024
  • 0.721 December 2020
  • 0.619 December 2018
  • 0.519 December 2018
  • 0.4.117 October 2018
  • 0.417 October 2018
  • 0.3.201 June 2017
  • 0.3.106 March 2017
  • 0.309 February 2017
  • 0.213 November 2016

Comments



0.8 is the latest of 10 releases



MIT license
Stats
15 github stars & 5 github forks
2 downloads in the last day
80 downloads in the last 30 days
7985 total downloads