我按价格低 - 高(p.date_added ASC
)对所有类别进行默认排序,但对于特定类别示例“新产品”,我希望排序为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';
}
如何对“新产品”类别进行排序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';
}
但是不起作用。
谢谢。
答案 0 :(得分:0)
如果您真的想将其硬编码到控制器中,这不是我建议的,您可以根据类别设置默认排序。一种方法是创建一个带有设置的数组,并在逻辑中引用它:
$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')
);
就个人而言,我不走这条路。它会让你的生活变得困难,而其他开发者的生活也可能因为不得不在这个网站上工作而受到诅咒。相反,我建议您深入挖掘并在管理面板中为此创建设置。这将是一项更多的工作,但从长远来看,你将学到很多东西,你的网站将不会成为一堆乱码的硬编码。
答案 1 :(得分:0)
你应该做的是检查当前的类别状态,你应该在第一个之前放置以下内容:
$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,你最好把你的写作(当然看到它工作并且不包含任何错误)转换成模块。