Jump to content

Adding images a product created from a module


redbull330

Recommended Posts

I am using prestashop version 1.4.4 and I want creating a module for creating a product with pictures. I am getting the fields from user using form than I try to create a product using product class like

$product = new Product();

$product->price = 10;

...

$product->add();

 

product created succesfully but after this how can add product images using Image class I am able to save image in temporary file on server but how can bing to the product? any help

Link to comment
Share on other sites

Hi Redbull,

 

Probably using the function: $product->addProductAttribute(...);

 

Defined as follows: addProductAttribute(($price, $weight, $unit_impact, $ecotax, $quantity, $id_images, $reference, $supplier_reference, $ean13, $default, $location = NULL, $upc = NULL, $minimal_quantity = 1)

 

Defined in file classes/Product.php (Definition of 1.4.6.2, but will probably be similar to your version)

 

Examples how to use it are in Product.php self and also in : /<your admin folder>/tabs/AdminImport.php

 

 

Hope this helps,

pascal

Edited by PascalVG
added definition correctly, added version number PS (see edit history)
Link to comment
Share on other sites

Thank you for the quick reply PascalVG

I don't have $id_images because the picture what I have in temp_file isn't a image object I have only a image in temp_file, I have searched but nothing found in image class. for example when a create a image object using Image class, how can assume a existing image path to image object than bing to a product as product thickbox image

 

$image = new Image();

$image->legend = array((int)(Configuration::get('PS_LANG_DEFAULT')) => $string);

$image->position = Image::getHighestPosition($product->id) + 1;

$image->cover = false;

$image->id_product = $product->id;

$image->path ? I could not find any variable in Image class that assume my existing image path to image objects path

 

any help?

Link to comment
Share on other sites

Hi Redbull,

 

Maybe it helps if you look in AdminImportController. There is a function as follows:

 

/**
 * copyImg copy an image located in $url and save it in a path
 * according to $entity->$id_entity .
 * $id_image is used if we need to add a watermark
 *
 * @param int $id_entity id of product or category (set in entity)
 * @param int $id_image (default null) id of the image if watermark enabled.
 * @param string $url path or url to use
 * @param string entity 'products' or 'categories'
 * @return void
 */
protected static function copyImg($id_entity, $id_image = null, $url, $entity = 'products')

 

This function is used to import images, when importing products using a CSV file. I assume you could use this function as well. In another function in this file you can see how it is used. Give it a try,

pascal

  • Like 3
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...