简体   繁体   中英

trying to add a cover image to a product using api in prestashop 1.7.3.0

I have prestashop 1.7.3.0 installed and I'm trying to add a cover image to a product.

this is the class that I created with an addProductImage function

<?php

require_once _PS_ADMIN_CONTROLLER_DIR_ . 'AdminImportController.php';

class TuxInImage extends AdminImportControllerCore
{

    /**
     * @param $productId
     * @param $imgUrl
     * @param bool $regenerate
     * @throws PrestaShopDatabaseException
     * @throws PrestaShopException
     */
    public static function addProductImage($productId, $imgUrl, $isCover=false,$regenerate = true)
    {
        $img = new Image();
        $img->id_product = $productId;
        $img->cover=$isCover;
        $img->add();
        $imageId = $img->id;

        self::copyImg($productId, $imageId, $imgUrl, 'products', $regenerate);
    }
}

when I execute it, it adds the cover image. when I browse products on the admin panel.. I can properly see the image.

I noticed that when I open the website in my browser, it fails to load the images and it seems that they're missing some sort of a filename. it's looking for .jpg without first names.

for example: http://MY_SITE_URL/2510-home_default/.jpg

what am I missing ?

update

the code in the template that shows the product image:

 <img
            src = "{$product.cover.bySize.home_default.url}"
            alt = "{if !empty($product.cover.legend)}{$product.cover.legend}{else}{$product.name|truncate:30:'...'}{/if}"
            data-full-size-image-url = "{$product.cover.large.url}"
          >

and this is the product class that uses the image class to create the cover image for the product:

require_once __DIR__.DIRECTORY_SEPARATOR.'TuxInImage.php';

class TuxInProduct {

    private $heLangId;
    private $enLangId;

    function __construct($heLangId,$enLangId)
    {
        $this->heLangId=$heLangId;
        $this->enLangId=$enLangId;
    }

    function addProduct($categoryId, $carListNTypeId, $productHebrewName, $productEnglishName, $manufacturerNameHe, $manufacturerNameEn, $price,$imgPath) {
        $productObj = new Product();
        $productObj->id_category_default=$categoryId;
        $productObj->price=$price;
        $productObj->name=[$this->heLangId=>$productHebrewName,$this->enLangId=>$productEnglishName];
        $productObj->manufacturer_name=[$this->heLangId=>$manufacturerNameHe,$this->enLangId=>$manufacturerNameEn];
        $productObj->quantity=0;
        $productObj->add();

        $productObj->setWsCategories([$categoryId]);
        $productId = $productObj->id;
        TuxInImage::addProductImage($productId,__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'wind.jpg',true);
        ...
    }

}

update 2

output of var_dump for $product.cover :

array (size=9)
  'bySize' => 
    array (size=5)
      'small_default' => 
        array (size=3)
          'url' => string 'http://prestashop.ufk:8080/7410-small_default/.jpg' (length=50)
          'width' => int 98
          'height' => int 98
      'cart_default' => 
        array (size=3)
          'url' => string 'http://prestashop.ufk:8080/7410-cart_default/.jpg' (length=49)
          'width' => int 125
          'height' => int 125
      'home_default' => 
        array (size=3)
          'url' => string 'http://prestashop.ufk:8080/7410-home_default/.jpg' (length=49)
          'width' => int 250
          'height' => int 250
      'medium_default' => 
        array (size=3)
          'url' => string 'http://prestashop.ufk:8080/7410-medium_default/.jpg' (length=51)
          'width' => int 452
          'height' => int 452
      'large_default' => 
        array (size=3)
          'url' => string 'http://prestashop.ufk:8080/7410-large_default/.jpg' (length=50)
          'width' => int 800
          'height' => int 800
  'small' => 
    array (size=3)
      'url' => string 'http://prestashop.ufk:8080/7410-small_default/.jpg' (length=50)
      'width' => int 98
      'height' => int 98
  'medium' => 
    array (size=3)
      'url' => string 'http://prestashop.ufk:8080/7410-home_default/.jpg' (length=49)
      'width' => int 250
      'height' => int 250
  'large' => 
    array (size=3)
      'url' => string 'http://prestashop.ufk:8080/7410-large_default/.jpg' (length=50)
      'width' => int 800
      'height' => int 800
  'legend' => string '' (length=0)
  'cover' => string '1' (length=1)
  'id_image' => string '7410' (length=4)
  'position' => string '1' (length=1)
  'associatedVariants' => 
    array (size=0)
      empty

it seems that the false url of the image is missing the image id.

if I change http://prestashop.ufk:8080/7410-small_default/.jpg to http://prestashop.ufk:8080/7410-small_default/1.jpg that I see the first image in the db.

ok.. in this update I figured out mostly what's going on..

the url http://prestashop.ufk:8080/7410-small_default/1.jpg is false. because it doesn't matter what number I add before the .jpg . it ignores it. it just needs at least one character.

in the following rewrite rules:

# Images
RewriteRule ^([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$1$2$3.jpg [L]
RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$1$2$3$4.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$1$2$3$4$5.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg [L]
RewriteRule ^c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2$3.jpg [L]
RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L]

I needed to change all .+\\.jpg$ to .*\\.jpg$ for the rewrite to work on my images.

in the url http://prestashop.ufk:8080/7410-home_default/.jpg , 7410 is the image id, so I have no idea what data should be ignored before the .jpg .

now.. I'm guessing that I miss something and that is why I needed to change the rewrite rules in .htaccess. any ideas what ?

Array you are working with is generated here:

/Adapter/Image/ImageRetriever.php -> public function getImage($object, $id_image)

Function which is generating your URL is here:

/classes/Link.php -> public function getImageLink($name, $ids, $type = null)

I think you will find out now what's wrong :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM