在PHP中将XML产品导入到Prestashop
我需要创建一个PHP文件,将大量产品从外部源(分销商)导入我的Prestashop 1.7.6 我需要连接此服务“”以获取令牌,当我收到此字符串时,我需要连接此服务“”以接收XML文件 这是XML结构的一个示例:在PHP中将XML产品导入到Prestashop,php,xml,prestashop,Php,Xml,Prestashop,我需要创建一个PHP文件,将大量产品从外部源(分销商)导入我的Prestashop 1.7.6 我需要连接此服务“”以获取令牌,当我收到此字符串时,我需要连接此服务“”以接收XML文件 这是XML结构的一个示例: <![CDATA[ <product> <id>id<id> <description>description</description> <quantity>
<![CDATA[
<product>
<id>id<id>
<description>description</description>
<quantity>quantity</quantity>
<confezione>confezione</confezione>
<prezzo_lordo>prezzo acquisto senza sconti</prezzo_acquisto>
<price>price</price>
<info>info</info>
</product>
]]>
身份证件
描述
量
联盟
普雷佐收购senza sconti
价格
信息
]]>
问题有二:
- 如何用PHP语言连接此服务
- 当我拥有这个文件时,如何将XML代码导入我的Prestashop数据库
你能帮我解决这个问题吗
谢谢。我想你的想法是开发一个Prestashop模块并使用cron运行它,是这样吗 您是否尝试过使用curl创建XML文件 从Prestashop模块中,必须通过CURL将XML引入。加载XML后,只需解析(查看:)文件并使用Prestashop API处理每个产品 告诉我是否可以帮助您。首先创建一个模块 使用模块生成器加快该过程 [https://validator.prestashop.com/generator][1] 下载第1个文件 使用curl将文件下载到您的服务器 见此:https://stackoverflow.com/questions/19248371/how-can-i-save-a-xml-file-got-via-curl 2处理文件并插入/更新产品:
2.1)通过SQL直接到DB
2.2)通过PrestaShop API或
2.3)通过内部产品类对象(推荐) 2.1创建PHP脚本以加载xml并直接运行一些自定义SQL插入(您将需要处理图像上载并将所有图像插入数据库,这并不是那么简单)。因此,您需要填充部分或所有这些表:
- ps_产品
- ps_产品_lang
- 产品店
- ps_库存_可用
- ps\U类别产品
- ps_图像
- 产品下载
<?php
// add category first then make reference to category
// configure and add product
$product = new Product;
$product->name = $productName;
$product->ean13 = '';
$product->reference = '';
$product->id_category_default = $getCategoryID;
$product->category = $getCategoryID;
$product->indexed = 1;
$product->description = $description;
$product->condition = 'new';
$product->redirect_type = '404';
$product->visibility = 'both';
$product->id_supplier = 1;
$product->link_rewrite = $link_rewrite;
$product->quantity = $singleStock;
$product->price = round($price - (18.69 / 100) * $price, 2);
$product->active = 1;
$product->add();
请去阅读。这两个问题(一开始应该单独提出)都过于宽泛。这不是一个回答“如何做X”问题的教程网站——你需要自己付出更多的努力。
<?php
// this is not PrestaShop related script. This is pure PHP for manipulating large XML files.
// first load the file with curl and save it on your server in desired location. Then load the file as in example below:
$continueFrom = getLastNumWhereItStoped();
$iCount = 0;
$limit = 1000;
$xml = new XMLReader();
/*
* One-liners to gzip and ungzip a file:
* copy('file.txt', 'compress.zlib://' . 'file.txt.gz');
* copy('compress.zlib://' . 'file.txt.gz', 'file.txt');
*/
$xml->open('compress.zlib://'.'filename.xml.gz');
while($xml->read() && $xml->name != 'product')
{
// skip all not important nodes and stop on "product" node
}
/**
* Run on every "product" node untill it hits 1000
*/
while($xml->name == 'product' && $limit + $continueFrom >= $iCount)
{
if($iCount <= $continueFrom ) continue;
$element = new SimpleXMLElement($xml->readOuterXML());
$product = array(
'name' => strval($element->text->name),
'price' => strval($element->price->buynow),
'parent_category' => strval($element->category->attributes()->parent_category) // category have to be created before product import [maping category is as easy as you might think]
);
// ... do something with $product set create Product Class instance or... send it to API or make SQL insert directly
// if product exists just update the product value you want (for example price and stock quantity).
$iCount++;
$xml->next('product');
unset($element);
}