Answer the question
In order to leave comments, you need to log in
How to display the data of discounted products on the main page of Opencart?
The site has a "Special Price" sidebar located in header.tpl (catalog/view/theme/default/template/common/header.tpl). In it, you need to display data only for those products that have a discount.
I try with <?php foreach ($products as $product) { ?> , it says "Invalid argument supplied for foreach()". Looks like the $products array in header.tpl is not defined.
How to display discounted products data in header.tpl ?
Answer the question
In order to leave comments, you need to log in
To get a list of products, see catalog/model/catalog/product.php
Out of the box in opencart, there is no way to get products only at a discount, so it’s better to add it to the getProducts() method, otherwise you will have to get a list of all products and filter already in the controller - no well.
Well, as already mentioned, the variables for header.tpl are defined in catalog/controller/common/header.php, so there you load the model, get a list of products and pack it into the $this->data array
Look towards
catalog/controller/common/header.php
The variables for the header template are defined there.
Maybe someone will come in handy: (Relevant for version 1)
Between public function index() { and $this->response->setOutput($this->render()); in controller/PAGE_NAME.php
paste:
// Загружаем модели
$this->load->model('catalog/product');
$this->load->model('tool/image');
if (isset($this->request->get['page'])) {
$page = $this->request->get['page'];
} else {
$page = 1;
}
if (isset($this->request->get['limit'])) {
$limit = $this->request->get['limit'];
} else {
$limit = $this->config->get('config_catalog_limit');
}
$data = array(
'sort' => 'p.sort_order',
'order' => 'ASC',
'start' => ($page - 1) * $limit,
'limit' => $limit // Количество на странице
);
$results = $this->model_catalog_product->getProductSpecials($data);
$this->data['special_products'] = array();
foreach ($results as $result) {
// Получаем изображение
if ($result['image']) {
$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
} else {
$image = false;
}
// Получаем цену
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$price = false;
}
// Получаем цену со скидкой
if ((float)$result['special']) {
$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$special = false;
}
// Получаем налоги
if ($this->config->get('config_tax')) {
$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
} else {
$tax = false;
}
// Получаем рэйтинг
if ($this->config->get('config_review_status')) {
$rating = (int)$result['rating'];
} else {
$rating = false;
}
$this->data['special_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, 300) . '..',
'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', 'product_id=' . $result['product_id'])
);
}
<ul class="jcarousel-skin">
<?php foreach ($special_products as $special_product):
$price = (!empty($special_product['special'])) ? '<div class="price">'.$special_product['special'].'</div>' : '';
if ($special_product['special'] && $special_product['price']){
$tag_price = '<div class="tag-price"><span class="price-new">'. $special_product['special'] .'</span><span class="price-old">'. $special_product['price'] .'</span></div>';
} else { $tag_price = ''; }
$thumbnail = '';
if ($special_product['thumb']) {
$thumbnail = '<div class="image"><a href="'. $special_product['href'].'"><img src="'. $special_product['thumb'] .'" alt="'. $special_product['name'] .'" /></a></div>';
} else {
$thumbnail = '';
}
$name = '<div class="name"><a href="'.$special_product['href'].'">'. $special_product['name'].'</a></div>';
/*Item OUTPUT*/
echo '<li>'.$tag_price.$thumbnail.'<div class="about">'.$name.$price.'</div></li>';
endforeach; ?>
</ul>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question