loops - Prestashop - 产品图片未显示在具有友好 URL 的类别循环中
问题描述
启用友好 URL 后,我的产品图片未按类别显示。如果我关闭它,它们会再次出现。
我努力了 :
- 在后台重新生成图片
- 关闭缓存
当我检查带有友好 URL 的代码时,图像链接以一种奇怪的格式显示:https ://www.website.fr/3689-home_default/.jpg
Prestashop 1.6
编辑 :
看来问题来自我在自定义类别 TPL 中查询产品的方式。我正在手动查询子类别中的产品(我不是一次显示该类别的所有产品),如下所示:
{foreach from=$subcategories item=subcategory}
{if $subcategory.id_category == 64659}
{assign var="subcategory_id" value=$subcategory.id_category}
{assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
{include file="./product-list.tpl" products=$subcategory_object->getProducts('1','1','100','price','asc')}
{/if}
{/foreach}
当我使用以下行查询产品时,图像显示正常:
{include file="./product-list.tpl" products=$products}
在 product-list.tpl 中,这是获取图像的行:
src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'home_default')|escape:'html':'UTF-8'}"
谢谢你。
解决方案
默认情况下,您将仅在$subcategories
变量中获得子类别数组;$subcategory_object->getProducts('1','1','100','price','asc')
因此,除非您确实有子类别对象,否则获取该子类别的产品将永远不会起作用。
首先,您需要获取子类别对象,为此您需要通过覆盖Category 类来覆盖文件中的默认函数getSubCategories
。classes/Category.php
按照下面提到的步骤(1-2)创建覆盖类并修改步骤(3)中提到的模板中的代码
1)Category.php
在路径上创建文件override\classes
并在其中添加以下代码。这将覆盖默认函数以获取类别。
<?php
/**
* @override classes/Category.php
*
*/
class Category extends CategoryCore
{
/**
* @override
* Return current category childs
*
* @param int $id_lang Language ID
* @param bool $active return only active categories
* @return array Categories
*/
public function getSubCategories($id_lang, $active = true)
{
$result = parent::getSubCategories($id_lang, $active);
foreach ($result as &$row) {
// Preapre object of sub category here
$row['object'] = new Category($row['id_category'], $id_lang);
}
return $result;
}
}
class_index.php
2)从文件夹中删除文件cache
。
您的覆盖功能现已准备就绪。
3)将以下代码添加到您的模板以显示产品
{foreach from=$subcategories item=subcategory}
{if $subcategory.id_category == 64659}
{include file="./product-list.tpl" products=$subcategory.object->getProducts('1','1','100','price','asc')}
{/if}
{/foreach}
希望这对你有用。
推荐阅读
- javascript - 对字符串列表进行排序并优先选择更接近起始索引的字符串
- arrays - 确保通过合并 2 个 bigint 数组创建的数组中的唯一元素
- c++ - boost_asio_handler_invoke_helpers::invoke 仍然是在 boost asio 1.70 中调用完成处理程序的受支持方式吗?
- c# - 从特定选择查询更新记录时了解实体框架
- php - 如何编辑 whmcs 模板导航栏?
- mysql - SQLITE - 匹配数字并生成具有单个值的另一行
- react-native - 如何在 React Native 中立即停止循环动画
- ionic-framework - 使用 mysql 数据库在 ionic 中托管 PWA
- django - 如何修复分配前引用的错误局部变量“ListQuery”
- azure - 无法将机器人发布到 cortana 频道