Jump to content

Adding image to the product programmatically


gautamkakadiya

Recommended Posts

Hello Everyone,

 

I have added programmatically  product successfully but the image is not display to the particular product. Here is my code 

   

        $image = new Image();
        $id_image = Product::getCover($id_product);
        $image_url = "localhost/prestashop/img/2/3.jpg";
        $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())
        {
            $image->associateTo($shops);
            if (!self::copyImg($id_product, $image->id, $image_url, 'products', false))
            {
                $image->delete();
            }
        }

 

but the above code is not working properly. IT will not display image to the product. Please help me and suggest me where i have did the mistake.

 

Thanx in advance.

Edited by gautamkakadiya (see edit history)
Link to comment
Share on other sites

i guess you forget to define the static function copyImg() method on your file, so calling this method doesn't do anything

self::copyImg($id_product, $image->id, $image_url, 'products', false)

therefore your image file (which determined with var $image_url) is not saved, so it won't be displayed because it's not there

Link to comment
Share on other sites

Thanx for your replay.

 

in my above code i already added self::copyImg($id_product, $image->id, $image_url, 'products', false). but still the image is not display to the product. 

 

i don't know what should i do.. i don't have any idea about this how to add image to the product. if u have any other solution or u have code then please help me ...

 

 

Link to comment
Share on other sites

  • 5 months later...
  • 3 years later...
  • 4 years later...

Hii

try using the below function in your file

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

 

Edited by Hasnainraza Sundrani (see edit history)
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...