我正在使用Prestashop。我在类别页面上获取产品的所有图像时遇到了麻烦。例如: -
我有一个名为“abc”的产品。 此产品有3张图片。一个是封面图像和另外两个图像。我想在类别页面上显示所有三个图像,目前它只显示封面图像。
答案 0 :(得分:1)
当我在我的一个客户项目中解决问题时。我还想在产品列表页面上显示所有图像。
我使用@Ravi Kumar回答的代码,并在模板文件中使用已分配的$ images变量,如下所示
<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>
下面我正在编写我在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);
}
答案 1 :(得分: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);
}
}
答案 2 :(得分:0)
好吧这似乎是一个非常普遍的问题,对于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;
}
}
第二:在任何.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缓存系统。 希望它可以帮助别人。
答案 3 :(得分:-1)
使用课程。在prestashop文件夹中,有一个名为override的文件夹。创建一个php文件,并尝试使用类覆盖。