Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prestashop add images to products

Tags:

php

prestashop

I have a product object, which I am creating in a PHP script. I need to add a thumbnail and a large image, which I have in a zip file. The file name contains the product ID.

Whats the best way to achieve this in code? I'm assuming I need to extract the images to somewhere in file system, but I have no idea how prestashop handles images.

thanks!

like image 462
Jay Avatar asked Nov 22 '13 19:11

Jay


People also ask

How do I upload images to PrestaShop?

Store Manager for PrestaShop is empowered with the useful ability to add images using drag&drop. Basically, all you need to do is to press “Add image” button, browse through the folders of your local computer, click on the necessary image(s) and move them to the place where product images can be previewed.

Where does PrestaShop store product images?

there are all product images, especially in subdirectories /img/p/1/ img/p/2/ img/p/3/ etc.

How do I change my image in PrestaShop?

To change the image, click the Browse button under the Image field. Select your new image from your local computer. Once you have selected the new image, click on the Save button to activate it.


1 Answers

If you have the Product ID ($id_product) and the image URL ($url), you can do the following:

$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, $url, 'products', false))
    {
        $image->delete();
    }
}
like image 73
PrestaShopDeveloper Avatar answered Oct 04 '22 13:10

PrestaShopDeveloper