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

opencart sort order for only a specific category

我有所有类别的默认排序顺序(p.date_added ASC),但对于特定类别示例 "new products" 我希望排序为 p.date_added DESC

在`catalog/controller/product/category.php 我有:

<?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';
    }

如何为类别 "new products" 排序 p.date_added DESC

我试过:

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';
}  

但是不起作用。

谢谢。

如果您真的想将其硬编码到控制器中(我不推荐这样做),您只需根据类别设置默认排序即可。一种方法是创建一个带有设置的数组并在逻辑中引用它:

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

如果您愿意,以后可以为其他类别添加数组:

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

就我个人而言,我不会走这条路。这会让你的生活变得困难,也会让其他开发人员的生活变得困难,他们可能会因为不得不在这个网站上工作而受到诅咒。相反,我建议您更深入地挖掘并在管理面板中为此创建一个 setting。这将需要更多的工作,但在漫长的 运行 中,您会学到很多东西,您的网站也不会变成一团乱麻的硬编码规则。

你应该做的是检查当前的类别状态,嗯,你应该把下面的放在第一个if之前:

$current_category_id = array();
$targeted_category_name = "new products";
$should_change = false; // this is a boolean telling us if the current category
                        // is what we want
if(isset($_GET['path']))
{
    $current_category_id = explode($_GET['path']);
}

foreach($current_category_id as (int)$cat)
{
    if($cat != 0)
    {
         $category_info = $this->model_catalog_category->getCategory($path_id);
         if($category_info['name'] == $targeted_category_name)
         {
            $should_change = true;
            break; // discontinue the loop
         }
    }

}

然后在添加上述代码后,您应该将 sort if 条件从 :

更改为
if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }   

收件人:

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

尽管为了控制您的核心并能够轻松更新您的 opencart,您最好将您编写的内容(当然是在看到它有效并且不包含错误之后)变成一个模块。