I need to retreve the closest category witch has an image assigned and place this image as the sub-category image. Currently I have 4 levels of categories, but it may grow in the future, that is why I need to retrieve the closest parent category witch has an image.

For example, I have the Category Winter with 5 sub-categories, and only Winter has an image assigned, so I want to display this image as a subcategory image.

I hope I make myself clear..

Please help,

Thanks a lot.

My current page_header.phtml

    $_helper    = Mage::helper('catalog/output');
    $_category  = Mage::registry('current_category');
    $_imgUrl = $_category->getImageUrl();
    if (!$_imgUrl){
<script type="text/javascript">
<div class="category-header-container">
    <div class="bgback" style="background:#F7F6F4;">
        <div class="container row" style="<?php if($_imgUrl): ?>background-image: url(<?php echo $_imgUrl; ?>);<?php endif; ?>">
            <div class="table-container">
                <div class="category-top-title">
                    <h1><?php echo $_helper->categoryAttribute($_category, $_category->getName(), 'name') ?></h1>
                <div class="left-area breadcrumbs-holder">
                    <?php echo $this->getChildHtml("breadcrumbs"); ?>
<div class="zn_header_bottom_style"></div>

You can get the parent categories sorted by level like this. Let's say that $category is your current category.

$parentIds = explode('/', $category->getPath());
unset($parentIds[count($parentIds) - 1]); //remove the current category
$parents = Mage::getModel('catalog/category')->getCollection()
    ->addFieldToFilter('entity_id', array('in' => $parentIds))
    ->addAttributeToSort('level', 'desc');

Then just loop through the parents and stop if you find one with an image:

$imageUrl = null;
foreach ($parents as $parent) {
    $imageUrl = $parent->getImageUrl();
    if ($imageUrl) {
if ($imageUrl) {
    //you have found the closest parent with an image
else {
    //there are no parents with images.
  • could you help me implement this in my code? I'm trying, but I'm keep getting errors. Sorry, Newby Alert :) P.S. Added my phtml – Artur Rain Oct 23 '14 at 15:11

No, you don't want to do this. (just to make sure, this is more a joke - don't feel offended, of course I don't know what you wants ;-))

What you want is a cron job which runs every X minute (what ever suits you) and check all categories without image.

Then you just run up the tree (make sure to have the whole tree loaded) and set the images on the category. Maybe you want to add an attribute to save the information that this image is retrieved from category X or that it is not explicitly set.

Cronjob: - Load all categories without image - Load all categories (if it is not too much)

foreach($categoriesWithoutImage as $category) {

And finding the image is the tough part (because recursion is not that easy)

private function getImageForCategory(Mage_Catalog_Model_Category $category) {
    if($category->hasImage()) {
        return $category->getImage();
    return $this->getImageForCategory($this->getCategory($category->getParentId()));

$this->getCategory is a function which just holds all categories for better performance

This way you iterate over all categories and climb down the tree of ancestors to find the nearest image

  • So basically this cron job will add the image of the main category to all sub-categories, correct? I don't think I understand the concept of this Cron Job. Sorry. I'm not afended, just searching for a best solution to add images to all categories, without manualy assigning one to every category I have. – Artur Rain Oct 23 '14 at 14:50
  • updated the post – Fabian Blechschmidt Oct 23 '14 at 14:56
  • could you please help me implement this in the code I posted in the question, I'm newby and getting errors. Not sure how to corretly add it. Thank you very much. – Artur Rain Oct 23 '14 at 15:24
  • You don't implement it in your header or somewhere else. You write your own magento cron: webguys.de/magento/tuerchen-08-magento-cron-demystified – Fabian Blechschmidt Oct 23 '14 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.