1

I previously used ObjectManager to get current category image. but it sometimes caused problems to me. Like loading blank page without no content, slower loading time etc.. , also I saw in a few questions that people commented that using ObjectManager directly is not a good approach.

This is the page source when I get blank page error.

    <img class="img-responsive" src="<br />
    <b>Fatal error</b>:  Uncaught Error: Call to a member function getImageUrl() on null in E:\xampp\htdocs\magento_projects\abc\app\design\frontend\abcvendor\abctheme\Magento_Catalog\templates\product\view\brandlogoimage.phtml:37
    Stack trace:
    #0 E:\xampp\htdocs\magento_projects\abc\vendor\magento\framework\View\TemplateEngine\Php.php(59): include()
    #1 E:\xampp\htdocs\magento_projects\abc\vendor\magento\framework\View\Element\Template.php(255): Magento\Framework\View\TemplateEngine\Php-&gt;render(Object(Magento\Catalog\Block\Product\View\Interceptor), 'E:/xampp/htdocs...', Array)
    #2 E:\xampp\htdocs\magento_projects\abc\var\generation\Magento\Catalog\Block\Product\View\Interceptor.php(544): Magento\Framework\View\Element\Template-&gt;fetchView('E:/xampp/htdocs...')
    #3 E:\xampp\htdocs\magento_projects\abc\vendor\magento\framework\View\Element\Template.php(279): Magento\Catalog\Block\Product\View\Interceptor-&gt;fetchView('E:/xampp/htdocs...')
    #4 E:\xampp\htdocs\magento_projects\abc\vendor\magento\framework\View\Element\Abstrac in <b>E:\xampp\htdocs\magento_projects\abc\app\design\frontend\abcvendor\abctheme\Magento_Catalog\templates\product\view\brandlogoimage.phtml</b> on line <b>37</b><br />

This is the code I used to get category in my page, I used phtml file.

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category'); ?> 
    <img class="img-responsive" src="<?php echo $category->getImageUrl(); ?>" alt="brandlogo" /></a>

How can I prevent that blank page loading error?

Is there any better approach to get category image than method I've used here?

2

try this code its work for me

add this code in vendor/magento/module-catalog/view/frontend/templates/product/view/details.phtml file and check

 $product = $block->getProduct();

$categories = $product->getCategoryIds(); /*will return category ids array*/
foreach($categories as $category){
    $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
    echo $cat->getName();?>
    <img src="<?php echo $cat->getImageUrl(); ?>" height="100" width="100">
<?php
    }
?>

Please override product detail page.

  • 1
    did you get the required result? – Mandar Parbate Oct 27 '17 at 8:41
  • 1
    Yes, I can get result from using ObjectManager, But I need to get this without using ObjectManager. Thanks for all your help, But I'm going to try as good as I can to avoid using objectManager – Joey Oct 27 '17 at 8:42
  • i have update the code please check – Mandar Parbate Oct 27 '17 at 8:46
  • did you get the result? – Mandar Parbate Oct 27 '17 at 8:48
  • It's irrelevant if he got the result or not. It's the wrong way to do it. – seanbreeden Oct 23 '18 at 15:49
0

You can create Block class in which you can get current category like:

    namespace Namespace\Module_Name\Block;

class Subcategories extends \Magento\Framework\View\Element\Template
{

    /**
     * @var \Magento\Framework\Registry
     */
    protected $_registry;

    /**
     * @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
     */
    protected $_categoryCollectionFactory;

    /**
     * @var \Magento\Catalog\Helper\Category
     */
    protected $_categoryHelper;

    /**
     * Subcategories constructor.
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
     * @param \Magento\Catalog\Helper\Category $categoryHelper
     * @param \Magento\Framework\Registry $registry
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {
        $this->_registry = $registry;
        $this->_categoryCollectionFactory = $categoryCollectionFactory;
        $this->_categoryHelper = $categoryHelper;
        parent::__construct($context, $data);
    }


    public function getCurrentCategory()
    {
        return $this->_registry->registry('current_category');
    }


    public function getCategoryCollection(){
        $_category = $this->getCurrentCategory();
        $collection = $this->_categoryCollectionFactory->create();
        $collection->addAttributeToSelect('*')
            ->addAttributeToFilter('is_active', 1)
            ->setOrder('position', 'ASC')
            ->addIdFilter($_category->getChildren());

        return $collection;
    }


}

And you can call this function in your phtml file like:

    $category = $block->getCurrentCategory();
//------------OR--------------------
    $categoryCollection = $block->getCategoryCollection();
    $category->getImageUrl();
  • Now I get this error, Fatal error: Uncaught Error: Call to a member function getImageUrl() on null – Joey Oct 27 '17 at 7:00
  • this might be because if you are not getting the current category, I think – Mandar Parbate Oct 27 '17 at 7:04
  • can you please check the print_r($category->getData()) if your getting the current category or not? – Mandar Parbate Oct 27 '17 at 7:05
  • how to fix that? when using object manager approach, I get category image first but after few page refreshes, its gone and I get same error – Joey Oct 27 '17 at 7:06
  • whats the error – Mandar Parbate Oct 27 '17 at 7:07

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.