我正在尝试创建一个 PHP 文件,以便以 XML 格式显示我的所有产品。 PHP 文件位于 PS 安装的 ROOT 目录中。
您能告诉我如何在不向页面添加 html 和 body 标签的情况下初始化 PS 吗? 我还想获得产品的 PublicPrice 和 PriceWithoutReduction。
当我尝试此代码时,出现致命错误。 网址:http://www.topludo.fr/xml_guide.php
谢谢您的提前。
皮埃尔。
<?php
ini_set('display_errors', 'on');
header("Content-Type:application/xml");
include(dirname(__FILE__) . '/config/config.inc.php');
Context::getContext()->shop->setContext(Shop::CONTEXT_ALL);
echo '<?xml version="1.0" encoding="UTF-8"?>' . chr(10);
echo '<catalogue lang="FR" date="' . date('Y-m-d H:i') . '" GMT="+1" version="2.0">' . chr(10);
$id_lang = 1;
$front = false;
// Requête identifiant les produits disponibles dans le catalogue
echo $sql = 'SELECT p.*, pl.* , m.`name` AS manufacturer_name FROM `'._DB_PREFIX_.'product` p '.
' LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product`)'.
' LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)'.
' WHERE pl.`id_lang` = '.(int)$id_lang.' ORDER BY name ASC';
$rq = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
foreach ($rq as &$row)
$row = Product::getTaxesInformations($row);
$products_query = $rq;
$product_num = 0;
foreach($products_query as $key => $products) {
$product = new Product($products['id_product']);
$product_num++;
$categories = Product::getProductCategoriesFull($product->id, $id_lang);
$category = end($categories);
//$reducPrice = Product::getPriceStatic($products['id_product'], true, null, 0);
echo $product->getPublicPrice();
$fullPrice = ceil($product->getPriceWithoutReduct());
if ($fullPrice < $reducPrice) {
$discount_price = '';
$regular_price = $fullPrice;
$sale = 0;
} else {
$discount_price = $reducPrice;
$regular_price = $fullPrice;
$sale = 2;
}
$cover_id = $product->getCover($product->id); // L'image du produit
$link = new Link();
$cover_image_link = $link->getImageLink('large-default', $cover_id['id_image']);
$sp = SpecificPrice::getByProductId($product->id); // Pour les dates de reduc
$manufacturer = Manufacturer::getNameById($product->id_manufacturer); // Le fabricant
echo '<product place="' . $product_num . '">' . "\n";
echo '<merchant_category><![CDATA[' . $category['name'] . ']]></merchant_category>' . chr(10);
echo '<offer_id><![CDATA[' . $product->id . ']]></offer_id>' . chr(10);
echo '<name><![CDATA[' . $product->name[1] . ']]></name>' . chr(10);
echo '<description><![CDATA[' . substr(strip_tags(str_replace(array('<BR>', '<br>'), "</P>\n<P>", html_entity_decode($products['description']))), 0, 245) . '...]]></description>' . chr(10);
echo '<regular_price currency="EUR">' . $regular_price . '</regular_price>' . chr(10);
echo '<product_url><![CDATA[' . $product->getLink() . ']]></product_url>' . chr(10);
echo '<image_url><![CDATA[' . $cover_image_link . ']]></image_url>' . chr(10);
echo '<discount_price currency="EUR">' . $discount_price . '</discount_price>' . chr(10);
echo '<price_discounted_from><![CDATA[' . substr($sp[0]['from'], 0, 16) . ']]></price_discounted_from>' . chr(10);
echo '<price_discounted_until><![CDATA[' . substr($sp[0]['to'], 0, 16) . ']]></price_discounted_until>' . chr(10);
echo '<sales>' . $sale . '</sales>' . chr(10);
echo '<delivery currency="EUR">FR;0;</delivery>' . chr(10);
echo '<manufacter>'.''.'</manufacter>' . chr(10);
echo '<brand><![CDATA[' . $manufacturer . ']]></brand>' . chr(10);
echo '<model_number><![CDATA[' . $product->reference . ']]></model_number>' . chr(10);
echo '<manufacturer_product_id><![CDATA[]]></manufacturer_product_id>' . chr(10);
echo '<ean13>'.$product->ean13.'</ean13>' . chr(10);
echo '<guarantee unit="year">1</guarantee>' . chr(10);
echo '<used>0</used>' . chr(10);
echo '<used_condition><![CDATA[]]></used_condition>' . chr(10);
echo '<update_date><![CDATA[' . substr($product->date_upd, 0, 16) . ']]></update_date>' . chr(10);
echo '<promo_text><![CDATA[]]></promo_text>' . chr(10);
echo '<offer_valid_from><![CDATA[' . substr($sp[0]['from'], 0, 16) . ']]></offer_valid_from>' . chr(10);
echo '<offer_valid_until><![CDATA[' . substr($sp[0]['to'], 0, 16) . ']]></offer_valid_until>' . chr(10);
echo '<size unit="cm"></size>' . chr(10);
echo '<weight unit="kg">0.00</weight>' . chr(10);
echo '<color><![CDATA[]]></color>' . chr(10);
echo '</product>';
flush();
}
echo '</catalogue>';
要初始化 PrestaShop,请使用:
require_once __DIR__.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.inc.php';
require_once __DIR__.DIRECTORY_SEPARATOR.'init.php';
我建议使用
SimpleXML,而不是使用标头将内容类型设置为 xml,然后
echo
标记。操作 XML 变得更加容易,并且代码也更容易阅读。创建 SimpleXMLElement
并将所有产品属性添加为子项后,您可以使用 asXML
方法获取 XML 字符串,也可以通过将文件名参数传递给 asXML
将 XML 保存到文件中。