Witam
jak w Magento2 w swoim bloku (albo phtmlu) pobrać główną nazwę kategori ROOT całego drzewa (domyślnie nazywa się: "Default ". Zmieniłem jej nazwę i chciałbym ją pobrać do phtmla.
Prosze o pomoce.
To mój blok:
<?php
namespace Bloczek\Mymenu\Block;
class Mymenu extends \Magento\Framework\View\Element\Template
{
protected $_categoryHelper;
protected $categoryFlatConfig;
protected $topMenu;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Catalog\Helper\Category $categoryHelper
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Helper\Category $categoryHelper,
\Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
\Magento\Theme\Block\Html\Topmenu $topMenu
) {
$this->_categoryHelper = $categoryHelper;
$this->categoryFlatConfig = $categoryFlatState;
$this->topMenu = $topMenu;
parent::__construct($context);
}
/**
* Return categories helper
*/
public function getCategoryHelper()
{
return $this->_categoryHelper;
}
/**
* Return top menu html
* getHtml($outermostClass = '', $childrenWrapClass = '', $limit = 0)
* example getHtml('level-top', 'submenu', 0)
*/
public function getHtml()
{
return $this->topMenu->getHtml();
}
/**
* Retrieve current store categories
*
* @param bool|string $sorted
* @param bool $asCollection
* @param bool $toLoad
* @return \Magento\Framework\Data\Tree\Node\Collection|\Magento\Catalog\Model\Resource\Cat
egory\Collection|array
*/
public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
{
return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
}
/**
* Retrieve child store categories
*
*/
public function getChildCategories($category)
{
if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource()) {
$subcategories = (array)$category->getChildrenNodes(); } else {
$subcategories = $category->getChildren();
}
return $subcategories;
}
}
w phtmlu:
// np. wyświetlenie kategorii:
<?php
$categories = $this->getStoreCategories(true,false,true);
$categoryHelper = $this->getCategoryHelper();
foreach($categories as $category):
if (!$category->getIsActive()) {
continue;
}
?>
<a href="
<?php echo $categoryHelper->getCategoryUrl($category) ?>" class="c">
<?php echo $category->getName() ?></a>
// ...
jak przed tymi kategoriami pobrać i wyświetlić ROOT ?
Ten post edytował dopelganger 26.05.2017, 09:17:32