0

I want to get the subcategories of a main category using its main categories url_key to the home page.

I tried in this way.

<?php 
namespace Eightmedia\Homepage\Block\Index;

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


protected $_categoryFactory;

public function __construct(
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Catalog\Block\Product\Context $context,
array $data = []) {
    $this->_categoryFactory = $categoryFactory;
    $this->_storeConfig = $scopeConfig;
    $this->_registry = $registry;
    parent::__construct($context, $data);

}

public function getSubcategoryByUrlkey($url_key)
{ 
        $categorys = $this->_categoryFactory->create()
                          ->getCollection()
                          ->addAttributeToFilter('url_key',$url_key)
                          ->addAttributeToSelect('*');
        $cat_id = $categorys->getFirstItem()->getEntityId();

        $category = $this->_categoryFactory->create()->load($cat_id);
        $childrenCategories = $category->getChildrenCategories();

        foreach($childrenCategories as $subcategorie) {
           echo '+'.$subcategorie->getName().
                '+'.$subcategorie->getDescription().
                '+'.$subcategorie->getData('featured_manufacturer').'</br>';
}
}

Here i can get the sub_category_name but i cannot get any other stuffs. featured_manufacturer is a custom category attribute.

Magento version 2.2.2

1 . How to get all subcategories ?

2 . How to get custom category attribute values?

UPDATE : My install script

$eavSetup -> removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'featured_manufacturer');


        $eavSetup -> addAttribute(\Magento\Catalog\Model\Category :: ENTITY, 'featured_manufacturer', [
                    'type' => 'int',
                    'label' => 'Featured Manufacturer?',
                    'input' => 'select',
                    'required' => false,
                    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                    'sort_order' => 110,
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'group' => 'General Information',
                    "default" => "",
                    "class"    => "",
                    "note"       => ""
        ]
        );
2

I tested code it is working with following logic you can test it. you can add ->addAttributeToSelect('featured_manufacturer')

public function getSubcategoryByUrlkey($url_key)
{ 
        $categorys = $this->_categoryFactory->create()
                          ->getCollection()
                          ->addAttributeToFilter('url_key',$url_key)
                          ->addAttributeToSelect('*');
        $cat_id = $categorys->getFirstItem()->getEntityId();

        $category = $this->_categoryFactory->create()->load($cat_id);
        $childrenCategories = $category->getChildrenCategories();
        $allChildIds = $childrenCategories->getAllIds();

        $newCollection = $this->_categoryCollectionFactory->create()
                        ->addFieldToFilter('entity_id',array('in' => $allChildIds))
                            ->addAttributeToSelect('*');
        foreach($newCollection as $subcategorie) {

           echo '+'.$subcategorie->getName().
                '+'.$subcategorie->getDescription().
                '+'.$subcategorie->getData('featured_manufacturer').'</br>';
    }
0
0

Did you create custom category attribute via InsallData/UpgradeData script? Here is the proper way of creating a custom attribute, check if below values are true else it won't retrieve on models

Proper way is to in the Install/Upgrade script:

 $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'my_custom_attribute',
        [
            // ...
            'used_in_product_listing' => true, // for category pages
            'visible_on_front' => true, // for frontend??
            'is_used_in_grid' => true, // for category pages
            'is_visible_in_grid' => true // for category pages
        ]
    );

also on the collection do:

$collection->addAttributeToSelect('my_custom_attribute')

and if its the template you can just call:

$category->getMyCustomAttribute()
// or
$category->getData('my_custom_attribute');
3
  • yes.via a custom mmodule i created the custom category attribute.Please check my approach & help me to solve.to where i want to add the $collection->addAttributeToSelect('my_custom_attribute') in my subcategory foreach()?
    – Mujahidh
    Mar 22 '18 at 7:31
  • Yes you are right. Try it!
    – Shireen N
    Mar 22 '18 at 7:32
  • i did not add 'used_in_product_listing' => true, // for category pages 'visible_on_front' => true, // for frontend?? 'is_used_in_grid' => true, // for category pages 'is_visible_in_grid' => true // for category pages in my install script.How can i upgrade it?
    – Mujahidh
    Mar 22 '18 at 7:38

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.