1
<?php
namespace Vendor\Module\Controller\Index;


use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action
{
    protected $resultJsonFactory;
    /**
    * @var \Magento\Catalog\Helper\Category
    */
    protected $categoryHelper;

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

    protected $resultPageFactory;

    public function __construct(Context $context,
        JsonFactory $jsonFactory, 
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Catalog\Model\CategoryRepository $categoryRepository,
        PageFactory $pageFactory)
    {
        $this->resultJsonFactory = $jsonFactory;
        $this->categoryHelper = $categoryHelper;
        $this->categoryRepository = $categoryRepository;
        $this->resultPageFactory = $pageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $selectedCatId = $this->getRequest()->getParam('subCatId');

        $categoryObj = $this->categoryRepository->get($selectedCatId);

        var_dump($categoryObj->getImageUrl());
            exit;
    }
}

I have set the image in admin for category Id: 358: enter image description here

Below is the output of print_r($categoryObj->getData());

<pre>Array(
[entity_id] => 358
[attribute_set_id] => 12
[parent_id] => 2
[created_at] => 2019-08-05 11:43:07
[updated_at] => 2019-08-05 12:08:06
[path] => 1/2/358
[position] => 22
[level] => 2
[children_count] => 34
[row_id] => 358
[created_in] => 1
[updated_in] => 2147483647
[store_id] => 1
[abacos_codigo] => 
[abacos_codigo_pai] => 
[all_children] => 
[automatic_sorting] => 
[available_sort_by] => 
[category_zone] => 
[children] => 
[custom_apply_to_products] => 0
[custom_design] => 
[custom_design_from] => 
[custom_design_to] => 
[custom_layout_update] => 
[custom_use_parent_settings] => 0
[default_sort_by] => 
[description] => 
[display_mode] => PRODUCTS
[filter_price_range] => 
[hide_desktop] => 0
[image] => 
[include_in_menu] => 0
[is_active] => 1
[is_anchor] => 0
[landing_page] => 
[meta_description] => 
[meta_keywords] => 
[meta_title] => 
[mobile_img] => 
[name] => Start
[nav-image] => 
[page_layout] => category-full-width
[path_in_store] => 
[thumbnail] => 
[url_key] => start
[url_path] => start
[weltpixel_category_url] => 
[weltpixel_category_url_newtab] => 0
[weltpixel_mm_bottom_block] => 
[weltpixel_mm_bottom_block_cms] => 
[weltpixel_mm_bottom_block_type] => none
[weltpixel_mm_columns_number] => 4
[weltpixel_mm_column_width] => auto
[weltpixel_mm_display_mode] => sectioned
[weltpixel_mm_left_block] => 
[weltpixel_mm_left_block_cms] => 
[weltpixel_mm_left_block_type] => none
[weltpixel_mm_mob_hide_allcat] => 0
[weltpixel_mm_right_block] => 
[weltpixel_mm_right_block_cms] => 
[weltpixel_mm_right_block_type] => none
[weltpixel_mm_top_block] => 
[weltpixel_mm_top_block_cms] => 
[weltpixel_mm_top_block_type] => none

)

  • The issue was having in Enterprise Magento. Its been fixed after reindexing. – Dinesh Rajput Aug 7 '19 at 8:45
  • try print_r($categoryObj->getImageUrl()); – Shafeel Sha Aug 8 '19 at 8:28
1

Try to use this below code in your controller. It maybe helpful for you :

protected $categoryFactory


public function __construct(
    .....
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
    .....
){
    .....
    $this->categoryFactory = $categoryFactory;
    .....
}

public function execute()
{
    $selectedCatId = $this->getRequest()->getParam('subCatId');
    $categoryObj = $this->categoryFactory->create()->load($selectedCatId);
    echo $categoryObj->getImageUrl();
}

Output :

enter image description here

  • Its not working. If returns null array. – Dinesh Rajput Aug 7 '19 at 5:30
  • It seems like you didn't set image in that category. – Ankita Patel Aug 7 '19 at 5:31
  • Please make sure that image set in that appropriate category. – Ankita Patel Aug 7 '19 at 5:31
  • Please check my edited question with image screenshot of Admin. – Dinesh Rajput Aug 7 '19 at 5:34
  • remove generated and clean cache please. – Ankita Patel Aug 7 '19 at 5:39
0

Try this:

     protected $_storeManager;

        public function __construct(
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            $data = []
        ) {
            $this->_storeManager = $storeManager;
            parent::__construct($data);
        }

        $categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
        $categories = $categoryFactory->create()                              
            ->addAttributeToSelect('*')
            ->setStore($this->_storeManager->getStore()); //categories from current store will be fetched

        foreach ($categories as $category){
            print_r($category->getImageUrl());
        }
die('end');

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.