嗨,我刚刚开始新手。我的要求是将XML产品列表作为产品导入Prestashop。值得庆幸的是,prestashop的文档中已经提供了相同的示例文档。我只是复制粘贴并尝试了。可悲的是,它并没有奏效。我将复制粘贴我在下面尝试的代码和之后的错误消息。
PHP文件(位于prestashop的根目录中):
<?php
include(dirname(__FILE__).'/config/config.inc.php');
include(dirname(__FILE__).'/init.php');
$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Products>
<Reference>1101TEST</Reference>
<Valid_internet_product>1</Valid_internet_product>
<Products_name>Test product</Products_name>
<Price>49.99</Price>
<Active_product>1</Active_product>
<SupplierNo>8</SupplierNo>
<Weight>5</Weight>
<Description>My long product description</Description>
<Short_Description>Product desc.</Short_Description>
<MinOrderQty>1</MinOrderQty>
<Categories>
<Category>
<CategoryID>3</CategoryID>
<CategoryName>Home\Prod</CategoryName>
<Active_category>1</Active_category>
<Changed>0</Changed>
</Category>
</Categories>
<Tax_Class_ID>1</Tax_Class_ID>
<Discount>
<Discount_percentage>percentage</Discount_percentage>
<discountprice_ex_vat>0</discountprice_ex_vat>
<Discountprice_include_vat>0</Discountprice_include_vat>
<Pct_ReductionPercent>0</Pct_ReductionPercent>
</Discount>
</Products>
</Document>
XML;
$xml = simplexml_load_string($xml_string);
foreach ($xml->Products as $product_xml)
{
if ($product_xml->Valid_internet_product == 1)
{
/* Update an existing product or Create a new one */
$id_product = (int)Db::getInstance()->getValue('SELECT id_product FROM '._DB_PREFIX_.'product WHERE reference = \''.pSQL($product_xml->Reference).'\'');
$product = $id_product ? new Product((int)$id_product, true) : new Product();
$product->reference = $product_xml->Reference;
$product->price = (float)$product_xml->Price;
$product->active = (int)$product_xml->Active_product;
$product->weight = (float)$product_xml->Weight;
$product->minimal_quantity = (int)$product_xml->MinOrderQty;
$product->id_category_default = 2;
$product->name[1] = utf8_encode($product_xml->Products_name);
$product->description[1] = utf8_encode($product_xml->Description);
$product->description_short[1] = utf8_encode($product_xml->Short_Description);
$product->link_rewrite[1] = Tools::link_rewrite($product_xml->Products_name);
if (!isset($product->date_add) || empty($product->date_add))
$product->date_add = date('Y-m-d H:i:s');
$product->date_upd = date('Y-m-d H:i:s');
$id_product ? $product->updateCategories(array(2)) : $product->addToCategories(array(2));
$product->save();
echo 'Product <b>'.$product->name[1].'</b> '.($id_product ? 'updated' : 'created').'<br />';
}
}
错误信息是:
Fatal error: Uncaught exception 'PrestaShopException' with message 'Property Product->link_rewrite is empty'
我在循环中尝试了var_dump $ product-&gt; link_rewrite 1,它确实有字符串&#34; test-product&#34;。
我缺少什么?
完整错误消息的屏幕截图:
答案 0 :(得分:0)
就像你说的,在这种情况下,看到你安装了两种语言,你必须为所有语言设置链接重写:
进行此更改:
$languages = Language::getLanguages(true); // Get all the active languages
foreach(...){
/* ... */
/* change '$product->link[1] = ...' in this */
foreach($languages as $language)
{
$product->name[$language['id_lang']] = utf8_encode($product_xml->Products_name);
$product->description[$language['id_lang']] = utf8_encode($product_xml->Description);
$product->description_short[$language['id_lang']] = utf8_encode($product_xml->Short_Description);
$product->link[$language['id_lang']] = Tools::link_rewrite($product_xml->Products_name);
}
/* ... */
}
您无法知道prestashop保存的语言的确切ID,因此这是保存多语言字段的正确方法。
特别是在您的情况下,您必须为数组的索引设置2,因为您的语言具有ID 2,ID为1的语言被禁用,因此它是&#34;无用的&#34;并且您必须至少给出默认语言的字段。