2015-02-09 54 views
1

嗨,我只是新的開始。我的要求是將XML產品列表作爲產品導入Prestashop。謝天謝地,在prestashop的文檔中已經提供了相同的示例文檔。我只是複製粘貼並試用了它。可悲的是,它運作不佳。我將複製粘貼我在下面嘗試的代碼和之後的錯誤消息。將XML產品列表導入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 $產品 - > link_rewrite 1內環路和它有字符串「測試產品」。

我缺少什麼?

截圖完整的錯誤消息: enter image description here

+0

檢查您的商店是否安裝了其他語言,通常用於多語言字段prestashop使用索引等於ID語言的數組。因此,請嘗試執行以下操作:$ product-> link_rewrite = [...] – sarcom 2015-02-10 10:37:35

+0

Hi @SarcoM,我的商店有兩種語言安裝英語美國(禁用,ID:1)和英國英國(啓用,ID:2)。你能告訴我在上面的代碼段中我應該修改什麼來實現這個功能? – 2015-02-10 10:42:48

+0

試試我寫的解決方案:) – sarcom 2015-02-10 11:22:46

回答

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的語言被禁用,所以它是「無用的」,你必須至少給出默認語言。

+0

感謝您的解決方案。我已經嘗試在閱讀你的第一個答覆後嘗試將數組索引設置爲[2]。可悲的是,它沒有奏效。這一個也是一樣的。不工作。拋出幾乎相同的錯誤。 PS(註釋prestashop核心文件中的驗證部分使事情有效;)。數據插入,一切都很好) – 2015-02-10 12:49:38

+0

知道這個評論出來驗證它不是最終的解決方案,不,這不是一個解決方案。 :)。 – sarcom 2015-02-10 13:55:48

+0

還有一件事是上面的代碼沒有在Prestashop 1.6+ – 2015-02-11 04:56:33