gautamkakadiya Posted December 19, 2014 Share Posted December 19, 2014 (edited) 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 December 19, 2014 by gautamkakadiya (see edit history) Link to comment Share on other sites More sharing options...
gonebdg - webindoshop.com Posted December 19, 2014 Share Posted December 19, 2014 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 More sharing options...
gautamkakadiya Posted December 20, 2014 Author Share Posted December 20, 2014 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 More sharing options...
TheLousyCoder Posted May 28, 2015 Share Posted May 28, 2015 Hi Gautam, Did you find any solution adding image to the product? Will you please share any code to do so?? Regards Link to comment Share on other sites More sharing options...
LauraPresta Posted June 18, 2018 Share Posted June 18, 2018 same.. any solution finally ? Link to comment Share on other sites More sharing options...
Hasnainraza Sundrani Posted September 27, 2022 Share Posted September 27, 2022 (edited) 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 September 27, 2022 by Hasnainraza Sundrani (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now