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 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思