douhuang5623 2015-05-04 06:11
浏览 195
已采纳

opencart仅针对特定类别的排序顺序

I have the default sort order for all categories by price low-high (p.date_added ASC) but for a specific category example "new products" I want the sort to be p.date_added DESC.

In `catalog/controller/product/category.php I have:

<?php class ControllerProductCategory extends Controller {
public function index() {
    $this->language->load('product/category');

    $this->load->model('catalog/category');

    $this->load->model('catalog/product');

    $this->load->model('tool/image');



    if (isset($this->request->get['filter'])) {
        $filter = $this->request->get['filter'];
    } else {
        $filter = '';
    }

    if (isset($this->request->get['sort'])) {
        $sort = $this->request->get['sort'];
    } else {
        $sort = 'p.price';
    }

    if (isset($this->request->get['order'])) {
        $order = $this->request->get['order'];
    } else {
        $order = 'ASC';
    }

How can I make for the category "new products" the sort p.date_added DESC?

I have tried:

if (isset($this->request->get['sort'])) {
    $sort = $this->request->get['sort'];
} else {
    $sort = 'p.price';
}

if (isset($this->request->get['sort'])) {  
    $sort = $this->request->get['sort'];
} else (isset($category_id) && $category_id == '109') {
    $sort = 'p.date_added';
}  

But doesn't work.

Thank You.

  • 写回答

2条回答 默认 最新

  • doulezhi5326 2015-05-04 06:58
    关注

    If you truly want to hard code this in to the controller, which is not something I would recommend, you can simply set the default sort depending on category. One way to do it would be create an array with settings and refer to it in the logic:

    $categorySorts = array(
        'default'   => array('sort' => 'p.price', 'order' => 'ASC'),
        '109'       => array('sort' => 'p.date_added', 'order' => 'DESC')
    );
    
    $default = $categorySorts['default'];   
    if (isset($this->request->get['path'])) {
        $parts = explode('_', (string)$this->request->get['path']);
        $category_id = (int)array_pop($parts);
        if (isset($categorySorts[$category_id])) {
            $default = $categorySorts[$category_id];
        }
    }
    
    if (isset($this->request->get['sort'])) {
        $sort = $this->request->get['sort'];
    } else {
        $sort = $default['sort'];
    }
    
    if (isset($this->request->get['order'])) {
        $order = $this->request->get['order'];
    } else {
        $order = $default['order'];
    }
    

    You could then add the your array for other categories in the future if you so desire:

    $categorySorts = array(
        'default'   => array('sort' => 'p.price', 'order' => 'ASC'),
        '109'       => array('sort' => 'p.date_added', 'order' => 'DESC'),
        '110'       => array('sort' => 'p.sort_order', 'order' => 'DESC'),
        '111'       => array('sort' => 'rating', 'order' => 'DESC')
    );
    

    Personally, I would not go down this path. It will make your life difficult and the lives of other developers who may be cursed with having to work on this site. Instead, I advise you to dig a little deeper and create a setting for this in the admin panel. It will be a lot more work but in the long run you will learn a lot and your website will not become a tangles mess of hardcoded rules.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面