Matthew
Matthew

Reputation: 309

How to display Category images in OpenCart

My categories.php module in controller:

        $this->data['categories'][] = array(
            'category_id'       => $category['category_id'],
            'name'              => $category['name'],
            'image'             => $category['image'],
            'children'          => $children_data,              
            'href'              => $this->url->link('product/category', 'path=' . $category['category_id'])
        );  

My categories.tpl in /products/ (View)

  <?php foreach ($categories as $category) { ?>
  <li><a href="<?php echo $category['href']; ?>"><img src="<?php echo $category['image']; ?>" alt="<?php echo $category['name']; ?>"></a></li>
  <?php } ?>

However it just gives me an error saying that the variable is unknown.

Am I doing something wrong?

Upvotes: 1

Views: 6759

Answers (2)

Jonid Bendo
Jonid Bendo

Reputation: 880

  1. The First problem is that you are editing the wrong controller file, you should edit the controller/product/category.php if you want your changes to appear in that specific view.

  2. The images will not appear as it is since it is not saved as a full url in the database:

    insted of => $category['image']

    it should be => DIR_IMAGE.$category['image']

Hope this solves your answer.

Upvotes: 2

shadyyx
shadyyx

Reputation: 16055

Your approach was good but missing something - it is good to resize the image between displaying so that users need to download less (or smaller) resources when browsing Your store.

Instead of

            'image'             => $category['image'],

use this

            'image'             => $this->model_tool_image->resize($category['image'], $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height')),

This will ensure the images are resized to the dimensions defined in administration and will append DIR_IMAGE in front of the image path.

If this line is missing in Your source code:

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

make sure You add it before the category image is going to be resized.

Upvotes: 1

Related Questions