<?php
require_once('api/Simpla.php');
$simpla = new Simpla();
header("Content-type: text/xml; charset=UTF-8");
print (pack('CCC', 0xef, 0xbb, 0xbf));
// Заголовок
print
"<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE yml_catalog SYSTEM 'shops.dtd'>
<yml_catalog date='".date('Y-m-d H:i')."'>
<shop>
<name>".$simpla->settings->site_name."</name>
<company>".$simpla->settings->company_name."</company>
<url>".$simpla->config->root_url."</url>
";
// Валюты
$currencies = $simpla->money->get_currencies(array('enabled'=>1));
$main_currency = reset($currencies);
print "<currencies>
";
foreach($currencies as $c)
if($c->enabled)
print "<currency id='".$c->code."' rate='".$c->rate_to/$c->rate_from*$main_currency->rate_from/$main_currency->rate_to."'/>
";
print "</currencies>
";
// Категории
$categories = $simpla->categories->get_categories();
print "<categories>
";
foreach($categories as $c)
{
print "<category id='$c->id'";
if($c->parent_id>0)
print " parentId='$c->parent_id'";
print ">".htmlspecialchars($c->name)."</category>
";
}
print "</categories>
";
// Товары
$simpla->db->query("SET SQL_BIG_SELECTS=1");
// Товары
$simpla->db->query("
SELECT v.price, v.id AS variant_id, p.name AS product_name, v.name AS variant_name, v.position AS variant_position, p.id AS product_id, p.url, p.body, pc.category_id, i.filename AS image, brembo.*, p.brand_id, brands.name AS brand_name
FROM s_variants v
LEFT JOIN s_products p ON v.product_id = p.id
LEFT JOIN s_products_categories pc ON p.id = pc.product_id
AND pc.position = (
SELECT MIN( position )
FROM s_products_categories
WHERE product_id = p.id
LIMIT 1 )
LEFT JOIN s_images i ON p.id = i.product_id
AND i.position = (
SELECT MIN( position )
FROM s_images
WHERE product_id = p.id
LIMIT 1 )
INNER JOIN brembo brembo ON p.id = brembo.product_id
INNER JOIN s_brands brands ON brands.id = p.brand_id
WHERE p.visible
AND (
v.stock >0
OR v.stock IS NULL
)
GROUP BY v.id ORDER BY p.id, v.position");
print "<offers>";
$currency_code = reset($currencies)->code;
// В цикле мы используем не results(), a result(), то есть выбираем из базы товары по одному,
// так они нам одновременно не нужны - мы всё равно сразу же отправляем товар на вывод.
// Таким образом используется памяти только под один товар
$prev_product_id = null;
while($p = $simpla->db->result())
{
$variant_url = '';
if ($prev_product_id === $p->product_id)
$variant_url = '?variant='.$p->variant_id;
$prev_product_id = $p->product_id;
$price = round($simpla->money->convert($p->price, $main_currency->id, false),2);
print
"
<offer id='$p->variant_id' available='true'>
<url>".$simpla->config->root_url.'/products/'.$p->url.$variant_url."</url>";
print "
<price>$price</price>
<currencyId>".$currency_code."</currencyId>
<categoryId>".$p->category_id."</categoryId>
";
if($p->image)
print "<picture>".$simpla->design->resize_modifier($p->image, 200, 200)."</picture>
";
print "<vendor>".htmlspecialchars(trim(strip_tags($p->brand_name)))."</vendor>
";
print "<name>".htmlspecialchars($p->product_name).($p->variant_name?' '.htmlspecialchars($p->variant_name):'')."</name>
";
print "<model>".htmlspecialchars(trim(strip_tags($p->brand_name))).' '.htmlspecialchars(trim(strip_tags($p->{'Артикул производителя'})))."</model>
";
print "<sales_notes>Минимальный заказ 1 единица.</sales_notes>
";
print "<description>
".htmlspecialchars(strip_tags($p->body))."
</description>
";
foreach ($p as $pa=>$ram)
{
if($ram &&
$pa!="price" &&
$pa!="variant_id" &&
$pa!="product_name" &&
$pa!="variant_name" &&
$pa!="variant_position" &&
$pa!="product_id" &&
$pa!="url" &&
$pa!="body" &&
$pa!="category_id" &&
$pa!="image" &&
$pa!="brand_name" &&
$pa!="brand_id"
)
print "<param name='".htmlspecialchars(strip_tags(trim($pa)))."'>".htmlspecialchars(strip_tags(trim($ram)))."</param>
";
}
print "</offer>
";
}
print "</offers>
";
print "</shop>
</yml_catalog>
";