3

I am trying to get a level 2 category list. I got all level 2 categories but I am also getting 2 unknown categories which are not in backend. This is weird. Here is my code

public function getShopCategories()
{
    $objectManagerr = \Magento\Framework\App\ObjectManager::getInstance();
    $categoryFactory = $objectManagerr->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');

    $categories = $categoryFactory->create()
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('is_active', 1) //only active categories
                    ->addAttributeToFilter('include_in_menu', 1)
                    ->addAttributeToFilter('level', array('eq'=>2));

    return $categories;
}

I reindexed and checked for different store views but not getting these categories.

  • Have you checked in the database is unknown category is present? – Chirag Patel Jun 14 at 4:54
  • Use the below query to get all categories. – Chirag Patel Jun 14 at 4:59
  • 1
    SELECT e.*, IF(at_name.value_id > 0, at_name.value, at_name_default.value) AS name FROM catalog_category_entity AS e INNER JOIN catalog_category_entity_varchar AS at_name_default ON (at_name_default.entity_id = e.entity_id) AND (at_name_default.attribute_id = '41') LEFT JOIN catalog_category_entity_varchar AS at_name ON (at_name.entity_id = e.entity_id) AND (at_name.attribute_id = '41') – Chirag Patel Jun 14 at 4:59
  • thank you, it worked @ChiragPatel – Bhakti Thakkar Jun 17 at 6:09
  • 1
    Thanks for the appreciation answer :) +1 for your appreciation. – Chirag Patel Jun 17 at 6:22
1

Check unknown category is present in the database? You can use below query to getcategories.

SELECT e.*, IF(at_name.value_id > 0, at_name.value, at_name_default.value) AS name FROM catalog_category_entity AS e INNER JOIN catalog_category_entity_varchar AS at_name_default ON (at_name_default.entity_id = e.entity_id) AND (at_name_default.attribute_id = '41') LEFT JOIN catalog_category_entity_varchar AS at_name ON (at_name.entity_id = e.entity_id) AND (at_name.attribute_id = '41')

I hope it helps!

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.