Jump to content

[SOLVED]Add Images programmatically


jmstyle008

Recommended Posts

Hi I created a file to import all my product images from a directory and assigning them accordingly. The problem I've had is that in my DB my cover is set to 1 as its supposed to be while adding an image as the image cover. When i go to my front end and search for this product, i can see the image in the category list page but once i click on the product detail i don't get an image. I get the default Image. When i search for my product in the back end, i see the image and i notice that the cover icon is not set, i have to click on it and save in order to fix this issue. does any one else have an issue like this?

The qureies i have are...

 

$sql = 'INSERT INTO image (id_product, position, cover) values("' . $productId . '","' . $position . '","' . $cover . '")';

 

The following one is according to the id languages

 

$sql = 'INSERT INTO image_lang (id_image, id_lang) values("' . $idImage . '","' . $langId . '")';

 

for each store i have

$sql = 'INSERT INTO image_shop (id_image, id_shop,cover) values("' . $idImage . '","' . $idShop . '", "' . $cover . '")';

Link to comment
Share on other sites

You said that in the database the cover is correctly set to 1?

 

If so, it's probably a cache problem:

First try this:

Clear your browser cache and check again. Maybe you also have to reload the page with CTRL-F5 (windows)/Command-R (Mac)

 

If that doesn't do the trick, try this:

Temporarily turn off cache in Advanced Parameters->Performance and clear your browser cache once more.

 

See if that helps.

Pascal

Link to comment
Share on other sites

  • 1 year later...

$url='img/10.jpg';( this is your image both)

 

$image = new Image();

$image->id_product = $themeid;
$image->id_image=$themeid;
$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 (!AdminImportController::copyImg($id_product, $image->id, $url, 'products', false))
    {
         echo"img1234";
        $image->delete();
    }
}

Link to comment
Share on other sites

  • 4 years later...

 

This code is valid in prestashop 1.7.6

 

$id_product = 914;
            $url='http://domain.com/img/logo.jpg';
            $image = new Image();
            $image->id_product = $id_product;
            $image->position = 1;
            $image->cover = true; // or false;

            if (($image->validateFields(false, true)) === true &&
            ($image->validateFieldsLang(false, true)) === true && $image->add())
            {
    
                if (AdminImportController::copyImg($id_product, $image->id, $url, 'products', false))
                {
                   
                }else{
                    echo "fallido";
                    $image->delete();
                }
            }

 

Link to comment
Share on other sites

  • 1 year later...

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...