corpsepk/yii2-yandex-market-yml

用于自动生成 Yandex.Market YML 的 Yii2 模块

0.8 2024-04-09 13:24 UTC

This package is auto-updated.

Last update: 2024-09-09 14:22:16 UTC


README

自动生成 Yandex.Market YML 的 Yii2 模块。

Latest Version Build Status Quality Score Minimum PHP Version

安装

安装此扩展的首选方式是通过 composer

  • 运行以下命令
php composer.phar require --prefer-dist "corpsepk/yii2-yandex-market-yml" "~0.8"

或者在您的应用程序的 composer.json 文件的 require 部分添加以下内容

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

配置配置

配置应用程序配置文件中的 cache 组件,例如

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

在应用程序配置文件的 modules 部分添加新的模块,例如

'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'],
        ],
    ],
],

在应用程序配置文件的 urlManager 部分添加新的规则,例如

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

配置 Category 模型

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

在 AR 类别模型中添加行为,例如

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
                ];
            }
        ],
    ];
}

配置 Offer 模型

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

在 AR 模型中添加行为,例如

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',
                    )
                ]);
            }
        ],
    ];
}

测试

./vendor/bin/phpunit

如何使用

使用控制台命令构建 yml

有用的链接

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