将产品目录从Prestashop 1.7导出到csv文件以在Facebook Shop上使用

问题描述

这是有效的,但是我对产品ID的要求不是全部按数字顺序排列,例如,我的产品ID像(1,2,3,4,5,7,10,12,13,14,等等...)

当脚本生成文件时,它将创建产品ID(1、2、3、4、5、6、7、8、9、10、11、12、13、14等),等等id表示没有产品信息,因此会创建一个空白的id行。

可以帮我解决这个问题吗?

Regard的

<?PHP
   // Connecting to database
   
   $con = MysqLi_connect('127.0.0.1','root','','prestahop');
   
   // check connection
   
   if(MysqLi_connect_errno())
   {
       echo "Failed to connect to MysqL : " .MysqLi_connect_errno();
           
   }
   
   
   $domain = "127.0.0.1/prestashop";
   
    $names = array();
    $descriptions = array();
    $limit = array();
    $skus = array();
    $prices = array();
     
   
   $stmt = $con->prepare("SELECT id_product,name,description FROM ps_product_lang where id_lang = 1");
   
   $stmt->execute();
   $stmt->store_result();
   //$stmt->bind_result($active,$id,$name,$description);
   $stmt->bind_result($id,$description);
   
   while($stmt->fetch())
   
   {
       
       //saving names and descriptions in associative array with id as key
       
       $names[$id] = $name;
       $descriptions[$id] = $description;
       
   }
   
   $stmt->close();
   
   $stmt = $con->prepare("SELECT id_product,reference,price FROM ps_product");
   $stmt->execute();
   $stmt->store_result();
   $stmt->bind_result($id,$reference,$price);
   
   while($stmt->fetch())
   
   {
       $limit[] = $id;
           
       //saving id,skus and price in associative array with id as key
       
       $ids[$id] = $id;
       $skus[$id] = $reference;
       $prices[$id] = $price;
   }
   
   $stmt->close();
   
   $fp = fopen("fbshop.csv","w");
   
   // setting column headers
   
   $fields = array(
   
                'id','title','description','availability','condition','price','link','image_link','brand','additional_image_link','age_group'
   
                );
     fputcsv($fp,$fields); 
     $count = 1;
   
     while($count < count($limit))
     {
       $lineData = array(
           
           $ids[$count],$names[$count],//$descriptions[$count],strip_tags($descriptions[$count]),'in stock','New',number_format((float)$prices[$count],'.',''),$domain . 'index.PHP?id_product=' . $ids[$count] . '&controller=product',$domain . $ids[$count] . '-large_default/' . str_replace(" "," ",strtolower($names[$count])) . '.jpg','BrandName',"-",'adult'
        );  
         
         fputcsv($fp,$lineData);
         $count++;
         
         
     }
   
   fclose($fp);
   
   ?>

解决方法

此脚本假定产品ID之间没有“漏洞” ...

为避免在CSV文件中写入不存在的ID,您可以在:之后紧接插入支票:

while($count < count($limit))
{

赞:

if (!isset($ids[$count])) {
    continue;
} 
,

也许您可以通过以下方式执行sql查询:

<?php

error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors','1');

$db = mysqli_connect('127.0.0.1','root','password','my_presta_bd');

if (mysqli_connect_errno()) {
    echo "Failed to connect to Mysql : " . mysqli_connect_errno();

}

$sql = "SELECT pp.id_product,pp.reference,pp.price,ppl.name,ppl.description FROM ps_product pp LEFT JOIN ps_product_lang ppl ON (ppl.id_product = pp.id_product) WHERE ppl.id_lang = 1;";
$query = mysqli_query($db,$sql);

然后,您可以将csv文件中的数据保存到while中:

while($product_list = mysqli_fetch_assoc($query)){

   
    $id_product     = $product_list['id_product'];
    $reference      = $product_list['reference'];
    $price          = $product_list['price'];
    $name           = $product_list['name'];
    $description    = $product_list['description'];
    
    echo $id_product.' - '.$reference.' '.$price.' '.$name.' '.$description.' <br>';
    
}

例如,产品ID 14:

$sql = "SELECT pp.id_product,ppl.description FROM ps_product pp LEFT JOIN ps_product_lang ppl ON (ppl.id_product = pp.id_product) WHERE ppl.id_lang = 1 AND pp.id_product <= 14;";

您还必须出口产品组合吗?

PD: 要仅获取有效产品,请执行sql查询:

SELECT pp.id_product,pp.active,ppl.description FROM ps_product pp LEFT JOIN ps_product_lang ppl ON (ppl.id_product = pp.id_product) WHERE ppl.id_lang = 1 AND pp.id_product <= 14 AND pp.active = 1