phpprestashopprestashop-1.5

How to add image during programmatic product import in prestashop?


I can't find a proper documentation on adding images during product inserting. Here is the working code of my xml product import script. I have no idea how to add product images also while adding a product.

foreach ($xml->Products as $product_xml)
{
    if ($product_xml->Valid_internet_product == 1)
    {
        /* Update an existing product or Create a new one */
        $id_product = (int)Db::getInstance()->getValue('SELECT id_product FROM '._DB_PREFIX_.'product WHERE reference = \''.pSQL($product_xml->Reference).'\'');
        $product = $id_product ? new Product((int)$id_product, true) : new Product();
        $product->reference = $product_xml->Reference;
        $product->price = (float)$product_xml->Price;
        $product->active = (int)$product_xml->Active_product;
        $product->weight = (float)$product_xml->Weight;
        $product->minimal_quantity = (int)$product_xml->MinOrderQty;
        $product->id_category_default = 2;
        $product->name = utf8_encode($product_xml->Products_name);
        $product->description = utf8_encode($product_xml->Description);
        $product->description_short = utf8_encode($product_xml->Short_Description);
        $product->link_rewrite = Tools::link_rewrite($product_xml->Products_name);
        $product->image_url = 'http://i.imgur.com/jLThaBj.jpg';
        if (!isset($product->date_add) || empty($product->date_add))
            $product->date_add = date('Y-m-d H:i:s');
        $product->date_upd = date('Y-m-d H:i:s');
        $id_product ? $product->updateCategories(array(2)) : $product->addToCategories(array(2));


        $product->save();

        echo 'Product <b>'.$product->name.'</b> '.($id_product ? 'updated' : 'created').'<br />';
    }
} 

Solution

  • This is implemented with the following method:

    AdminImportControllerCore::copyImg()
    

    You can just copy/paste it, or change it if you need.