Magento Asked by Ravindrasinh Zala on August 3, 2020
How can I get a first category level from Magento 2.3, my category setup looks like this now.
root_catalog
|-First |-Shoes
|-T-shirts
|-Brands
|-Demo
|-Test
Now First category click open sub category like shoes and T-Shirt in magento 2.3 dependency injection
you can get the whole category path form this code and can separate the level you want even if you product have only current category it will gives you full path with name of categories: |
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$category_Ids = $block->getProduct()->getCategoryIds(); /*will return category ids array*/
$index = count($category_Ids)-1;
$lastcatid = $category_Ids[$index];
$categoryFactory = $objectManager->get('MagentoCatalogModelCategoryFactory');// Instance of Category Model // YOUR CATEGORY ID
$category = $categoryFactory->create()->load(intval($lastcatid));
// Parent Categories
$categoryall ="";
$parentIdsArray = $category->getParentIds(intval($lastcatid));
$parentIdsnewArray = array_push($parentIdsArray,intval($lastcatid));
for($i=0;$i<count($parentIdsArray);$i++){
$cat = $objectManager->create('MagentoCatalogModelCategory')->load($parentIdsArray[$i]);
if($cat->getName() != "Root Catalog"){
$categoryall .= $cat->getName()."/";
}
}
echo $categoryall;
Answered by nihal malik on August 3, 2020
Please try below code to get the first level category collection in your template phtml file:
<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
//get current category
$collection = $objectManager->get('MagentoCatalogModelResourceModelCategoryCollectionFactory')->create();
$collection->addAttributeToFilter('level', array('eq' => 2));
foreach ($collection as $category) {
print_r($category->getData());
}
?>
Note: Direct use of objectManager
is not recommended. Try to create the function in your block class and use it in the template.
Also, you can get the Parent Categories and check the level in for loop!
use MagentoFrameworkRegistry $registry
...
public function getCategoryCollection(){
$currentCategory = $this->_registry->registry('current_category');
if($currentCategory){
if($currentCategory->getParentCategories()){
foreach ($currentCategory->getParentCategories() as $parent) {
//check level
if ($parent->getLevel() == 1) {
return $parent->getId();
}
}
}
}
return null;
}
Or try to inject the MagentoCatalogModelResourceModelCategoryCollectionFactory
class in your block class
use MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryCollection
public function getCategoryCollection()
{
// $level = 2;
$collection = $this->_categoryCollection->create()
->addAttributeToSelect('*')
// ->setStore($this->_storeManager->getStore())
->addAttributeToFilter('level', array('eq' => 2));
// ->addLevelFilter($level); OR
->addAttributeToFilter('is_active','1');
return $collection;
}
Hope this helps!
Answered by Bhaumik Upadhyay on August 3, 2020
SELECT
e2.*
FROM
el_magento.catalog_category_entity e1
INNER JOIN
el_magento.catalog_category_entity e2
ON
e1.entity_id = e2.parent_id
Where
e1.parent_id = 0;
Let me know if you need any further assistance !
Answered by Joao71 on August 3, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP