pretashop: add and update products programmatically

I'm working on a project where I need to add products from our ERP System to a Prestashop Catalog, and need to regularly mass update (10,000 products) some information on specific product (Pricing, Stock Information etc...)

Current Situation:

  • I've managed to create script to import from a CSV and add the files to the DB using "$object = new Product();

First Problem

Every time I run the script the products get created again. I need to create some sort of IF, that when the product exists it does not create it again.

Here's my solution:

$reference = $insert['reference'] ; #This Reference is the SKU that comes from my ERP
$sql = 'SELECT id_product FROM `'._DB_PREFIX_.'product` WHERE reference="'.$reference.'" ';
$res = Db::getInstance()->getValue($sql);

if (!empty($res))

{echo "The product already exist \n\n"};

Else  {echo "Starting to create the product";

 

Second Problem

The second problem is that I need to be able to update the items which are already created. I believe I'll use as script similar as the following:

$productObj = new Product($id);
$productObj->setFieldsToUpdate(array('price'));
$productObj->price = "12345";
$productObj->save();

 

The problem here is that I need to get the product_id to Update buy I only have the Reference...

Leave a Reply

Your email address will not be published. Required fields are marked *