使用PHP将XML产品导入Prestashop 处理大型 XML 文件生成访问令牌创建资源更新资源有用的资源:

我需要创建一个PHP文件,以将许多产品从外部来源(分销商)导入Prestashop 1.7.6。

我需要连接到该服务“ http://www.ferrunion.com/ita/codice/id_service.php”以获取令牌,当我获取此字符串时,需要连接到该服务“ http://www.ferrunion.com/ita/codice/catalogo_service.php”以获取XML文件。

这是XML结构的示例:

<![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> 
]]>

问题是2:

  • 我该如何用PHP语言来介绍这项服务?
  • 有了文件后,如何将XML代码导入Prestashop的数据库中?


您能帮我解决这个问题吗?

谢谢。

shw8192935 回答:使用PHP将XML产品导入Prestashop 处理大型 XML 文件生成访问令牌创建资源更新资源有用的资源:

我猜你的想法是开发一个Prestashop模块并使用cron运行它,是吗?

您是否尝试过使用curl来携带XML文件?

必须从Prestashop模块中通过CURL带来XML。加载XML后,您只需解析(看看:https://www.w3schools.com/php/php_xml_dom.asp)文件并使用Prestashop API处理每个产品。

告诉我是否可以帮助您

,

首先创建一个模块

使用模块生成器加快进程

[https://validator.prestashop.com/generator][1]

1 下载第 3 个文件

使用 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_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 请求作为正文内容再次转到相同的网址。

有用的资源:

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

这个话题相当广泛,但希望这能让你开始。

本文链接:https://www.f2er.com/3050177.html

大家都在问