首先创建一个模块
使用模块生成器加快进程
在 https://validator.prestashop.com/generator 在线生成启动模块
1 下载文件
使用 curl 将文件下载到您的服务器
看到这个:https://*.com/questions/19248371/how-can-i-save-a-xml-file-got-via-curl
2 处理文件并插入/更新产品:
2.1) 通过 SQL 直接到 DB
2.2) 通过 PrestaShop API 或
2.3) 通过内部 Product Class 对象(推荐)
2.1 创建 PHP 脚本以加载 xml 并直接运行一些自定义 SQL 插入(您将需要处理图像上传并将所有图像插入数据库,这不是那么简单)。因此,您需要填充部分或全部这些表:
- ps_product
- ps_product_lang
- ps_product_shop
- ps_stock_available
- ps_category_product
- ps_image
- ps_product_download
...
2.2 使用 PrestaShop API,它将通过 URL 端点获取资源并在内部将其插入数据库。这样 PrestaShop 系统将为您处理很多事情,如果他们决定更改数据库中的某些内容(或负责上传您的图像和数据库关系),您将不必使用每个新的 PrestaShop 版本更新您的脚本
2.3 使用 Prestashop 内部 Product 类来插入新的产品 wo 数据库。
<?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();
Prestahop 导入脚本示例here
调查产品类here
处理大型 XML 文件
在大型 XML 文件上运行 php 时,不要使用 simplexml_load_file() 函数将所有文件加载到内存中。而是将 XMLReader 与 SimpleXMLElement 结合使用。
<?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);
}
/* 如果成功 */
$continueFrom = setLastNumWhereItStoped($continueFrom + $limit);
3 安排任务
设置 CRON 作业,自动运行脚本(下载 XML),然后在您真正需要更新的字段上运行更新。阅读您的主机提供商文档,了解如何做到这一点
DOCS PrestaShop API 端点
生成访问令牌
启用网络服务 默认情况下,PrestaShop 上的网络服务功能是禁用的,需要在第一次使用前打开。您可以使用 GUI 或以编程方式启用它。两种方法都在这里介绍:
https://devdocs.prestashop.com/1.7/webservice/tutorials/creating-access/
创建资源
v要创建资源,您只需获取资源的 XML 空白数据(例如 /api/someendpoint?schema=blank),用您的更改填充它,然后将整个 XML 作为正文内容的 POST HTTP 请求发送到 / api/someendpoint/ URL。
PrestaShop 将负责添加数据库中的所有内容,并返回一个指示操作已成功的 XML 文件以及新创建的客户的 ID。
更新资源
要编辑现有资源:获取要更改的资源的完整 XML 文件(例如 /api/someendpoint/1),根据需要编辑其内容,然后发送带有整个 XML 文件的 PUT HTTP 请求作为正文内容再次指向相同的 URL。
有用的资源:
https://devdocs.prestashop.com/1.7/webservice/
https://devdocs.prestashop.com/1.7/webservice/getting-started
https://devdocs.prestashop.com/1.7/modules/creation/external-services/
https://drib.tech/programming/parse-large-xml-files-php
使用数据库:
https://devdocs.prestashop.com/1.7/development/database/db/
https://devdocs.prestashop.com/1.7/development/database/structure/
图像是如何存储的 - 路径生成解释
Insert image to prestashop database
主题相当广泛,但希望这能帮助您入门。