php - How can i add sort order for featured products in opencart? -
hi know if there way choose of featured products show first? how can sort products in featured module? newest oldest.. code module . how can set sort order 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 $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); } } }
}
every product in opencart has "sort order" field on "data" tab, can fill when add or edit product:
go file:
catalog/controller/module/featured.php
add these line products array
'sort_order' => $product_info['sort_order'], 'date_added' => $product_info['date_added']
and use array_multisort
bellow:
$temp_array = array(); foreach ($data['products'] $key => $row){ $temp_array[$key] = $row['sort_order']; } array_multisort($temp_array, sort_asc, $data['products']);
insert above code before if ($data['products']) {
here 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 $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' , 'date_added' products array 'sort_order' => $product_info['sort_order'], 'date_added' => $product_info['date_added'] ); } } } // create temporary array $temp_array = array(); foreach ($data['products'] $key => $row){ // can use 'date_added' or 'sort_order' or 'price' or ... $temp_array[$key] = $row['date_added']; } // 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 on opencart 2.1.0.1, hope you.
Comments
Post a Comment