Skip to content

Instantly share code, notes, and snippets.

@vasia123
Created September 9, 2016 09:01
Show Gist options
  • Save vasia123/075b9ebb1bb285d774a9311acc724ec8 to your computer and use it in GitHub Desktop.
Save vasia123/075b9ebb1bb285d774a9311acc724ec8 to your computer and use it in GitHub Desktop.
YML export for miniShop2
<?php
$siteName = $modx->getOption('siteName', $scriptProperties, $modx->getOption('site_name')); // Имя сайта
$companyName = $modx->getOption('companyName', $scriptProperties, $modx->getOption('site_name')); // Компания
$bid = $modx->getOption('bid', $scriptProperties, 10); // Цена за клик
$siteUrl = $modx->makeUrl($modx->getOption('site_start'), '', '', 'full'); // Ссылка на главную страницу сайта
$descTpl = strip_tags($modx->getOption('descTpl', $scriptProperties, '[[+description]]')); // Шаблон для описания товара, без html
$paramCurrency = $modx->getOption('paramCurrency', $scriptProperties, 'RUR'); // Валюта
$paramDelivery = $modx->getOption('paramDelivery', $scriptProperties, true); // Доставка, по умолчанию - да. Можно указать имя параметра.
//////////////////////////////////////////////////////////////////////////////
// Заголовок выгрузки
$yml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$yml .= "<!DOCTYPE yml_catalog SYSTEM \"shops.dtd\">\n";
$yml .= "<yml_catalog date=\"".date('Y-m-d H:i')."\">\n";
$yml .= " <shop>\n";
$yml .= " <name>$siteName</name>\n";
$yml .= " <company>$companyName</company>\n";
$yml .= " <url>$siteUrl</url>\n";
// Курсы валют, приравнены к курсу ЦБРФ
$yml .= " <currencies>\n";
$yml .= " <currency id=\"RUR\" rate=\"1\"/>\n";
$yml .= " <currency id=\"USD\" rate=\"CBRF\"/>\n";
$yml .= " <currency id=\"EUR\" rate=\"CBRF\"/>\n";
$yml .= " </currencies>\n";
// Секция категорий
$categories = array();
$q = $modx->newQuery('msCategory', array('published' => 1, 'deleted' => 0, 'parent:!=' => 0, 'class_key' => 'msCategory'));
$q->select('id,parent,pagetitle');
$q->sortby('id','asc');
if ($q->prepare() && $q->stmt->execute()){
while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
$categories[$row['id']] = $row;
}
}
$yml .= " <categories>\n";
foreach ($categories as $v) {
$name = htmlspecialchars($v['pagetitle'], ENT_QUOTES, 'UTF-8');
if (isset($categories[$v['parent']])) {
$yml .= ' <category id="'.$v['id'].'" parentId="'.$v['parent'].'">'.$name.'</category>'."\n";
}
else {
$yml .= ' <category id="'.$v['id'].'">'.$name.'</category>'."\n";
}
}
$yml .= " </categories>\n";
// Секция описания товаров
if (is_string($paramDelivery)) {$delivery = ",`Data`.`$paramDelivery` as `delivery`";}
$q = $modx->newQuery('msProduct', array('published' => 1, 'deleted' => 0, 'class_key' => 'msProduct'));
$q->innerJoin('msProductData', 'Data', '`msProduct`.`id` = `Data`.`id`');
$q->select('`msProduct`.`id`,`parent`,`pagetitle`,`longtitle`,`description`,`introtext`');
$q->select('`Data`.`price`, `Data`.`image`'.$delivery);
$q->sortby('pagetitle','asc');
if ($q->prepare() && $q->stmt->execute()){
$goods = $q->stmt->fetchAll(PDO::FETCH_ASSOC);
}
$yml .= " <offers>\n";
foreach ($goods as $v) {
if (strpos($v['image'], '://') == false) {
if (strpos($v['image'], '/') === 0) {$v['image'] = substr($v['image'], 1);}
$image = $siteUrl.$v['image'];
}
else {$image = $v['image'];}
$image = strpos($v['image'], 'http') === 0 ? $v['image'] : $siteUrl.$v['image'];
$delivery = $paramDelivery == 'true' || $paramDelivery == 'false' ? $paramDelivery : $v['delivery'];
$pl = array('[[+pagetitle]]','[[+longtitle]]','[[+introtext]]');
$vl = array($v['pagetitle'],$v['longtitle'],$v['introtext']);
$description = htmlspecialchars(str_replace($pl, $vl, $descTpl), ENT_QUOTES, 'UTF-8');
$yml .= " <offer id=\"".$v['id']."\" available=\"true\" bid=\"$bid\">\n";
$yml .= " <url>".$modx->makeUrl($v['id'],'','','full')."</url>\n";
$yml .= " <price>".$v['price']."</price>\n";
$yml .= " <currencyId>".$paramCurrency."</currencyId>\n";
$yml .= " <categoryId>".$v['parent']."</categoryId>\n";
$yml .= " <picture>".$image."</picture>\n";
$yml .= " <delivery>".$delivery."</delivery> \n";
$yml .= " <name>".htmlspecialchars($v['pagetitle'], ENT_QUOTES, 'UTF-8')."</name>\n";
$yml .= " <description>".$description."</description>\n";
$yml .= " </offer>\n";
}
$yml .= " </offers>\n";
$yml .= " </shop>\n";
$yml .= "</yml_catalog>\n";
return str_replace('&', '&amp;', $yml);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment