Jump to content

How to add image during programmatic product import ?


jitheshkt

Recommended Posts

Hi can some one explain me how to add images also while importing product programatically..?

Here is my working code of adding product programatically. Which filed is responsible for image ? I can't find a proper documentation on this :(

/* 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->link_rewrite = "new-prodo";
        /*var_dump($product);
        exit();*/
        $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();
Link to comment
Share on other sites

  • 9 months later...

I took the code from https://www.prestashop.com/forums/topic/269006-solvedadd-images-programmatically/

 

The only thing to do before is to edit or override AdminImportController::copyImg as it's a protected function. Just change it to public.

$image = new Image();
$image->id_product = $product->id;
$image->position = Image::getHighestPosition($product->id_product) + 1;
$image->cover = true;
if (($image->validateFields(false, true)) === true && ($image->validateFieldsLang(false, true)) === true && $image->add())
{
      if (!AdminImportController::copyImg($product->id_product, $image->id, 'http://i.imgur.com/jLThaBj.jpg', 'products', false))
      {
            $image->delete();
      }
}
Link to comment
Share on other sites

  • 2 years later...
  • 4 weeks later...

Hi

Thanks for your reply i tried more than 100 times but the image does not added to product

here is my code

<?php
require('../config/config.inc.php');
$product = new Product();
$product->ean13 = 9999999999999;
$product->name = "asdaf";
$product->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') =>  'test-importu');
$product->id_category = 2;
$product->id_category_default = 2;
$product->redirect_type = '404';
$product->price = 22;
$product->quantity = 1;
$product->minimal_quantity = 1;
$product->show_price = 1;
$product->on_sale = 0;
$product->online_only = 1;
$product->meta_keywords = 'test';
$product->is_virtual=1;
$product->add();
$product->addToCategories(array(2));

$shops = Shop::getShops(true, null, true);
$image = new Image();
$image->id_product = $id_product;
$image->position = Image::getHighestPosition($id_product) + 1;
$image->cover = true; // or false;
if (($image->validateFields(false, true)) === true &&
    ($image->validateFieldsLang(false, true)) === true && $image->add())
{
    echo"21414";
    $image->associateTo($shops);
    if (!AdminImportController::copyImg($id_product, $image->id, 'https://www.prestashop.com/forums/uploads/profile/photo-thumb-735924.jpg', 'products', false))
    {
        $image->delete();
    }
}
Edited by musakhani (see edit history)
Link to comment
Share on other sites

  • 1 year later...
  • 5 months later...

I use this function and code to assign image, maybe this will help you: ($PATH_OF_THE_IMAGE must be an absolute path to the file on disk.)

....
$image = new Image();
$image->id_product = (int) $product->id;
$image->position = Image::getHighestPosition($product->id) + 1;
$image->cover =  true;
$image->add();
if (!self::copyImg($product->id, $image->id, $PATH_OF_THE_IMAGE, 'products', true)) {
$image->delete();
}

....

public static function copyImg($id_entity, $id_image, $sourcePath, $entity = 'products', $regenerate = true) {
        $watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));

        switch ($entity) {
            default:
            case 'products':
                $image_obj = new Image($id_image);
                $path = $image_obj->getPathForCreation();
                break;
            case 'categories':
                $path = _PS_CAT_IMG_DIR_ . (int) $id_entity;
                break;
            case 'manufacturers':
                $path = _PS_MANU_IMG_DIR_ . (int) $id_entity;
                break;
            case 'suppliers':
                $path = _PS_SUPP_IMG_DIR_ . (int) $id_entity;
                break;
        }

        ImageManager::resize($sourcePath, $path . '.jpg');
        $images_types = ImageType::getImagesTypes($entity);

        if ($regenerate) {
            foreach ($images_types as $image_type) {
                ImageManager::resize($sourcePath, $path . '-' . stripslashes($image_type['name']) . '.jpg', $image_type['width'], $image_type['height']);
                if (in_array($image_type['id_image_type'], $watermark_types))
                    Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
            }
        }
        return true;
    }

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...