How to add image during programmatic product import in prestashop?

社会主义新天地 提交于 2019-12-07 18:16:36

问题


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 />';
    }
} 

回答1:


This is implemented with the following method:

AdminImportControllerCore::copyImg()

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




回答2:


From where do you take $xml->products? Aren't you using the Web Service?

Product image is another object like product, and first you should create a new imagen and then adding this image id to product.




回答3:


You can see here as I do. Look at the line 209 which is where the code starts to add the new image.

https://github.com/xabikip/PrestaShopWebService/blob/master/examples/createProduct.php



来源:https://stackoverflow.com/questions/28447131/how-to-add-image-during-programmatic-product-import-in-prestashop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!