4

i have this xml file that is being generated by a stock management software i have that i would like to insert it into the Pretashop database. can anybody please help me with this xml file?

<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Products>
<Reference>1101TEST</Reference>
<Valid_internet_product>1</Valid_internet_product>
<Products_name>universal</Products_name>
<Price>69.95</Price>
<Active_product>1</Active_product>
<SupplierNo>08</SupplierNo>
<Weight>5</Weight>
<Description>Koncentreret universalmiddel med salmiak</Description>
<Short_Description>Koncentreret universalmiddel med salmiak</Short_Description>
<MinOrderQty>1</MinOrderQty>
<Categories>
<Category>
<CategoryID>63</CategoryID>
<CategoryName>Bins\Universal</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>
4

1 回答 1

4

使用 PrestaShop,您可以使用以下选项之一轻松导入数据:

  1. 通过在后台使用“CSV 导入”功能(假设您也有一个 .csv 导出而不是这个 XML 文件)
  2. 通过使用 PrestaShop Web 服务(需要定制开发)
  3. 通过使用现有的类(需要自定义开发)

下面是一个使用您的 XML 字符串的快速代码片段。它将创建或更新所有产品,并考虑它们的价格、可用性、名称、描述、重量等。

请注意:

  • 此代码不适用于组合(颜色、尺寸等)
  • 此代码不会为您创建类别,而是将所有产品添加到“主页”类别

这只是一个帮助你的例子;)

<?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>universal</Products_name>
<Price>69.95</Price>
<Active_product>1</Active_product>
<SupplierNo>08</SupplierNo>
<Weight>5</Weight>
<Description>Koncentreret universalmiddel med salmiak</Description>
<Short_Description>Koncentreret universalmiddel med salmiak</Short_Description>
<MinOrderQty>1</MinOrderQty>
<Categories>
<Category>
<CategoryID>63</CategoryID>
<CategoryName>Bins\Universal</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 />';
    }
}
于 2013-06-19T12:58:16.133 回答