2012-01-04 9 views
1

Prestashopを使用しています。カテゴリページで商品のすべての画像を取得する際に問題が発生しています。例えば: -カテゴリページで製品のすべてのイメージを取得する方法 - Prestashop

私は製品名が "abc"です。 この商品の3画像があります。 1つは表紙画像と2つの他の画像です。カテゴリページに3つの画像をすべて表示したいのですが、現在はカバー画像のみが表示されています。

答えて

-1

クラスを使用します。 Prestashopフォルダには、overrideという名前のフォルダがあります。 PHPファイルを作成し、クラスオーバーライドを使用してこれを試してください。

0
class CategoryController extends CategoryControllerCore 
{ 
    function __construct() { 
     parent::__construct(); 
    } 

    public function process() 
    { 
     parent::process(); 

     $productImages = array();     
     $newImages = array();     
     foreach($this->cat_products as $product) 
     { 
      $new_product = new ProductCore($product['id_product']); 
      $images = $new_product->getImages((int)self::$cookie->id_lang); 

      foreach ($images AS $k => $image) 
      { 
       $productImages[(int)$image['id_image']] = $image; 
      } 

      $newImages[$product['id_product']] = $productImages; 
      $productImages = null; 
      $new_product = null; 

     } 
     if (count($newImages)) 
      self::$smarty->assign('images', $newImages); 
    } 

} 
1

私のクライアントプロジェクトの1つで問題を解決していましたが、また、商品一覧ページにすべての画像を表示したいと思います。

は私がcategoryController.phpで書かれた私の機能を書いています下の

/** 
* Assign list of products template vars 
*/ 
public function assignProductList() 
{ 
    $hookExecuted = false; 
    Hook::exec('actionProductListOverride', array(
     'nbProducts' => &$this->nbProducts, 
     'catProducts' => &$this->cat_products, 
     'hookExecuted' => &$hookExecuted, 
    )); 


    // The hook was not executed, standard working 
    if (!$hookExecuted) 
    { 
     $this->context->smarty->assign('categoryNameComplement', ''); 
     $this->nbProducts = $this->category->getProducts(null, null, null, $this->orderBy, $this->orderWay, true); 
     $this->pagination((int)$this->nbProducts); // Pagination must be call after "getProducts" 
     $this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay); 
    } 
    // Hook executed, use the override 
    else 
     // Pagination must be call after "getProducts" 
     $this->pagination($this->nbProducts); 

    $productImages = array();     
    $newImages = array(); 

    foreach ($this->cat_products as &$product) 
    { 
     if ($product['id_product_attribute'] && isset($product['product_attribute_minimal_quantity'])) 
      $product['minimal_quantity'] = $product['product_attribute_minimal_quantity']; 

     $new_product = new ProductCore($product['id_product']); 
     $images = $new_product->getImages((int)self::$cookie->id_lang); 

     foreach ($images AS $k => $image) 
     { 
      $productImages[(int)$image['id_image']] = $image; 
     } 

     $newImages[$product['id_product']] = $productImages; 

     $productImages = null; 
     $new_product = null; 
    } 
    if (count($newImages)) 
       self::$smarty->assign('images', $newImages); 

    $this->context->smarty->assign('nb_products', $this->nbProducts); 
} 
+0

私は上記の解答を編集しました。特にスマートなコードです。 URL書き換えを有効にしている間に問題が発生していたためです。 イメージパスコードを取得するには '$ link-> getImageLink($ name、$ ids、$ type = NULL)' 最後にイメージパス 'src =" {$ link-> getImageLink($ product.link_rewrite、 $ imageIds、 'large_default')} "" –

0
ファイル@Raviクマーによって答えたコードを使用し、この

<ul id="thumbs_list_frame"> 
    {if isset($images[$product.id_product])} 
     {foreach from=$images[$product.id_product] item=image name=thumbnails} 
     {assign var=imageIds value="`$product.id_product`-`$image.id_image`"} 
     <li id="thumbnail_{$image.id_image}"> 

       <img id="thumb_{$image.id_image}" src="{$link->getImageLink($product.link_rewrite, $imageIds, 'large_default')}" alt="{$image.legend|htmlspecialchars}" alt="" title="{$product->name|escape:'htmlall':'UTF-8'}" width="{$largeSize.width}" height="{$largeSize.height}" /> 

     </li> 
     {/foreach} 
    {/if} 
</ul> 

のようなテンプレートファイルに割り当てられた$画像変数で働いていました

これは非常に一般的な問題であると思われます。プレstashopには新しく、同じ問題に対処していますが、私は多くの似た要求を見つけましたし、解決策もないので、これはどうしたらいいのですか?

まず:Productクラスをオーバーライドする、作成/override/classes/Product.php

class Product extends ProductCore { 
    /** 
    * @param string $id_product ID of the product to fetch 
    * @param bool $exclude_cover Whether to remove or not the cover from the returned list 
    * @return array List of the product images 
    * @throws PrestaShopDatabaseException 
    */ 
    public static function getProductImagesID($id_product, $exclude_cover = false) { 
     $id_image = Db::getInstance()->executeS('SELECT `id_image` FROM `' . _DB_PREFIX_ . 'image` WHERE `id_product` = ' . (int)($id_product) . ($exclude_cover ? ' AND `cover` IS NULL' : '') . ' ORDER BY position ASC'); 
     return $id_image; 
    } 
} 

第二:あなたはどんなの.tplファイルに望むようはそれを使用

... 
{assign var='productImgs' value=Product::getProductImagesID($product.id_product,true)} 
{* now you have an array of product images called $productImgs *} 
{* eg. show the first image of the product that is not saved as cover *} 
<img class="..." src="{$link->getImageLink($product.link_rewrite, $productImgs[0]['id_image'], 'home_default')|escape:'html':'UTF-8'}" alt="..."/> 

フィニッシュ!

ボーナスは、コントローラーファイルからデータベースにアクセスすることは意味がありますが、クラスをオーバーライドするとプラットフォームのアップグレードが不要になります。

キャッシュファイル/cache/class_index.phpを削除し、すべてのプリスタッシュキャッシングシステムを無効/有効にすることを忘れないでください。 他人を助けてくれることを願っています。

関連する問題