Hi,
I was wondering if there was a way (or extension) that allows you to display the subcategories of a top-level category as banners on that category page instead of products? E.G. Pasta is the top-level category that has no products assigned to it. Pasta is the parent of the subcategories - Spaghetti, Fusili, Linguini, Penne. On the Pasta category page, it displays banners (pulled from category images) 'Spaghetti', 'Fusili', 'Linguini', 'Penne' as clickable links to those categories. This method is often used to dispense with some filters.
I could, at a stretch, use the category description to hardcode the banners, but would like to be able to do it programmatically if possible.
Any pointers would be gratefully received.
TIA
I was wondering if there was a way (or extension) that allows you to display the subcategories of a top-level category as banners on that category page instead of products? E.G. Pasta is the top-level category that has no products assigned to it. Pasta is the parent of the subcategories - Spaghetti, Fusili, Linguini, Penne. On the Pasta category page, it displays banners (pulled from category images) 'Spaghetti', 'Fusili', 'Linguini', 'Penne' as clickable links to those categories. This method is often used to dispense with some filters.
I could, at a stretch, use the category description to hardcode the banners, but would like to be able to do it programmatically if possible.
Any pointers would be gratefully received.
TIA
I've done something similar for a few clients before - it displays a slider of all the first-level subcategories of the current category at the top of the page instead of banners. But essentially quite similar to what you are trying to achieve in your first post.
If you look at the 'catalog/controller/product/category.php' file, you will see that there is already this line of code which retrieves all the first-level subcategories of the current category:
Below it, you will see that it already loops through the `$results` variable and assigns the name of the subcategory and the link to the subcategory to a `$data['categories']` array. And in the template twig file, you can see that it already displays all the subcategories under the "Refine Search" section just below the heading title.
You can just easily modify this existing code to include the images of the subcategories, and display them on the frontend. For example, in the for-loop of `$results`:
Then on the frontend, just modify the existing code to display your banners instead of a list of URLs.
You could also use the Category module, but what you have to do is similar to the above, resize and include the image in the array, then display it on the frontend.
If you look at the 'catalog/controller/product/category.php' file, you will see that there is already this line of code which retrieves all the first-level subcategories of the current category:
Code: Select all
$results = $this->model_catalog_category->getCategories($category_id);
You can just easily modify this existing code to include the images of the subcategories, and display them on the frontend. For example, in the for-loop of `$results`:
Code: Select all
$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),
'image' => this->model_tool_image->resize($result['image'], $width, $height)
);
You could also use the Category module, but what you have to do is similar to the above, resize and include the image in the array, then display it on the frontend.
Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager Pro • Drag & Drop Sort Order
Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.
Thank you, I appreciate the reply. Rather than bothering with banners, I decided just to use the category image with the existing link.
So this is my code in template/product/category.twig. I just popped the image on top of the link. The problem is that it's not working in every category.
Not entirely sure why, as the categories are set up exactly the same as each other.
EDIT: The subcategories are set up so less than 6 are stacked vertically, and more than 6 are then displayed horizontally, so the pages where the images weren't displaying were those with less than 6 subcategories. I've commented that out now.
I've had a look at the category module, but the syntax is different from the category controller.
Would I need to do something similar to this:
But in /controller/extension/module/category.php?
And maybe add this
I've tried the following but it's not working.
My apologies, I can muddle through a lot of programming just with a bit of logic, but I'm not great when getting into the nitty-gritty.
So this is my code in template/product/category.twig. I just popped the image on top of the link. The problem is that it's not working in every category.
Code: Select all
<div class="col-sm-3">
<ul>
{% for child in category %}
<img src="{{ child.image }}">
<li><a href="{{ child.href }}">{{ child.name }}</a></li>
{% endfor %}
</ul>
</div>
EDIT: The subcategories are set up so less than 6 are stacked vertically, and more than 6 are then displayed horizontally, so the pages where the images weren't displaying were those with less than 6 subcategories. I've commented that out now.
I've had a look at the category module, but the syntax is different from the category controller.
Would I need to do something similar to this:
Code: Select all
'image' =>$this->model_tool_image->resize($result['image'], 140, 140)
But in /controller/extension/module/category.php?
And maybe add this
Code: Select all
$this->load->model('tool/image');
Code: Select all
$data['categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'children' => $children_data,
'image' =>$this->model_tool_image->resize($category['image'], 140, 140),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
Who is online
Users browsing this forum: No registered users and 10 guests