doukang7858 2016-11-10 15:06 采纳率: 0%
浏览 119
已采纳

如何在opencart中为特色产品添加排序顺序?

Hi does anyone know if there is a way to choose which of your featured products show first? How can i sort products in featured module? like newest to oldest.. This is the code for module . How can i set the sort order for products ?

    $data['heading_title'] = $this->language->get('heading_title');

    $data['text_tax'] = $this->language->get('text_tax');

    $data['button_cart'] = $this->language->get('button_cart');
    $data['button_wishlist'] = $this->language->get('button_wishlist');
    $data['button_compare'] = $this->language->get('button_compare');

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

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

    $data['products'] = array();

    if (!$setting['limit']) {
        $setting['limit'] = 4;
    }

    if (!empty($setting['product'])) {
        $products = array_slice($setting['product'], 0, (int)$setting['limit']);

        foreach ($products as $product_id) {
            $product_info = $this->model_catalog_product->getProduct($product_id);

            if ($product_info) {
                if ($product_info['image']) {
                    $image = $this->model_tool_image->resize($product_info['image'], $setting['width'], $setting['height']);
                } else {
                    $image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']);
                }

                if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
                    $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                } else {
                    $price = false;
                }

                if ((float)$product_info['special']) {
                    $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                } else {
                    $special = false;
                }

                if ($this->config->get('config_tax')) {
                    $tax = $this->currency->format((float)$product_info['special'] ? $product_info['special'] : $product_info['price']);
                } else {
                    $tax = false;
                }

                if ($this->config->get('config_review_status')) {
                    $rating = $product_info['rating'];
                } else {
                    $rating = false;
                }

                $data['products'][] = array(
                    'product_id'  => $product_info['product_id'],
                    'thumb'       => $image,
                    'name'        => $product_info['name'],
                    'description' => utf8_substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
                    'price'       => $price,
                    'special'     => $special,
                    'tax'         => $tax,
                    'rating'      => $rating,
                    'href'        => $this->url->link('product/product', 'product_id=' . $product_info['product_id'])
                );
            }
        }
    }

    if ($data['products']) {
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/featured.tpl')) {
            return $this->load->view($this->config->get('config_template') . '/template/module/featured.tpl', $data);
        } else {
            return $this->load->view('default/template/module/featured.tpl', $data);
        }
    }
}

}

  • 写回答

1条回答 默认 最新

  • douyi2798 2016-11-10 18:35
    关注

    Every product in Opencart has a "Sort order" field on "data" tab, you can fill it when you add or edit a product:

    Opencart Sort order field for product

    Go to this file:

    catalog/controller/module/featured.php
    

    add these to line to products array

    'sort_order' => $product_info['sort_order'],
    'date_added' => $product_info['date_added']
    

    and use array_multisort as bellow:

    $temp_array = array();
    foreach ($data['products'] as $key => $row){
        $temp_array[$key] = $row['sort_order'];
    }
    array_multisort($temp_array, SORT_ASC, $data['products']);
    

    insert above code just before if ($data['products']) {

    Here is full code:

    <?php
    class ControllerModuleFeatured extends Controller {
        public function index($setting) {
            $this->load->language('module/featured');
    
            $data['heading_title'] = $this->language->get('heading_title');
    
            $data['text_tax'] = $this->language->get('text_tax');
    
            $data['button_cart'] = $this->language->get('button_cart');
            $data['button_wishlist'] = $this->language->get('button_wishlist');
            $data['button_compare'] = $this->language->get('button_compare');
    
            $this->load->model('catalog/product');
    
            $this->load->model('tool/image');
    
            $data['products'] = array();
    
            if (!$setting['limit']) {
                $setting['limit'] = 4;
            }
    
            if (!empty($setting['product'])) {
                $products = array_slice($setting['product'], 0, (int)$setting['limit']);
    
                foreach ($products as $product_id) {
                    $product_info = $this->model_catalog_product->getProduct($product_id);
    
                    if ($product_info) {
                        if ($product_info['image']) {
                            $image = $this->model_tool_image->resize($product_info['image'], $setting['width'], $setting['height']);
                        } else {
                            $image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']);
                        }
    
                        if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
                            $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                        } else {
                            $price = false;
                        }
    
                        if ((float)$product_info['special']) {
                            $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                        } else {
                            $special = false;
                        }
    
                        if ($this->config->get('config_tax')) {
                            $tax = $this->currency->format((float)$product_info['special'] ? $product_info['special'] : $product_info['price']);
                        } else {
                            $tax = false;
                        }
    
                        if ($this->config->get('config_review_status')) {
                            $rating = $product_info['rating'];
                        } else {
                            $rating = false;
                        }
    
                        $data['products'][] = array(
                            'product_id'  => $product_info['product_id'],
                            'thumb'       => $image,
                            'name'        => $product_info['name'],
                            'description' => utf8_substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
                            'price'       => $price,
                            'special'     => $special,
                            'tax'         => $tax,
                            'rating'      => $rating,
                            'href'        => $this->url->link('product/product', 'product_id=' . $product_info['product_id']),
                            // Add 'sort_order' and 'date_added' to products array
                            'sort_order' => $product_info['sort_order'],
                            'date_added' => $product_info['date_added']
                        );
                    }
                }
            }
    
            // Create a temporary array
            $temp_array = array();
            foreach ($data['products'] as $key => $row){
                // You can use 'date_added' or 'sort_order' or 'price' or ...
                $temp_array[$key] = $row['date_added'];
            }
            // You can use SORT_ASC or SORT_DESC
            array_multisort($temp_array, SORT_ASC, $data['products']);
    
    
            if ($data['products']) {
                if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/featured.tpl')) {
                    return $this->load->view($this->config->get('config_template') . '/template/module/featured.tpl', $data);
                } else {
                    return $this->load->view('default/template/module/featured.tpl', $data);
                }
            }
        }
    }
    

    I've tested this on Opencart 2.1.0.1, Hope this help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。