I want to show all the category on the left sidebar on home page. Please let me know how to get all the categories by collection, or there is another way to call all the category. please let me know
-
1This seems to be a duplicate of Adding layered navigation block to custom page and applying layered navigation to CMS page filled with new products. I think the answer in the second link will help you.– HollyCommented Oct 9, 2015 at 16:12
-
2this is Magento 2 question, links related to Magento 1.x– FireBearCommented Oct 9, 2015 at 16:34
-
the answer mention is in magento 1.x series , but i want to show categorys on home page (sidebar) in magento 2– sandeep sharmaCommented Oct 10, 2015 at 5:29
Add a comment
|
4 Answers
Here is open source extension for do that - you can use code and modify for your needs Magento 2 Frontend : How to call category collection on home page Explained in blog post
-
1Awesome FireBear. Its work for me. thanks for giving such attention Commented Oct 12, 2015 at 5:25
-
How to show category images I am using $category->getImageUrl(); but its not working Commented Feb 15, 2016 at 10:43
<?php
$categoryHelper = $this->helper('Magento\Catalog\Helper\Category');
foreach($categoryHelper->getStoreCategories() as $category):
?>
<li><a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>"><?php echo $category->getName() ?></a></li>
<?php endforeach; ?>
Check below code:
<?php
$objectManagerr = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManagerr->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryFactory->create()
->addAttributeToSelect('*');
foreach ($categories as $category):
echo $category->getName();
endforeach;
?>
-
1Btw, it's not recommended to use object manager directly for creating objects. Use DI instead. Commented May 25, 2016 at 6:43
-
Is there any way to get the products of current category like above? If any than please share..– YudiCommented Oct 27, 2016 at 13:06
You need to use DI in your class - 1. add property of AdminSession class to class 2. add including of the variable using dependensy injection:
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
public function __construct(
\Magento\Store\Model\StoreManagerInterface $_storeManager
){
$this->_storeManager = $_storeManager;
}
$store = 1;//store id
public function getCategories($store){
$rootCategoryId = $this->_storeManager->getStore($store)
->getRootCategoryId();
$collection = $this->_categoryCollectionFactory->create();
$collection->addAttributeToSelect('name','id')
->addIsActiveFilter(true)
->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"))
->addLevelFilter(2);
foreach ($collection as $category) {
$categories[] = array(
'id'=>$category->getId(),
'name'=>$category->getName(),
);
}
return $categories;
}