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

时间:2015-06-09 20:38:01

标签: php opencart

我正在开发一个Opencart 2 - 项目,需要在分类视图中单独回显每个产品的过滤器ID(例如,作为产品大拇指的附加类,将不同的样式应用于纯素食/素食食品)。 / p>

我已经打破了几个小时的手指,对控制器,模型和视图文件进行了调整,但无法找到解决方案......经过多次(事后看来很愚蠢)的尝试,我终于得到了卡在这里:

在我的目录/视图/主题/默认/模板/产品/ 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 } ?>

要获得该变量,我将其添加到第215行的目录/ controller / product / category.php 中的产品数据数组中:

$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']
                );

...并修改了getProducts()方法(因为这是上面调用的关联方法,以生成$ result)在第60行的 catalog / model / catalog / product.php 中添加最后一个第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错误:子查询返回超过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查询(de-php'ized),结果是空的。第一个查询很顺利。

也许我正在以错误的方式完成它并且可以更容易实现...我只想显示与产品相关的所有Filter_ID

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

目录/ model / catalog / product.php 类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";
    }

}

目录/ controller / product / category.php

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

真正奇怪的是它甚至不会在else语句中添加“无过滤器”文本。不幸的是,我在客户的网站空间上这样做,我几乎没有机会通常调试......

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

编辑3: 好的,我现在放弃......我刚刚编辑了$product['description']来显示$ price - 我的模板仍打印出解密(!!!)

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

编辑4: 就在我清楚地擦拭我的安装并重新开始之前,我得到了Miwisoft-Support的提示,每个人都应该知道,谁在Joomla通过Mijoshop使用OC2: 修改Controller / Model-Files后,转到Admin Panel“Extensions”=&gt; “修改”并点击右上角的“刷新”按钮。 Mijoshop在应用自己的修改后使用原始文件的副本,因此OC根的更改无法生效。 BTW现在遇到了一个PHP错误,所以重新开始工作并继续这样做;)我会让你更新!

最后编辑: 如果我之前知道这一点,可以节省很多时间......

2 个答案:

答案 0 :(得分:1)

更改查询中的这个小部分

(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 @ {{1假设该函数的名称将为class ModelCatalogProduct,因此函数实现将是这样的:

getAllProductFilters

(2)现在,转到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; } 文件并更改此行:
catalog/controller/product/category.php

{{ 1}}

答案 1 :(得分:1)

在我清楚地擦除我的安装并重新开始之前,我得到了Miwisoft-Support的提示,每个人都应该知道,谁在Joomla使用 OC2通过Mijoshop :修改Controller /之后模型文件,转到Mijoshop管理面板,“扩展”=&gt; “修改”并点击右上角的“刷新”按钮。 Mijoshop在应用自己的修改后使用原始M,C文件的副本,因此OC根的更改无法生效。