如何使用基于参考的 WebService 检查 prestashop 中是否已存在产品?

问题描述

我正在使用 Prestashop 1.7.7 的这个脚本(工作正常)在 prestashop 中创建一个产品(使用网络服务),但有时它会多次创建相同的产品..

我如何首先检查是否已经有一个值为“$product_barcode”的现有产品作为参考?

所以如果这个产品已经存在,我可以跳过这个。

//on récupère les infos relatives au produit
$product_model = $result[0]['product_model'];
$product_barcode = $result[0]['product_barcode'];
$product_price = $result[0]['product_price'];
$product_vat = $result[0]['product_vat'];

//on va rechercher le produit a modifier sur le prestashop
$parameters = array();

try {
    // creating webservice access
    $webService = new PrestaShopWebservice($url,'***********************',true);
 
    $xmlResponse = $webService->get(['url' => $url . '/api/products?schema=blank']);
    $productXML = $xmlResponse->product[0]; //Récupération du schéma de création
    $productXML->name->language[0] = $product_model;
    $productXML->description->language[0] = "A MODIFIER";
    $productXML->description->language[1] = "test de description FR";
    $productXML->price = $product_price;
    $productXML->reference = $product_barcode;
    $productXML->id_category_default = 2;
    $productXML->state = 1;//Cette valeur doit être à 1 pour que le produit soit visible dans les listings admin
    $productXML->id_tax_rules_group = $product_vat;
 
    //On ajoute également dans une catégorie
    //La première catégorie est déjà présente dans le schéma
    $productXML->associations->categories->category[0]->id = 30;
    
    //Envoi des informations au webservice
    $opt = ['resource' => 'products'];
    $opt['postXml'] = $xmlResponse->asXML();
    $return = $webService->add($opt);
    $id = $return->product->id;
    echo "Creation du produit $id <br />";
 
    } catch ( PrestaShopWebserviceException $e){
        echo $e->getMessage();
    }
}
?>

提前致谢

解决方法

请求 /api/products/?filter[reference]=[your_reference] 并检查是否返回任何产品。

您可以按照 https://devdocs.prestashop.com/1.7/webservice/tutorials/advanced-use/additional-list-parameters/

中的说明按其他字段进行过滤