在opencart中随机化默认产品 [英] randomize default products in opencart

查看:81
本文介绍了在opencart中随机化默认产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCart V1.5.3.1,并试图在页面加载时按类别对产品进行随机化或随机排序. 其他排序选项仍然应该仍然有效(按价格,等级,字母顺序,..).

I'm using OpenCart V1.5.3.1 and am trying to randomize or shuffle the products per category on page load. The other sort options should still work though (per price, rating, alphabetical,..).

有人能给我一些指导吗?

Anybody that can give me some pointers?

非常感谢, 史蒂文

我尝试过的代码: 在controller/catalog/product/category.php

Code I've tried: In controller/catalog/product/category.php

就在下面

$this->data['products'][] = array( 'product_id' => $result['product_id'], 'thumb' => $image, 'name' => $result['name'], 'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..', 'price' => $price, 'special' => $special, 'tax' => $tax, 'rating' => $result['rating'], 'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']), 'href' => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id']) );

$this->data['products'][] = array( 'product_id' => $result['product_id'], 'thumb' => $image, 'name' => $result['name'], 'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..', 'price' => $price, 'special' => $special, 'tax' => $tax, 'rating' => $result['rating'], 'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']), 'href' => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id']) );

我添加了:

shuffle($this->data['products']);

我还尝试了以下方法: 在下面:

In stead of this I also tried this: Just below:

$results = $this->model_catalog_product->getProducts($data);

我添加了:

        srand((float)microtime() * 1000000);
    shuffle($results);
    $results = array_slice($results, 0, $data['limit']);

不幸的是,这两种方法在选择其他排序选项(定级,定价,名称)时也会混洗结果.我只希望将页面加载的初始结果改组.

Both these methods unfortunately shuffle also the results when choosing another sorting option (rating, pricing, name). I only want the initial results on page load to be shuffled.

推荐答案

解决方案:

转到catalog/model/catalog/product.php并找到方法getProducts($data). 在这里更改:

Go to catalog/model/catalog/product.php and find the method getProducts($data). Here change this:

if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
    if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
        $sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
    } else {
        $sql .= " ORDER BY " . $data['sort'];
    }
} else {
    $sql .= " ORDER BY p.sort_order";   
} 

对此:

if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
    if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
        $sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
    } else {
        $sql .= " ORDER BY " . $data['sort'];
    }
} else {
    $sql .= " ORDER BY RAND()"; 
} 

(提示:仅更改了最后一个$sql .= ...)

(hint: only the last $sql .= ... changed)

通过这种简单的更改,如果没有选择排序,则产品将始终按照随机顺序进行排序.

By this simple change if there is no sorting chosen the products will always be sorted in random order.

这篇关于在opencart中随机化默认产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆