3

In back end I can see the category has an image but if I try to show it in a template it is empty:

  $_category = Mage::registry('current_category');
  $_imgUrl = $_category->getImageUrl();

Why is $_imgUrl empty?

Template file is \app\design\frontend\default\kemweb\template\page

var_dump($_category) looks like this:

`$_category` does contain the correct item (entity_id checked). Same code worked on other magento installation. Any clue what is going wrong here?

 object(Mage_Catalog_Model_Category)#96 (23) {
  ["_eventPrefix":protected]=>
  string(16) "catalog_category"
  ["_eventObject":protected]=>
  string(8) "category"
  ["_cacheTag":protected]=>
  string(16) "catalog_category"
  ["_useFlatResource":protected]=>
  bool(false)
  ["_designAttributes":"Mage_Catalog_Model_Category":private]=>
  array(6) {
    [0]=>
    string(13) "custom_design"
    [1]=>
    string(18) "custom_design_from"
    [2]=>
    string(16) "custom_design_to"
    [3]=>
    string(11) "page_layout"
    [4]=>
    string(20) "custom_layout_update"
    [5]=>
    string(24) "custom_apply_to_products"
  }
  ["_treeModel":protected]=>
  NULL
  ["_defaultValues":protected]=>
  array(1) {
    ["url_path"]=>
    string(13) "schenken.html"
  }
  ["_storeValuesFlags":protected]=>
  array(1) {
    ["url_path"]=>
    bool(true)
  }
  ["_lockedAttributes":protected]=>
  array(0) {
  }
  ["_isDeleteable":protected]=>
  bool(true)
  ["_isReadonly":protected]=>
  bool(false)
  ["_resourceName":protected]=>
  string(16) "catalog/category"
  ["_resource":protected]=>
  NULL
  ["_resourceCollectionName":protected]=>
  string(27) "catalog/category_collection"
  ["_dataSaveAllowed":protected]=>
  bool(true)
  ["_isObjectNew":protected]=>
  NULL
  ["_data":protected]=>
  array(34) {
    ["store_id"]=>
    string(1) "1"
    ["entity_id"]=>
    string(1) "3"
    ["entity_type_id"]=>
    string(1) "3"
    ["attribute_set_id"]=>
    string(1) "3"
    ["parent_id"]=>
    string(1) "2"
    ["created_at"]=>
    string(19) "2013-06-19 11:29:11"
    ["updated_at"]=>
    string(19) "2013-07-02 09:56:14"
    ["path"]=>
    string(5) "1/2/3"
    ["position"]=>
    string(1) "1"
    ["level"]=>
    string(1) "2"
    ["children_count"]=>
    string(1) "0"
    ["name"]=>
    string(8) "Schenken"
    ["url_key"]=>
    string(8) "schenken"
    ["meta_title"]=>
    NULL
    ["display_mode"]=>
    string(8) "PRODUCTS"
    ["custom_design"]=>
    NULL
    ["page_layout"]=>
    NULL
    ["url_path"]=>
    string(13) "schenken.html"
    ["thumbnail"]=>
    string(16) "cat_schenken.jpg"
    ["is_active"]=>
    string(1) "1"
    ["include_in_menu"]=>
    string(1) "1"
    ["landing_page"]=>
    NULL
    ["is_anchor"]=>
    string(1) "1"
    ["custom_use_parent_settings"]=>
    string(1) "0"
    ["custom_apply_to_products"]=>
    string(1) "0"
    ["description"]=>
    NULL
    ["meta_keywords"]=>
    NULL
    ["meta_description"]=>
    NULL
    ["custom_layout_update"]=>
    NULL
    ["available_sort_by"]=>
    NULL
    ["custom_design_from"]=>
    NULL
    ["custom_design_to"]=>
    NULL
    ["filter_price_range"]=>
    NULL
    ["path_ids"]=>
    array(3) {
      [0]=>
      string(1) "1"
      [1]=>
      string(1) "2"
      [2]=>
      string(1) "3"
    }
  }
  ["_hasDataChanges":protected]=>
  bool(true)
  ["_origData":protected]=>
  array(33) {
    ["store_id"]=>
    string(1) "1"
    ["entity_id"]=>
    string(1) "3"
    ["entity_type_id"]=>
    string(1) "3"
    ["attribute_set_id"]=>
    string(1) "3"
    ["parent_id"]=>
    string(1) "2"
    ["created_at"]=>
    string(19) "2013-06-19 11:29:11"
    ["updated_at"]=>
    string(19) "2013-07-02 09:56:14"
    ["path"]=>
    string(5) "1/2/3"
    ["position"]=>
    string(1) "1"
    ["level"]=>
    string(1) "2"
    ["children_count"]=>
    string(1) "0"
    ["name"]=>
    string(8) "Schenken"
    ["url_key"]=>
    string(8) "schenken"
    ["meta_title"]=>
    NULL
    ["display_mode"]=>
    string(8) "PRODUCTS"
    ["custom_design"]=>
    NULL
    ["page_layout"]=>
    NULL
    ["url_path"]=>
    string(13) "schenken.html"
    ["thumbnail"]=>
    string(16) "cat_schenken.jpg"
    ["is_active"]=>
    string(1) "1"
    ["include_in_menu"]=>
    string(1) "1"
    ["landing_page"]=>
    NULL
    ["is_anchor"]=>
    string(1) "1"
    ["custom_use_parent_settings"]=>
    string(1) "0"
    ["custom_apply_to_products"]=>
    string(1) "0"
    ["description"]=>
    NULL
    ["meta_keywords"]=>
    NULL
    ["meta_description"]=>
    NULL
    ["custom_layout_update"]=>
    NULL
    ["available_sort_by"]=>
    NULL
    ["custom_design_from"]=>
    NULL
    ["custom_design_to"]=>
    NULL
    ["filter_price_range"]=>
    NULL
  }
  ["_idFieldName":protected]=>
  string(9) "entity_id"
  ["_isDeleted":protected]=>
  bool(false)
  ["_oldFieldsMap":protected]=>
  array(0) {
  }
  ["_syncFieldsMap":protected]=>
  array(0) {
  }
}

6 Answers 6

5

I see that only the thumbnail attribute has a value (cat_schenken.jpg). In magento admin you can upload a category image and a category thumbnail image. So you probably uploaded only the thumbnail image and not the main image.

Try to use Zend_Debug::dump($_category->getData()) to have a better view of the category's attributes.

0
4

Looks like you don't even have the image attribute set correctly, getImageUrl needs this:

public function getImageUrl()
{
    $url = false;
    if ($image = $this->getImage()) {
        $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
    }
    return $url;
}

According to the var_dump there is something wrong with the category model, usually there should the $_data['image'] attribute set to something.

Are you sure you don't mix image and thumbnail up? And did you modify something in the database? Like the attribute-group relation? Because that might cause your problems.

3

I had the same issue on my Magento 1.9.2.1 build. But I found (weirdly) the image URL was not returned when using getChildrenCategories(). My answer was to return the image using getCollection() on the model instead like so:

    $_cat = Mage::getModel('catalog/category')->load($id);
    if (isset($_cat) && !empty($_cat) && is_object($_cat)) {
        // Create category collection for children
        $childrenCollection = $_cat->getCollection();
        // Only get child categories of parent cat
        $childrenCollection->addIdFilter($_cat->getChildren());
        // Only get active categories
        $childrenCollection->addAttributeToFilter('is_active', 1);
        // Add base attributes
        $childrenCollection->addAttributeToSelect('url_key')
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('all_children')
                ->addAttributeToSelect('is_anchor')
                ->setOrder('position', Varien_Db_Select::SQL_ASC)
                ->joinUrlRewrite();
        // Add Image 
        $childrenCollection->addAttributeToSelect('image');
        if (count($childrenCollection) > 0) {
            foreach($childrenCollection as $subcat) {
                if ($subcat->getIsActive()) {
                    $subcatName = $subcat->getName();
                    $subcatUrl = $subcat->getUrl();
                    $img = $subcat->getImage();
                    $subcatImgUrl = Mage::getBaseUrl('media').'catalog/category/' . $img;

                    if (!empty($subcatName) && !empty($subcatUrl)) {
                        $html .= '<li class="brand ' . $className . '">';
                        $html .=    '<a href="' . $subcatUrl . '" title="' . $subcatName . '">';
                        $html .=        '<img src="' . $subcatImgUrl . '" alt="' . $subcatName . '" />';
                        $html .=    '</a>';
                        $html .= '</li>';
                    }
                }
            }
        }

PandaWebStudio's answer really helped to debug the situation so thank you for that.

2

As per as your var dump registry variable does not contain the field image.

First you need to check getImage() give value or not

In thebod answer already mention that getImageUrl() will give value whenever image field of category have any value.

Here in this question, The object of category does not contain field image that getImageUrl() does not give result.

May be this category Eav Attribute (image) does not exit database.

For checking this attribute in system,you can try below Mysql query

SELECT attribute_code
FROM eav_attribute
WHERE entity_type_id
IN (

SELECT entity_type_id
FROM eav_entity_type
WHERE entity_type_code =  'catalog_category'
)  and attribute_code ='image'
0

I am using this to get category image url....

<?php $child= Mage:getSingleton('catalog/layer')->getCurrentcategory()->getId();
 $imagesrc= Mage:getModel('catalog/category')->load($child)->getimageUrl(); 
 echo $imagesrc; ?>
0

I tried following so that it does not affect performance and you need to call the category model seprately

Open in root of magento 2.2

/vendor/magento/module-catalog/Model/ResourceModel/Category.php

On line750ish

Add

->addAttributeToSelect(
    'image'
)

or replace function

 /**
     * Return child categories
     *
     * @param \Magento\Catalog\Model\Category $category
     * @return \Magento\Catalog\Model\ResourceModel\Category\Collection
     */
    public function getChildrenCategories($category)
    {
        $collection = $category->getCollection();
        /* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */
        $collection->addAttributeToSelect(
            'url_key'
        )->addAttributeToSelect(
            'image'
        )->addAttributeToSelect(
            'name'
        )->addAttributeToSelect(
            'all_children'
        )->addAttributeToSelect(
            'is_anchor'
        )->addAttributeToFilter(
            'is_active',
            1
        )->addIdFilter(
            $category->getChildren()
        )->setOrder(
            'position',
            \Magento\Framework\DB\Select::SQL_ASC
        )->joinUrlRewrite();

        return $collection;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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