Jump to content

Get image link of a product


Recommended Posts

Hello,

 

I'm trying to get the image links of a product in tpl.

I've followed this topic "https://www.prestashop.com/forums/topic/157163-product-image-url/" but I'm only half way.

 

I'm getting this "http://quartoscoimbra.pt/img/p/.jpg" instead of http://quartoscoimbra.pt/img/p/ "image_id" .jpg

 

Correct example would be "http://quartoscoimbra.pt/img/p/1/1.jpg" where "1/" is the name of the image id. 

 

This is my function in php,

public function hookDisplayHome(){        
        
        $productObj = new Product();

        $categories = new Smarty();

        $id_lang=Configuration::get('PS_LANG_DEFAULT');    

        $categories = Quartoscoimbra::getCategories($id_lang, true, false);

        $this->context->smarty->assign('all_categories' ,$categories);    

        $products = $productObj -> getProducts($id_lang, 0, 0, 'id_product', 'DESC' );

        $this->context->smarty->assign('all_product' ,$products);        
       
        $id_image = Product::getCover($products);
        // get Image by id
        if (sizeof($id_image) > 0) {
            $image = new Image($id_image['id_image']);
            // get image full URL
            $image_url = _PS_BASE_URL_._THEME_PROD_DIR_.$image->getExistingImgPath().".jpg";
        }
        
        $this->context->smarty->assign(array(
            'image_url' => $image_url,
        ));
        
        echo "<pre>";
        print_r($image_url);
        die();
       
    return $this->display(__FILE__, 'quartoscoimbra.tpl' );   
    }

This a debug foreach in tpl

{foreach from=$all_categories item=categories}  
            <b>{$categories.id_category}</b>
            {foreach from=$all_product item=product}
                    {if $product.id_category_default === $categories.id_category}
                        {$product.name}<br/>
                        {$product.id_product}<br/>
                        {$image_url}<br/>
                    {/if} 
            {/foreach}
        {/foreach}

All help appreciated.

Thanks in advance!

 

Best regards,

Bruno

Link to comment
Share on other sites

As far as I can see, you forgot to iterare over your products. Static getCover in the products class awaits a product id as parameter, no array.

Hello Imagetag,

 

Ok so I have an array "$products" that has all information of a product, but what you are saying is that I need something like this: $id_image = Product::getCover($id_product); right? I tried it but it says undifined variable :(

 

Thanks!

 

Best regards,

Link to comment
Share on other sites

I think {$image_url}  is a bit confused.

 

$img_url builds the path to the image directory, but it doesn't get me the folders after " p/ ", this folders are the image id.

For example if the image ID is 123 then the image will be stored under /p/1/2/3/123,jpg

Link to comment
Share on other sites

Which products do you want to display?

 

By the way, getProducts is a static function, so you could call it Product::getProducts(....). It returns an array of products, over which you could iterate, with foreach for example, and in this array the product id is behind the key "id_product".

 

P.S.

 

You could walk to this array by reference an add the url to your product array. The smarty assign has to move down to the end of the function, of course.

Edited by imagetag (see edit history)
Link to comment
Share on other sites

Which products do you want to display?

 

By the way, getProducts is a static function, so you could call it Product::getProducts(....). It returns an array of products, over which you could iterate, with foreach for example, and in this array the product id is behind the key "id_product".

 

P.S.

 

You could walk to this array by reference an add the url to your product array. The smarty assign has to move down to the end of the function, of course.

Woow thanks! I did it as you said, I foreach the array and search for id_product,

Like this:

foreach($products as $item) {
    $id_product = $item['id_product'];
}

Now if I print_r $image_url it shows me all the correct images for the product under the same ID.

Many thanks for the explanation imagetag :)

 

Best regards,

Bruno

Link to comment
Share on other sites

This brings me problem number 2,

 

This is my code

public function hookDisplayHome(){ 
        
        $productObj = new Product();

        $categories = new Smarty();

        $id_lang=Configuration::get('PS_LANG_DEFAULT');    

        $categories = Quartoscoimbra::getCategories($id_lang, true, false);

        $this->context->smarty->assign('all_categories' ,$categories);    

        $products = $productObj -> getProducts($id_lang, 0, 0, 'id_product', 'DESC' );

                
        foreach($products as $item) {
            $id_product = $item['id_product'];
            $id_image = Product::getCover($id_product);
            
            if (sizeof($id_image) > 0) {
                $image = new Image($id_image['id_image']);
                $image_url = _PS_BASE_URL_._THEME_PROD_DIR_.$image->getExistingImgPath().".jpg";
                $arraytest[] = $image_url;
            }
            foreach($products as $key => $we)
                {
                    $products[$key]['image_url'] = $arraytest;
                }
            echo $image_url;
        };
        
        $this->context->smarty->assign('all_product' ,$products);

If I echo $image_url inside the loop, it shows me all the image links available.

I added image_url to the products array, everything is okay, the problem is that $image_url is an array, so how can I say that $image_url corresponds to each product of the loop? For example:

 

This is a print_r of a product inside $products array

Array
(
    [0] => Array
        (
            [id_product] => 12
            [id_supplier] => 0
            [id_manufacturer] => 0
            [id_category_default] => 13
            [id_shop_default] => 1
            [id_tax_rules_group] => 0
            [on_sale] => 0
            [online_only] => 0
            [ean13] => 
            [upc] => 
            [ecotax] => 0.000000
            [quantity] => 0
            [minimal_quantity] => 1
            [price] => 0.000000
            [wholesale_price] => 0.000000
            [unity] => 
            [unit_price_ratio] => 0.000000
            [additional_shipping_cost] => 0.00
            [reference] => 
            [supplier_reference] => 
            [location] => 
            [width] => 0.000000
            [height] => 0.000000
            [depth] => 0.000000
            [weight] => 0.000000
            [out_of_stock] => 2
            [quantity_discount] => 0
            [customizable] => 0
            [uploadable_files] => 0
            [text_fields] => 0
            [active] => 1
            [redirect_type] => 404
            [id_product_redirected] => 0
            [available_for_order] => 1
            [available_date] => 0000-00-00
            [condition] => new
            [show_price] => 1
            [indexed] => 1
            [visibility] => both
            [cache_is_pack] => 0
            [cache_has_attachments] => 0
            [is_virtual] => 0
            [cache_default_attribute] => 0
            [date_add] => 2016-04-27 22:34:23
            [date_upd] => 2016-04-27 22:34:23
            [advanced_stock_management] => 0
            [pack_stock_type] => 3
            [id_shop] => 1
            [id_lang] => 1
            [description] => 
            [description_short] => 
            [link_rewrite] => quarto-para-filotropica
            [meta_description] => 
            [meta_keywords] => 
            [meta_title] => 
            [name] => Quarto para filotropica
            [available_now] => 
            [available_later] => 
            [manufacturer_name] => 
            [supplier_name] => 
            [rate] => 0
            [tax_name] => 
            [image_url] => Array
                (
                    [0] => http://quartoscoimbra.pt/img/p/.jpg
                    [1] => http://quartoscoimbra.pt/img/p/3/3.jpg
                    [2] => http://quartoscoimbra.pt/img/p/2/2.jpg
                    [3] => http://quartoscoimbra.pt/img/p/1/1.jpg
                )

        )

I want to be able to have this

Array
(
    [0] => Array
        (
            ...
            ...
            ...
            [tax_name] => 
            [image_url] => http://quartoscoimbra.pt/img/p/.jpg

        )
        [1] => Array
        (
            ...
            ...
            ...
            [tax_name] => 
            [image_url] => http://quartoscoimbra.pt/img/p/1/1.jpg

        )
........

)

Is this possible?

 

Thanks for your patience.

 

Best regards,

Bruno

Link to comment
Share on other sites

  • 6 years later...

In prestashop, product images are categorized in sizes. You can check BO for that.
You can retrieve images with class of ImageRetriever.

This class helps you to get images by sizes.

Example for PHP use:

$imageRetriever = new ImageRetriever($this->context->link);
$imageRetriever->getProductImages(['id_product' => $id_product], $this->context->language);

 

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