0

I am trying to get an image which is uploaded for the category.

& also

I have category id and want to load complete details of that category based on category ID.

How can I achieve this?

0

You can create a custom Block with two functions. one for get category image by categoryID and another function for get category data by categoryId .

<?php

namespace Vendor\Module\Block;

use Magento\Catalog\Model\ResourceModel\CategoryFactory;
use Magento\Framework\View\Element\Template\Context;
use Magento\Store\Model\StoreManagerInterface;

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


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


   /**
   * @var \Magento\Catalog\Model\CategoryRepository
   */
   protected $categoryRepository;


    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Catalog\Model\CategoryRepository $categoryRepository,
         StoreManagerInterface $storeManager,
        array $data = []
    ) {
        parent::__construct($context);
        $this->categoryHelper = $categoryHelper;        
        $this->categoryRepository = $categoryRepository;
        $this->_storeManager = $storeManager;
    }

    /**
     * 
     * get category image
     * @param int categoryId
     * @return string Imageurl
     */
    public function getImageByCategoryId($categoryId){
        $category = $this->categoryRepository->get($categoryId);
        $imageurl = $category->getImageUrl();       
        return $imageurl;        
    }



    /**
     * 
     * get category Data
     * @param int categoryId
     * @return array 
     */
    public function getCategoryById($categoryId){
        $category = $this->categoryRepository->get($categoryId);        
        return $category->getData();
    }




}

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.