overplex/yml-generator

YML (Yandex Market Language) file generator

v1.11.6 2022-04-17 22:09 UTC

README

Build Status Code Coverage Scrutinizer Code Quality License Latest Stable Version Total Downloads

About

YML (Yandex Market Language) generator. Uses standard XMLWriter for generating YML file. Not required any other library you just need PHP 5.5.0 or >= version.

Generator supports this offer types:

Installation

Run composer require

composer require overplex/yml-generator

Or add this to your composer.json file:

"require": {
  "overplex/yml-generator": "~1.11.6",
}

Usage examples

<?php

use Bukashk0zzz\YmlGenerator\Model\Offer\OfferParam;
use Bukashk0zzz\YmlGenerator\Model\Offer\OfferSimple;
use Bukashk0zzz\YmlGenerator\Model\Category;
use Bukashk0zzz\YmlGenerator\Model\Currency;
use Bukashk0zzz\YmlGenerator\Model\Delivery;
use Bukashk0zzz\YmlGenerator\Model\ShopInfo;
use Bukashk0zzz\YmlGenerator\Settings;
use Bukashk0zzz\YmlGenerator\Generator;
use Bukashk0zzz\YmlGenerator\Cdata;

// Create second (unbuffered) connection to database (only for Yii 2)
$unbufferedDb = new \yii\db\Connection([
    'dsn' => \Yii::$app->db->dsn,
    'charset' => \Yii::$app->db->charset,
    'username' => \Yii::$app->db->username,
    'password' => \Yii::$app->db->password,
    'tablePrefix' => \Yii::$app->db->tablePrefix,
]);
$unbufferedDb->open();
$unbufferedDb->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

// Writing header to yml.xml
$yml = new Generator((new Settings())
    ->setOutputFile('yml.xml')
    ->setEncoding('UTF-8')
);

// Writing ShopInfo (https://yandex.ru/support/partnermarket/elements/shop.html)
$yml->addShopInfo((new ShopInfo())
    ->setName('BestShop')
    ->setCompany('Best online seller Inc.')
    ->setUrl('http://www.best.seller.com/')
);

// Writing currencies (https://yandex.ru/support/partnermarket/elements/currencies.html)
$yml->startCurrencies();
$yml->addCurrency((new Currency())
    ->setId('USD')
    ->setRate(1)
);
$yml->addCurrency((new Currency())
    ->setId('RUR')
    ->setRate(1)
);
$yml->finishBlock();

// Writing categories (https://yandex.ru/support/partnermarket/elements/categories.html)
$yml->startCategories();
/** @var CategoryModel $category */
foreach ($this->getCategoriesQuery()->each(50, $unbufferedDb) as $category) {
    $item = new Category();
    $item->setId($category->id);
    $item->setName($this->formatText($category->name));

    if ($category->parent) {
        $item->setParentId($category->parent->id);
    }

    $yml->addCategory($item);
    gc_collect_cycles();
}
$yml->finishBlock();

// Writing offers (https://yandex.ru/support/partnermarket/offers.html)
$yml->startOffers();
/** @var ProductModel $product */
foreach ($this->getProductsQuery()->each(50, $unbufferedDb) as $product) {

    $offer = new OfferSimple();
    $offer->setId($product->id);
    $offer->setUrl($product->getViewAbsoluteUrl());
    $offer->setName($product->name);
    $offer->setPrice($product->getPrice());
    $offer->setOldPrice($product->getOldPrice());
    $offer->setAvailable($product->in_stock);
    $offer->setVendorCode($product->articul);
    $offer->setCurrencyId('RUR');
    $offer->setCount($product->stock);
    $offer->setWeight($product->weight);
    $offer->setDimensions($product->depth, $product->width, $product->height);
    $offer->setDescription(new CData($this->formatDescription($product->text)));
    $offer->addPicture($product->getMainPhotoAbsoluteUrl());

    if (!empty($manufacturer)) {
        $offer->addCustomElement('manufacturer', $manufacturer);
    }

    // Характеристики

    foreach ($product->properties as $value) {
        $offer->addParam((new OfferParam)->setName($value->property->name)->setValue($value->value));
    }

    $yml->addOffer($offer);
    gc_collect_cycles();
}
$yml->finishBlock();

// Optional writing deliveries (https://yandex.ru/support/partnermarket/elements/delivery-options.xml)
$yml->startDeliveries();
$yml->addDelivery((new Delivery())
    ->setCost(2)
    ->setDays(1)
    ->setOrderBefore(14)
);
$yml->finishBlock();

$yml->finish();
$unbufferedDb->close();

OZON example

$yml = new Generator((new Settings())
    ->setOutputFile('ozon.xml')
    ->setEncoding('UTF-8')
    ->hideDtd()
);

$yml->startOffers();

$offer = new OfferSimple();
$offer->setId('articul');
$offer->setPrice(100);
$offer->setOldPrice(150);
$offer->setInStock(1000);
$offer->hideAvailable();
$yml->addOffer($offer);

$yml->finishBlock();

$yml->finish();

Copyright / License

See LICENSE