jitheshkt Posted February 10, 2015 Share Posted February 10, 2015 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 More sharing options...
gu78 Posted November 24, 2015 Share Posted November 24, 2015 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 More sharing options...
LauraPresta Posted June 18, 2018 Share Posted June 18, 2018 Thank you for this, it is working. But really... overriding the whole AdminImportController.php only to be able to upload product image ? or asking the customer to modify his file ? damn.. No other solution found ? Link to comment Share on other sites More sharing options...
musakhani Posted July 11, 2018 Share Posted July 11, 2018 (edited) 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 July 11, 2018 by musakhani (see edit history) Link to comment Share on other sites More sharing options...
chale Posted May 20, 2020 Share Posted May 20, 2020 did you manage to find a solution? Link to comment Share on other sites More sharing options...
neuhs Posted November 4, 2020 Share Posted November 4, 2020 Hi, any solution for this? Link to comment Share on other sites More sharing options...
rrataj Posted November 4, 2020 Share Posted November 4, 2020 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 More sharing options...
neuhs Posted November 5, 2020 Share Posted November 5, 2020 Hi @rrataj, thanks for your reply. I am looking at passing an external image link for the product image part instead of having to manually upload the image and supplying the absolute image path. Any idea if that's possible? Link to comment Share on other sites More sharing options...
rrataj Posted November 5, 2020 Share Posted November 5, 2020 @neuhs Well, then simply download this image before: ... $url = 'http://example.com/'; $image_name = 'image.jpg'; $PATH_OF_THE_IMAGE = '/my/folder/' . $image_name; file_put_contents($PATH_OF_THE_IMAGE, file_get_contents($url . $image_name)); ... 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