2

I would like certain categories to show a listing of subcategories above the products. My current template looks like this:

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
$subcats = $category->getChildrenCategories();
?>
<ul>
    <?php foreach ($subcats as $subcat) : ?>
        <?php if ($subcat->getIsActive()) : ?>
            <li>
                <a href="<?php echo $subcat->getUrl(); ?>" class="block-promo" title="<?php echo $subcat->getName() ?>"><?php echo $subcat->getName() ?></a>
            </li>
        <?php endif ?>
    <?php endforeach ?>
</ul>

I've tried replacing $subcat->getName() with $subcat->getMetaTitle(), hoping some hidden accessor would do its magic, but to no avail.

EDIT:

Following the answer provided, I created a new block:

use Magento\Framework\Registry;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class SubcategoryListing extends Template
{
    protected $_objectManager;
    protected $_registry;

    /**
    * @param Context $context
    * @param Registry $registry
    * @param ObjectManagerInterface $objectManager
    * @param array $data
    */
    public function __construct(
        Context $context,
        Registry $registry,
        ObjectManagerInterface $objectManager,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->_objectManager = $objectManager;
        $this->_registry = $registry;
    }

/**
    * Retrieve child store categories
    *
    */
    public function getSubcategoryListing()
    {
        $repo = $this->_objectManager->get('Magento\Catalog\Model\CategoryRepository');
        $category = $repo->get($this->_registry->registry('current_category')->getId());
        $subcategories = $category->getChildrenCategories();

        foreach ($subcategories as $subcategory) {
            yield $repo->get($subcategory->getId());
        }
    }
}

I then changed the template to:

    <div class="row">
<?php $subcats = $block->getSubcategoryListing(); ?>
<?php foreach ($subcats as $subcat) : ?>
    <?php if ($subcat->getIsActive()) : ?>
        <?php
            $_url = $subcat->getUrl();
            $_name = $subcat->getName();
            $_title = $subcat->getMetaTitle();
        ?>
        <div class="col-md-4 text-center">
            <div class="well">
                <a href="<?php echo $_url; ?>" title="<?php echo $_name ?>">
                    <?php echo $_title != '' ? $_title : $_name ?>
                </a>
            </div>
        </div>
    <?php endif ?>
<?php endforeach ?>
    </div>
    <br class="clearfix" />

This gave me the exact functionality I needed, plus it cleaned up the template quite a bit.

0

Catalog categories are implemented as EAV-based models in Magento 2. What is likely happening is that the meta_title attribute isn't included in the initial load of the catalog category model in this case, for whatever reason. Therefore, you'll want to reload the category model, and that should include the missing attribute.

A good way to do this is in the block, with the category repository interface's getById() method. However, if you can't create a new block, then you can reload the category in your template: $subcat->load($subcat->getId(). That should do it.

The best way would be to declare a plugin before the Magento\Catalog\Model\ResourceModel\Category::getChildrenCategories() method that adds the attribute to the model's collection, which becomes the collection that is returned to include that attribute.

Please let me know if you have any questions.

| improve this answer | |
  • I added a block that fully loads the subcategories via the repository, see my edit for the code and let me know if you have any suggestions/improvements. – Drew DuBois Dec 5 '16 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.