2013-05-15 205 views
1

需要您的幫助!如何通過產品ID獲取圖像路徑?獲取圖像路徑prestashop 1.5

Link :: getImageLink() - $ name的第一個參數,我不知道?

$searchResults = Search::find((int)(Tools::getValue('id_lang')), $query, 1, 10, 'position', 'desc', true); 
foreach ($searchResults as &$product) 
{ 
    $product['product_link'] = $this->context->link->getProductLink($product['id_product'], $product['prewrite'], $product['crewrite']); 
    $imageid = Product::getCover($product['id_product']); 
    // there i have image ID, how get image path $imagepath? 
    $product['image'] = $imagepath; 
} 
die(Tools::jsonEncode($searchResults)); 

回答

3

看看Link::getImageLink()。這是你必須使用的方法來獲得產品圖像的網址。

$imagePath = Link::getImageLink($product['link_rewrite'], $product['id_product'], 'home_default'); // change the last parameters to get the size you need 

這種方法也是在模板中可用,如果你有相同的語法

{$link->getImageLink($product.link_rewrite, $product.id_product, 'home_default')}` 

編輯 我重讀你的問題,並意識到你知道需要它。所以是第一個參數是 link_rewrite。希望這會有所幫助

0

要正確獲取產品圖像,請遵循以下代碼&正確傳遞參數(如果您遵循此代碼,啓用URL重寫時不會出現問題)。參數

{$link->getImageLink($name, $ids, $type = NULL) } 

細節都像

@param string $name rewrite link of the image 
@param string $ids id part of the image filename - can be "id_product-id_image" (legacy support, recommended) or "id_image" (new) 
@param string $type 

實例爲您

<img src="{$link->getImageLink($product.link_rewrite, $image.id_image, 'large_default')}" /> 
0

第1步:如果你有id_product,寫,讓你像這樣id_image屬性的功能:

private static function getCover($product_id) { 
    $sql = 'SELECT id_image FROM '._DB_PREFIX_.'image WHERE cover = 1 AND id_product='.$product_id; 
    $result = Db::getInstance()->getRow($sql); 
    return $result['id_image']; 
} 

第2步:

$id_image = self::getCover($product['id_product']); 
$image_path = $id_image.'-home_default/'.$product['link_rewrite'].'.jpg'; 

NB:然後用這樣的代碼獲取圖像路徑$產品是從執行SQL語句

得到一個數組