Jump to content
jmstyle008

[SOLVED]Add Images programmatically

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 . '")';

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Hi JM,

Glad you found the culprit!

I'll mark the topic as solved.

 

Would you mind to share the corrected SQL query, for educational purpose? :-)

Thanks,

pascal

  • Like 1

Share this post


Link to post
Share on other sites

$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();
    }
}

Share this post


Link to post
Share on other sites

 

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();
                }
            }

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More