如何在分类页面上获取产品的所有图像-Prestashop
我正在使用Prestashop。在类别页面上获取产品的所有图像时遇到问题。例如:- 我有一种叫做“abc”的产品。如何在分类页面上获取产品的所有图像-Prestashop,prestashop,Prestashop,我正在使用Prestashop。在类别页面上获取产品的所有图像时遇到问题。例如:- 我有一种叫做“abc”的产品。 此产品有3张图片。一张是封面图片,另外两张是其他图片。我想在分类页面上显示所有三个图像,目前它只显示封面图像。使用类。在prestashop文件夹中,有一个名为override的文件夹。创建一个php文件,并尝试使用类重写来执行此操作。当我在一个客户机项目中解决问题时。我还想显示产品列表页面上的所有图像 class CategoryController extends Catego
此产品有3张图片。一张是封面图片,另外两张是其他图片。我想在分类页面上显示所有三个图像,目前它只显示封面图像。使用类。在prestashop文件夹中,有一个名为override的文件夹。创建一个php文件,并尝试使用类重写来执行此操作。当我在一个客户机项目中解决问题时。我还想显示产品列表页面上的所有图像
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);
}
}
我使用由@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>
好的,这似乎是一个非常常见的问题,作为prestashop的新手,处理相同的问题时,我在网上发现了很多类似的请求,但没有有效的解决方案,所以我就是这样让它工作的: 首先:覆盖产品类,创建/override/classes/Product.php Second:在任何.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="..."/>
。。。
{assign var='productImgs'value=Product::getProductImagesID($Product.id\u Product,true)}
{*现在您有了一个名为$productImgs*的产品映像数组}
{*例如,显示未保存为封面的产品的第一个图像*}
getImageLink($product.link_rewrite,$productImgs[0]['id_image'],'home_default')|转义:'html':'UTF-8'}alt=“…”/>
结束
另外,从控制器文件访问数据库应该是有意义的,但是重写类可以节省平台升级的时间
记住删除缓存文件/cache/class_index.php并禁用/启用所有prestashop缓存系统。
希望它能帮助其他人。我编辑了上面的答案,特别是smarty代码。因为启用URL重写时它产生了问题。获取图像路径代码是
$link->getImageLink($name,$ids,$type=NULL)
最后我得到了图像路径src=“{$link->getImageLink($product.link\u rewrite,$imageIds,'large\u default'))“
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;
}
}
...
{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="..."/>