M
M
mts20502016-03-11 12:30:41
opencart
mts2050, 2016-03-11 12:30:41

How to display image in opencart menu?

Tell me how to display a category image in the opencart menu.
In the templete/common/ folder, the header.tpl file was added to the right place:

<?php if ($category['image']) { ?>
<img src="<?php echo $category['image']; ?>"/> 
<?php } else { ?>
<?php } ?>

Tell me what you need to add to controller/common/header.php
Code section that is responsible for displaying the menu:
$this->load->model('catalog/category');
    $this->load->model('catalog/product');
    $data['categories'] = array();
    $categories = $this->model_catalog_category->getCategories(0);
    foreach ($categories as $category) {
      if ($category['top']) {
        // Уровень 2
        $children_data = array();
        $children = $this->model_catalog_category->getCategories($category['category_id']);
        foreach ($children as $child) {
          $filter_data = array(
            'filter_category_id'  => $child['category_id'],
            'filter_sub_category' => true
          );
          $children_data[] = array(
            'name'  => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
            'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
          );
        }
        // Уровень 1
        $data['categories'][] = array(
          'name'     => $category['name'],
          'children' => $children_data,
          'column'   => $category['column'] ? $category['column'] : 1,
          'href'     => $this->url->link('product/category', 'path=' . $category['category_id'])
        );
      }
    }		
    $data['language'] = $this->load->controller('common/language');
    $data['currency'] = $this->load->controller('common/currency');
    $data['search'] = $this->load->controller('common/search');
    $data['cart'] = $this->load->controller('common/cart');

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mts2050, 2016-03-11
@mts2050

Pictures are displayed in categories, where it is implemented like this:

<div class="well red-links">
        <?php if ($thumb) { ?>
        <div class="pull-left"><img src="<?php echo $thumb; ?>" alt="<?php echo $heading_title; ?>" title="<?php echo $heading_title; ?>" class="img-thumbnail" style="margin: 0 10px 5px 0" /></div>
        <?php } ?>
        <?php if ($description) { ?>
        <?php echo $description; ?>
        <?php } ?>
        <div class="clearfix"></div>
      </div>

..../category.php
....
$this->load->model('tool/image');
....
      if ($category_info['image']) {
        $data['thumb'] = $this->model_tool_image->resize($category_info['image'], $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'));
      } else {
        $data['thumb'] = '';
      }
...
      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 = $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
        }
...
$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, $this->config->get('config_product_description_length')) . '..',
          'price'       => $price,
          'special'     => $special,
          'tax'         => $tax,
          'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
          'rating'      => $result['rating'],
          'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url)
        );

I tried this:
After:
Added:
After:
Added:
if ($category['image']) {
$image = $this->model_tool_image->resize($category['image'], 100, 50);
} else {
$image = false;					
}

After:
Added:
In the file .../common/header.tpl
Output like this:
<img src="<?php echo $category['thumb']; ?>" border="0">

F
fromstyle, 2016-03-11
@fromstyle

In short, you need to
add before

if ($category['image']) {
          $image = $this->model_tool_image->resize($category['image'], 50, 50);
        } else {
          $image = $this->model_tool_image->resize('no-image.png', 50, 50);
        }

$data['categories'][] = array(
          'name'     => $category['name'],

after add the line
Similar to this also for subcategories
Do not forget to connect $this->load->model('tool/image');if it is not done above in the code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question