プレスタショップを利用しています。カテゴリ ページで商品のすべての画像を取得する際に問題が発生します。例:-
私は「abc」という名前の製品を持っています。この商品の画像は3枚です。1 つはカバー画像で、他の 2 つは画像です。カテゴリ ページに 3 つの画像すべてを表示したいのですが、現在は表紙の画像のみが表示されています。
プレスタショップを利用しています。カテゴリ ページで商品のすべての画像を取得する際に問題が発生します。例:-
私は「abc」という名前の製品を持っています。この商品の画像は3枚です。1 つはカバー画像で、他の 2 つは画像です。カテゴリ ページに 3 つの画像すべてを表示したいのですが、現在は表紙の画像のみが表示されています。
わかりました、これは非常に一般的な問題のようです。prestashop を初めて使用し、同じ問題に対処しています。オンラインで同様のリクエストがたくさん見つかりましたが、解決策はありませんでした。
最初: 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;
}
}
2番目:任意の.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を削除し、すべての prestashop キャッシュ システムを無効/有効にすることを忘れないでください。それが他の誰かを助けることを願っています。
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);
}
}
クラスを使用します。prestashop フォルダーには、override という名前のフォルダーがあります。php ファイルを作成し、クラスのオーバーライドを使用してこれを実行してみてください。