Ahmed Mohammed
Ahmed Mohammed

Reputation: 33

Display thumbnail of category in category.tpl module in opencart?

I need to display thumbnail images of category in category.tpl module in opencart?

I want display thumbnail of category in extension/module/category.tpl not product/product.tpl

how i can do this?

opencart 2.3.0.2

Upvotes: 0

Views: 1698

Answers (1)

Rupak Nepali
Rupak Nepali

Reputation: 759

Show images for the sub-categories in the opencart version 2.3

Find following code at catalog\controller\product\category.php

$data['categories'][] = array(
    'name' => $result['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
    'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)
);

Replace the code with below code:

$data['categories'][] = array(
    'name' => $result['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
    'image' => $this->model_tool_image->resize($result['image'], 100,100),
    'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)
);

Changed is 'image' => $this->model_tool_image->resize($result['image'], 100,100), if you have to increase the size then change 100 to other values.

Find following code at catalog\view\theme\default\template\product\category.tpl

<?php if ($categories) { ?>
<h3><?php echo $text_refine; ?></h3>
<?php if (count($categories) <= 5) { ?>
<div class="row">
  <div class="col-sm-3">
    <ul>
      <?php foreach ($categories as $category) { ?>
      <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></li>
      <?php } ?>
    </ul>
  </div>
</div>
<?php } else { ?>
<div class="row">
  <?php foreach (array_chunk($categories, ceil(count($categories) / 4)) as $categories) { ?>
  <div class="col-sm-3">
    <ul>
      <?php foreach ($categories as $category) { ?>
      <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></li>
      <?php } ?>
    </ul>
  </div>
  <?php } ?>
</div>
<?php } ?>
<?php } ?>

Replace with the below code

<?php if ($categories) { ?>
      <h3><?php echo $text_refine; ?></h3>
      <?php if (count($categories) <= 5) { ?>
      <div class="row">
        <div class="col-sm-3">
          <ul>
            <?php foreach ($categories as $category) { ?>
            <li> <a href="<?php echo $category['href']; ?>">
                <?php if($category['image']){ ?>
                <img src="<?php echo $category['image']; ?>" ><br>
                <?php } ?>
                <?php echo $category['name']; ?></a></li>
            <?php } ?>
          </ul>
        </div>
      </div>
      <?php } else { ?>
      <div class="row">
        <?php  foreach (array_chunk($categories, ceil(count($categories) / 4)) as $categories) { ?>
        <div class="col-sm-3">
          <ul>
            <?php  foreach ($categories as $category) { ?>
            <li><a href="<?php echo $category['href']; ?>">
                <?php if($category['image']){ ?>
                <img src="<?php echo $category['image']; ?>" ><br>
                <?php } ?>
                <?php echo $category['name']; ?></a></li>
            <?php } ?>
          </ul>
        </div>
        <?php } ?>
      </div>
      <?php } ?>
<?php } ?>

Extra code added is below and there are two places to add the code:

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

You are set for the default theme, but if you are using custom theme then you have to manage as per your theme.

https://webocreation.com/blog/show-images-sub-categories-opencart-version-2-3

Upvotes: 1

Related Questions