cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect if a subcategories have Include in Menu

SOLVED

How to detect if a subcategories have Include in Menu

Hi,

 

I need to check how many subcategories in a category, have the Include In Menu set to yes.

is there a way to do this?

 

the reason that I need to do this is, so if the subcategories are all set to Include In Menu OFF, I will make the category link to open the product page not open the subcategory menu.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to detect if a subcategories have Include in Menu

@robertbits  - okay for that you need to get custom category collection in your block file and then you need to traverse into your template(.phtml) file.

 

Below is the logic to get the categories which have include_in_menu property is enabled:

 

protected $_categoryCollection;

public function __construct
( \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory, $data = [] )
{ $this->_categoryCollection = $categoryCollectionFactory; parent::__construct($data); } $categories = $this->_categoryCollection->create()
->addAttributeToFilter('include_in_menu', array('eq' => 1); //only categories that is included in menu foreach ($categories as $category)
{ $category->getName(); }

 

Hope it helps !!!

if issue solved,Click Kudos & Accept as Solution

View solution in original post

3 REPLIES 3

Re: How to detect if a subcategories have Include in Menu

@robertbits  - okay for that you need to get custom category collection in your block file and then you need to traverse into your template(.phtml) file.

 

Below is the logic to get the categories which have include_in_menu property is enabled:

 

protected $_categoryCollection;

public function __construct
( \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory, $data = [] )
{ $this->_categoryCollection = $categoryCollectionFactory; parent::__construct($data); } $categories = $this->_categoryCollection->create()
->addAttributeToFilter('include_in_menu', array('eq' => 1); //only categories that is included in menu foreach ($categories as $category)
{ $category->getName(); }

 

Hope it helps !!!

if issue solved,Click Kudos & Accept as Solution

Re: How to detect if a subcategories have Include in Menu

Hi @Manthan Dave,

 

I didn't manage to make it work.

I already have some code which will check if sub-categories exist which is this one:

public function hasChildren($category)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $tableName = $resource->getTableName('employee'); //gives table name with prefix
         
        //Select Data from table
        $sql = "Select * FROM catalog_category_entity WHERE parent_id='$category'";
        $result = $connection->fetchAll($sql);
        $result = count($result);
        return $result;
     }

it could be that when I added the code you gave me interfered with the current one.

 

Then I got an idea but don't know if possible: do you know where the IncludeInMenu option is saved in the database, since in that case I will added to the sql query.

 

do you think that, that's a good idea?

 

Update:

it worked, I had a conflict in a variable. thanks for your help.

Re: How to detect if a subcategories have Include in Menu

Hi @robertbits,

glad to know that you have resolved your issue , Happy to help and keep helping Smiley Happy

if issue solved,Click Kudos & Accept as Solution