Prestashop image URL

Karam Abo Ghalieh picture Karam Abo Ghalieh · Jun 25, 2015 · Viewed 25.4k times · Source

I am trying to get image URL for prestashop product depending on it's ID. I am trying this code for methods provided by prestashop, but I get nothing

<?php

//require('config/settings.inc.php');
//require('classes/Image.php');
require('classes/Link.php');
$ItemID=$_GET["catID"];
$db=mysql_connect(constant('_DB_SERVER_'),constant('_DB_USER_'), constant


$image = Image::getCover($ItemID);
$imagePath = Link::getImageLink($product->link_rewrite, $image['id_image'], 'home_default');

this code is not working, I tried to add the required classes but they also produce a problem. Also if I commented last two line the require itself produces an error. My other working code that I am planing to build a table of images URL manually is working

<?php
require('config/settings.inc.php');
$ItemID=$_GET["catID"];
$db=mysql_connect(constant('_DB_SERVER_'),constant('_DB_USER_'), constant

('_DB_PASSWD_')) or die('Could not connect');
mysql_select_db(constant('_DB_USER_'), $db) or die('');
 if (is_numeric($ItemID)) {
$result = mysql_query("SELECT name,date_add,price , IFNULL(imageURL,'') as imageURL 

FROM product inner join product_lang on product_lang.id_product=product.id_product 

left join app_product_image on product.id_product=app_product_image.id_product where 

id_lang=1 and product.id_product='$ItemID'") or die('Could not query');
$json = array();
    if(mysql_num_rows($result)){
           // $row=mysql_fetch_assoc($result);
        while($row=mysql_fetch_row($result)){
            //  cast results to specific data types

            //$test_data[]=$row;
                $json['name']=$row[0];
                $json['date']=$row[1];
                $json['price']=$row[2];
                $json['imgURL']=$row[3];

        }
       // $json['products']=$test_data;
    }
   echo json_encode($json);
    mysql_close($db);


 }

the above code is the last solution but i am trying not to write the product-image URLs manually!

Answer

Vipul Hadiya picture Vipul Hadiya · Jun 29, 2015

Here is the tested code, I put this code in test.php under root directory of my prestashop installation and it works awesome.

<?php
require_once dirname(__FILE__).'/config/config.inc.php';
require_once dirname(__FILE__).'/init.php';
$id_product = 1;//set your product ID here
$image = Image::getCover($id_product);
$product = new Product($id_product, false, Context::getContext()->language->id);
$link = new Link;//because getImageLInk is not static function
$imagePath = $link->getImageLink($product->link_rewrite, $image['id_image'], 'home_default');
echo $imagePath;

From your code you should be aware that getImageLink is not static function and your first block of code snippet $product is not initialized with any product.

For confirmation I have uploaded this file on my server you can check it here: Click here