Opencart 2 - 类别视图中的回声过滤器 ID/Description

Opencart 2 - Echo Filter ID/Description in Category View

我正在开发 Opencart 2 - 项目,需要在类别视图中分别回显每个产品的过滤器 ID(例如,作为产品缩略图的附加 Classes,以将不同的样式应用到vegan/vegetarian 食物)。

我已经折断手指好几个小时了,修改控制器、模型和视图文件,但无法找到解决方案……经过多次(事后看来愚蠢的)尝试,我终于卡在这里了:

在我的 catalog/view/theme/default/template/product/category.tpl 中,我为每个拇指回显 $product['filter_id']

<?php foreach ($products as $number=>$product) { ?>
<div class="product-thumb">
//...
<span class="filters"><?php echo $product['filter_id']; ?></span>
//..
</div>
<?php } ?>

为了获取该变量,我将其添加到 catalog/controller/product/category 中的产品数据数组中。php 大约第 215 行:

$data['products'][] = array(
                    'product_id'  => $result['product_id'],
                    'thumb'       => $image,
                    'name'        => $result['name'],
                    'description' => (html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')),
                    'price'       => $price,
                    'special'     => $special,
                    'tax'         => $tax,
                    'rating'      => $result['rating'],
                    'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url),
                    'filter_id'   => $result['filter_id']
                );

...并在 catalog/model/catalog/product.php 中修改了 getProducts() 方法(因为这是上面调用的用于生成 $result 的关联方法)在第 60 行附近,在第 3 行添加最后一个 SQL-语句:

public function getProducts($data = array()) {
        $sql = "SELECT p.product_id, (SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special,
 (SELECT pf.filter_id FROM " . DB_PREFIX . "product_filter pf, " . DB_PREFIX . "product p WHERE pf.product_id = p.product_id) AS filter_id";
        //...

不幸的是,这会引发 1242 错误:子查询 returns 多于 1 行 所以我挖出了 Can I concatenate multiple MySQL rows into one field? 并尝试了这个:

$sql = "SELECT p.product_id, (SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special, 
(SELECT GROUP_CONCAT(pf.filter_id SEPARATOR ', ') FROM " . DB_PREFIX . "product_filter pf, " . DB_PREFIX . "product p WHERE pf.product_id = p.product_id) AS filter_id";

由于这也无济于事,我在 PHPMyAdmin 中尝试了相同的 SQL-query(de-php'ized),结果为空.第一个查询没问题。

也许我做错了,它可以更容易地实现...我只是想显示与产品相关的所有 Filter_IDs

编辑: 尝试了 Abdo Abel 的方式,在我的代码中得到了这个:

catalog/model/catalog/product.php 里面 Class ModelCatalogProduct:

    public function getAllProductFilters($product_id) {
   $sql = "SELECT pf.filter_id FROM " . DB_PREFIX . "product_filter pf WHERE pf.product_id = '" . $product_id . "'";
    $query = $this->db->query($sql);

    if ($query->num_rows) {
        $filter_ids = "";
        foreach ($query->rows as $result) {
                $filter_ids .= $result['filter_id'].', ';
            }
        return $filter_ids;
    }
    else {
        return "no filter";
    }

}

catalog/controller/product/category.php:

$data['products'][] = array(
                    'product_id'  => $result['product_id'],
//...
                    'filter_id'   => $this->model_catalog_product->getAllProductFilters($result['product_id'])
                );

真正奇怪的是,它甚至不会显示我在 else 语句中添加的文本 "no filter"。不幸的是,我在我客户的网站空间上这样做,我几乎没有机会正常调试...

编辑 2: 将上面的 SQL 更改为建议,仍然没有效果......我想知道我的设置是否有问题 - 我还编辑了 $product['description'] 不在类别上显示那三个点 -页面(见上文,第二个代码段),但它们仍然存在...

编辑 3: 好的,我现在放弃了...我只是编辑了 $product['description'] 以显示 $price - 我的模板仍然打印描述 (!!!)

...
                    'name'        => $result['name'],
                    'description' => $price,
                    'price'       => $price,
...

编辑 4: 就在我清除我的安装并重新开始之前,我从 Miwisoft-Support 得到了提示,每个人都应该知道,谁在 Joomla 中通过 Mijoshop 使用 OC2: 修改 Controller/Model-Files 后,前往管理面板 "Extensions" => "Modification" 并点击右上角的刷新按钮。 Mijoshop 在应用自己的修改后使用原始文件的副本,因此 OC 的根目录中的更改无法生效。 顺便说一句,现在有一个 PHP 错误,所以回去继续工作吧 ;) 我会及时通知你的!

上次编辑: 有效 :) 本来可以节省很多时间,如果我以前知道的话...

更改查询中的这一小部分

(SELECT GROUP_CONCAT(pf.filter_id SEPARATOR ', ') FROM " . DB_PREFIX . "product_filter pf, " . DB_PREFIX . "product p WHERE pf.product_id = p.product_id) AS filter_id

(SELECT GROUP_CONCAT(pf.filter_id SEPARATOR ', ') FROM " . DB_PREFIX . "product_filter pf WHERE pf.product_id = p.product_id GROUP BY pf.product_id) AS filter_id

编辑

由于您在使用 group_concat 时遇到了一些问题(我实际上并没有意识到),这里有另一种方法可以满足您的要求:

(1) 创建一个单独的模型函数,returns 过滤给定产品 ID 的产品,此函数最合适的位置是在文件中mode/catalog/product.php @ class ModelCatalogProduct,假设该函数的名称将是 getAllProductFilters,那么函数实现将是这样的:

public function getAllProductFilters($product_id)
{
    $sql = "here place a sql that returns a list of the filter ids for a given product";
    $res = $this->db->query($sql);
    // if the associated product has no filters, return an empty string
    if($res->num_rows == 0)
        return "";
    // else, you should make a loop that concatenates the values of what ever column you want in some variable
    $concatenated_values = "";
    // and finally return it !
    return $concatenated_values;
}

(2) 现在,转到文件 catalog/controller/product/category.php
并将此行更改为:
'filter_id' => $result['filter_id']

'filter_id' => $this->model_catalog_product->getAllProductFilters($result['product_id'])

就在我清除我的安装并重新开始之前,我从 Miwisoft-Support 得到了提示,每个人都应该知道,谁在 Joomla 中通过 Mijoshop 使用 OC2 : 修改 Controller/Model-Files 后,前往 Mijoshop 管理面板,"Extensions" => "Modification" 并点击右上角的刷新按钮。 Mijoshop在应用自己的修改后使用原始M-,C-文件的副本,因此OC的根目录中的更改无法生效。