Methode 1 - Verwenden der Abhängigkeitsinjektion (DI)
Hier ist der Beispielcode zum Abrufen der Liste aller Kategorien in Magento 2 mithilfe der Abhängigkeitsinjektion.
Um die Kategoriedaten abzurufen, müssen wir möglicherweise das Objekt \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
und die \Magento\Catalog\Helper\Category
Klassen in den Konstruktor der Blockklasse unseres Moduls einfügen und über die Ansichtsdatei (.phtml) darauf zugreifen.
app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php
<?php
namespace YourCompanyName\YourModuleName\Block;
class YourCustomBlock extends \Magento\Framework\View\Element\Template
{
protected $_categoryCollectionFactory;
protected $_categoryHelper;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
\Magento\Catalog\Helper\Category $categoryHelper,
array $data = []
) {
$this->_categoryCollectionFactory = $categoryCollectionFactory;
$this->_categoryHelper = $categoryHelper;
parent::__construct($context, $data);
}
public function getCategoryCollection($isActive = true, $level = false, $sortBy = false, $pageSize = false) {
$collection = $this->_categoryCollectionFactory->create();
$collection->addAttributeToSelect('*');
// select only active categories
if ($isActive) {
$collection->addIsActiveFilter();
}
// select categories of certain level
if ($level) {
$collection->addLevelFilter($level);
}
// sort categories by some value
if ($sortBy) {
$collection->addOrderField($sortBy);
}
// set pagination
if ($pageSize) {
$collection->setPageSize($pageSize);
}
return $collection;
}
/**
* Retrieve current store level 2 category
*
* @param bool|string $sorted (if true display collection sorted as name otherwise sorted as based on id asc)
* @param bool $asCollection (if true display all category otherwise display second level category menu visible category for current store)
* @param bool $toLoad
*/
public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true) {
return $this->_categoryHelper->getStoreCategories($sorted = false, $asCollection = false, $toLoad = true);
}
}
Jetzt können wir die Funktionen in unserer Ansichtsdatei (.phtml) wie folgt verwenden.
// get the list of all categories
$categories = $block->getCategoryCollection();
foreach ($categories as $category) {
echo $category->getId() . '<br />';
echo $category->getName() . '<br />';
}
// get categories sorted by category name
$categories = $block->getCategoryCollection(true, false, 'name', false);
foreach ($categories as $category) {
echo $category->getId() . '<br />';
echo $category->getName() . '<br />';
}
// get current store’s categories
$categories = $block->getStoreCategories();
foreach ($categories as $category) {
echo $category->getId() . '<br />';
echo $category->getName() . '<br />';
}
Methode 2 -
Verwenden des Objektmanagers
Hier ist der Code, um die Liste aller Kategorien in Magento 2 mit dem Objektmanager abzurufen.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
// get the list of all categories
$categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryCollection->create();
$categories->addAttributeToSelect('*');
foreach ($categories as $category) {
echo $category->getId() . '<br />';
echo $category->getName() . '<br />';
echo $category->getUrl() . '<br />';
}
// get current store’s categories
$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categories = $categoryHelper->getStoreCategories();
foreach ($categories as $category) {
echo $category->getId() . '<br />';
echo $category->getName() . '<br />';
}