1

I am trying to get at a categories image from within PHP / a .phtml file.

Here is my code:

$_category  = $this->getCurrentCategory();
$_children  = $_category->getChildrenCategories();
foreach( $_children as $child ){
    if( $child->getIsActive() ){
        echo "<p>" . $child->getImageUrl() . "</p>";

This last line results in an empty string. When I var_dump it, I get the following:

bool(false)

Upon further investigation, this getImageUrl function is defined in /vendor/magento/module-catalog/Model/Category.php on line 656, like so:

public function getImageUrl(){
    $url = false;
    $image = $this->getImage();
    if ($image) {

The problem here is that $this->getImage() returns NULL. Nowhere in Category.php can I find the function getImage, and all of this makes me think that there is no getImage function in the Category class.

Is this correct? Do we need to add a getImage method to the class to make getImageUrl work?

Or is getImage inherited from elsewhere? And in this case, why is it returning NULL?


Disclaimers:

This is Magento 2.0.2.

I am not about to alter core code, I already have a theme of my own, and if I was to add a new function to the Category class I would extend the core code in my theme.

I have re-indexed, I have cleared caches, I have re-deployed.

5

The getImage() method is a magic function that is internally redirected to ->getData('image')

The Category Model is an EAV Model which basically means, that the attributes of this model are dynamically declared.

To optimize loading times, not all attributes are loaded in all places, which explains why the call getData('image') or getImage() returns nothing.

The collection to load the children categories is generated here: https://github.com/magento/magento2/blob/develop/app/code/Magento/Catalog/Model/ResourceModel/Category.php#L713

Only name, url_key, all_children and is_anchor attributes are added and so only these attributes are available.

One solution would be, to call ->load() on the child category which would load all attributes. But this is pretty expensive and should mostly be avoided.

you also could instead load your own collection with the image included: in your case, you could do sth. like this:

/* @var $_children \Magento\Catalog\Model\ResourceModel\Category\Collection */
$_children = $this->getCurrentCategory()->getCollection();
$_children->addAttributeToSelect('image')
    ->addAttributeToFilter('is_active', 1))
    ->addIdFilter($_category->getChildren())
    ->setOrder('position', \Magento\Framework\DB\Select::SQL_ASC);    

foreach( $_children as $child ){
        echo "<p>" . $child->getImage(); . "</p>";
5
  • thank you for this answer - unfortunately I keep running into this bizarre error message We're sorry, an error has occurred while generating this email. – Jimmery Apr 26 '16 at 16:44
  • thats indeed weird. This message seems to come from the email template filter: github.com/magento/magento2/blob/…. The only thing in a category I would assume to be filtered would be the description. Although it sounds like a bug, you could try to add the description to the collection with: ->addAttributeToSelect('description') – David Verholen Apr 26 '16 at 16:56
  • Ive been tweaking your code and mine, and I've finally got this to work! Thank you so much - Ive been banging my head against a wall with this problem for far too long! – Jimmery Apr 26 '16 at 16:58
  • nice! could you maybe provide the working code for other people having this problem pls? :) – David Verholen Apr 26 '16 at 17:02
  • @Jimmery, I'm also having the issue in getting the category image. Could you please with that? Here is the URL to my question: magento.stackexchange.com/questions/292744/… – Siva Oct 16 '19 at 6:20
1

Sometimes the problem is that the image defined to the all stores, but in the specific store not definde to use the default image (of the all stores). The solve: go in magento admin to catalog > categories and check the store you want. Choose the specific category and under the image choose "use default value".

step 1: enter image description here

step 2: enter image description here

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.