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


Opencart 2 - Echo Filter ID/Description in Category View

我正在做一个Opencart 2 -项目,需要在类别视图中分别回应每个产品的过滤器id(例如,作为产品拇指的附加类,将不同的样式应用于素食/素食食品)。

我已经打破我的手指几个小时了,现在,修改控制器,模型,和视图文件,但不能得到一个解决方案…经过多次尝试(事后看来很愚蠢),我最终被困在这里:

在我的目录/视图/主题/默认/模板/产品/类别。tpl i echo $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中的product-data数组中,大约在第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中第60行左右的getProducts()方法(因为这是上面调用来生成$result的相关方法),添加了第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-Error:子查询返回多于一行我可以将多个MySQL行连接到一个字段中吗?并尝试了这个:

$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查询(去php化),结果为空。第一个查询运行正常。

也许我做错了,它可以更容易实现…我只想显示与Product

相关的所有filter_id编辑:

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

catalog/model/catalog/product.php inside 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";
    }
}
在<<p> strong>目录/控制器/产品/category.php :
$data['products'][] = array(
                    'product_id'  => $result['product_id'],
//...
                    'filter_id'   => $this->model_catalog_product->getAllProductFilters($result['product_id'])
                );

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

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

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

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

编辑4:就在我清除安装并重新开始之前,我从Miwisoft-Support那里得到了一个提示,这是每个人都应该知道的,他在Joomla中通过Mijoshop使用OC2:修改控制器/模型文件后,转到管理面板"扩展"=>"修改",然后点击右上角的刷新按钮。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)创建一个单独的模型函数,返回给定产品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:修改控制器/模型文件后,前往Mijoshop管理面板,"扩展"=>"修改"并点击右上角的刷新按钮。

Mijoshop使用的是原M-, c -文件的副本,所以在OC的根目录中的更改无法生效。