首页 > 解决方案 > 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'}"

谢谢你。

标签: loopsprestashop

解决方案


默认情况下,您将仅在$subcategories变量中获得子类别数组;$subcategory_object->getProducts('1','1','100','price','asc')因此,除非您确实有子类别对象,否则获取该子类别的产品将永远不会起作用。

首先,您需要获取子类别对象,为此您需要通过覆盖Category 类来覆盖文件中的默认函数getSubCategoriesclasses/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.php2)从文件夹中删除文件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}

希望这对你有用。


推荐阅读