Skip to main content

Magento 2 get category collection using specific controllers and category level using object manager

Magento 2 get category collection using object manager

 <?php  
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();  
 $category = $objectManager->get('Magento\Framework\Registry')  
   ->registry('current_category'); //get current category  
 $requestInterface = $objectManager->get('Magento\Framework\App\RequestInterface');  
 //echo $routeName   = $requestInterface->getRouteName();  
 //echo $moduleName   = $requestInterface->getModuleName();  
 $controllerName = $requestInterface->getControllerName();  
 //echo $actionName   = $requestInterface->getActionName();  
 $parcatId = $category->getId(); // current Category ID  
 $parcategory = $objectManager->create('Magento\Catalog\Model\Category')  
   ->load($parcatId);  
 $parent_category = $parcategory->getParentCategory();  
 $subcatId = $parent_category->getId();  
 $subcategory = $objectManager->create('Magento\Catalog\Model\Category')  
   ->load($subcatId);  
 $subcats = $subcategory->getChildrenCategories();  
 $_helper = $this->helper('Magento\Catalog\Helper\Output');  
 ?>  
 <?php if ($parent_category->getLevel() == 2 || $controllerName == 'product'): ?>  
 <ul class="sub_cat_bg navigation">  
 <?php  
   foreach ($subcats as $subcat)  
   {  
     $subcaturl = $subcat->getUrl(); ?>  
  <li class="cat_image_bg level0">  
   <a class="level-top" href="<?php echo $subcaturl ?>">  
   <span><?php echo $subcat->getName(); ?></span>  
   </a>  
  </li>  
  <?php } ?>  
 </ul>  
 <?php endif; ?>  


Comments

Popular posts from this blog

Magento 2 Update product image attributes value using custom script programetically

Magento 2 Update product image programetically <?php //increase the max execution time @ini_set('max_execution_time', -1); //memory_limit @ini_set('memory_limit', -1); error_reporting(E_ALL); ini_set('display_errors', '1'); use \Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $instance = \Magento\Framework\App\ObjectManager::getInstance(); $state = $objectManager->get('\Magento\Framework\App\State'); $state->setAreaCode('frontend'); $product_collections = $instance->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collections = $product_collections->create(); foreach ($collections as $product) { if ($product->getId() == 2052) { $product = $objectManager->create('\Magento\Catalog\Model\Pr...

Magento 2 check product attribute values in database using mysql query

Magento 2 check product attribute in database using mysql query SELECT * FROM `catalog_product_entity_text` where store_id = 1; SELECT * FROM `catalog_product_entity_datetime` where store_id = 1; SELECT * FROM `catalog_product_entity_decimal` where store_id = 1; SELECT * FROM `catalog_product_entity_int` where store_id = 1; SELECT * FROM `catalog_product_entity_varchar` where store_id = 1; Note : In above SQL query store_id 1 is ID of store. kindly you need to check store Id for all store-views in admin and run the above code for each store-views separately.