在 PHP 7.2 和 Prestashop 1.7 上导出 XML 多个值时出现问题
Problem exporting XML multiple values on PHP 7.2 & Prestashop 1.7
我正在尝试将产品从 Prestashop 1.7 导出到 XML。一切正常,但脚本多次导出产品,具体取决于所属类别。
例如 Product1 在 All products - Clothes - Men 中,这种情况将在 XML.
中导出 3 次
我尝试使用基于产品 ID 的 array_unique,但它不起作用。有人可以给我指明正确的方向吗?
完整代码可以在这里找到:https://pastebin.com/MQxfYSj2
在这部分代码中,我尝试使用 array_unique 但没有结果:
private function getProductFromArray($arrProduct){
$objProduct = new Okazii_Connector_Product();
$objProduct->ID = $arrProduct['id_product'];
$objProduct->UniqueID = $arrProduct['id_product'];
$objProduct->Title = $arrProduct['name'];
if (mb_strlen($arrProduct['description']) > 3){
$objProduct->Description = $arrProduct['description'];
} else if (mb_strlen($arrProduct['description_short'])) {
$objProduct->Description = $arrProduct['description_short'];
} else {
$objProduct->Description = $arrProduct['name'];
}
$objProduct->Amount = $arrProduct['quantity'];
if ($objProduct->Amount == 0){
$objProduct->Amount = $this->getAmountFromStock($arrProduct['id_product']);
}
$objProduct->Category = $this->getCategoryString($arrProduct['id_category']);
$objProduct->Currency = $this->getCurrency();
$objProduct->Price = $arrProduct['price'];
if(!empty($arrProduct['available_now']) && $objProduct->Amount > 0)
{
$objProduct->InStock = $arrProduct['available_now'];
}
if(!empty($arrProduct['gtin']))
{
$objProduct->GTIN = $arrProduct['gtin'];
}
else if(!empty($arrProduct['ean13']))
{
$objProduct->GTIN = $arrProduct['ean13'];
}
else if(!empty($arrProduct['isbn']))
{
$objProduct->GTIN = $arrProduct['isbn'];
}
$this->setProductImages($objProduct);
$this->setProductBrand($objProduct, $arrProduct['id_manufacturer']);
return $objProduct;
}
我还尝试在脚本顶部仅获取基于产品 ID 的唯一产品,但效果不佳。
其他有用信息:PHP 7.2,Prestashop 1.7.6.5
尝试在class like
的开头创建一个属性
private $exportedProductsIds = [];
然后在每次迭代后将导出的产品 ID 放入 return
之前
array_push($this->exportedProductsIds, $objProduct->id);
然后在运行之前添加验证如果id已经在$exportedProductsIds
然后跳过迭代
if (!in_array($objProduct->id, $this->exportedProductsIds)) {
//run your code
}
但我认为放置此代码的最佳位置是调用 getProductFromArray
方法的地方。
我假设是这样的
private function exportProducts($products)
{
foreach ($products as $product) {
if (in_array($objProduct->id, $this->exportedProductsIds)) {
continue;
}
if ($this->getProductFromArray($arrProduct)) {
array_push($this->exportedProductsIds, $objProduct->id);
}
}
}
我正在尝试将产品从 Prestashop 1.7 导出到 XML。一切正常,但脚本多次导出产品,具体取决于所属类别。 例如 Product1 在 All products - Clothes - Men 中,这种情况将在 XML.
中导出 3 次我尝试使用基于产品 ID 的 array_unique,但它不起作用。有人可以给我指明正确的方向吗?
完整代码可以在这里找到:https://pastebin.com/MQxfYSj2
在这部分代码中,我尝试使用 array_unique 但没有结果:
private function getProductFromArray($arrProduct){
$objProduct = new Okazii_Connector_Product();
$objProduct->ID = $arrProduct['id_product'];
$objProduct->UniqueID = $arrProduct['id_product'];
$objProduct->Title = $arrProduct['name'];
if (mb_strlen($arrProduct['description']) > 3){
$objProduct->Description = $arrProduct['description'];
} else if (mb_strlen($arrProduct['description_short'])) {
$objProduct->Description = $arrProduct['description_short'];
} else {
$objProduct->Description = $arrProduct['name'];
}
$objProduct->Amount = $arrProduct['quantity'];
if ($objProduct->Amount == 0){
$objProduct->Amount = $this->getAmountFromStock($arrProduct['id_product']);
}
$objProduct->Category = $this->getCategoryString($arrProduct['id_category']);
$objProduct->Currency = $this->getCurrency();
$objProduct->Price = $arrProduct['price'];
if(!empty($arrProduct['available_now']) && $objProduct->Amount > 0)
{
$objProduct->InStock = $arrProduct['available_now'];
}
if(!empty($arrProduct['gtin']))
{
$objProduct->GTIN = $arrProduct['gtin'];
}
else if(!empty($arrProduct['ean13']))
{
$objProduct->GTIN = $arrProduct['ean13'];
}
else if(!empty($arrProduct['isbn']))
{
$objProduct->GTIN = $arrProduct['isbn'];
}
$this->setProductImages($objProduct);
$this->setProductBrand($objProduct, $arrProduct['id_manufacturer']);
return $objProduct;
}
我还尝试在脚本顶部仅获取基于产品 ID 的唯一产品,但效果不佳。
其他有用信息:PHP 7.2,Prestashop 1.7.6.5
尝试在class like
的开头创建一个属性private $exportedProductsIds = [];
然后在每次迭代后将导出的产品 ID 放入 return
之前array_push($this->exportedProductsIds, $objProduct->id);
然后在运行之前添加验证如果id已经在$exportedProductsIds
然后跳过迭代
if (!in_array($objProduct->id, $this->exportedProductsIds)) {
//run your code
}
但我认为放置此代码的最佳位置是调用 getProductFromArray
方法的地方。
我假设是这样的
private function exportProducts($products)
{
foreach ($products as $product) {
if (in_array($objProduct->id, $this->exportedProductsIds)) {
continue;
}
if ($this->getProductFromArray($arrProduct)) {
array_push($this->exportedProductsIds, $objProduct->id);
}
}
}