0

I am trying to get a list of all manufactures (brands) in my block for the current category. (Essentially the "Brands" filter in the layered navigation). This is my current code:

<?php
namespace Sulman\Brands\Block;

class Collection extends \Magento\Framework\View\Element\Template
{
    protected $_categoryFactory;
    protected $_registry;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Framework\Registry $registry,
        array $data = [])
    {
        $this->_registry = $registry;
        $this->_categoryFactory = $categoryFactory;
        parent::__construct($context, $data);
    }

    public function getBrands()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $categoryId = 3;

        $resource = $objectManager->get(\Magento\Framework\App\ResourceConnection::class);
        $connection = $resource->getConnection();

        $select = $connection->select()->from(['ea' => $connection->getTableName('eav_attribute')], 'ea.attribute_id')
            ->join(['eea' => $connection->getTableName('eav_entity_attribute')], 'ea.attribute_id = eea.attribute_id')
            ->join(['cea' => $connection->getTableName('catalog_eav_attribute')], 'ea.attribute_id = cea.attribute_id')
            ->join(['cpe' => $connection->getTableName('catalog_product_entity')], 'eea.attribute_set_id = cpe.attribute_set_id')
            ->join(['ccp' => $connection->getTableName('catalog_category_product')], 'cpe.entity_id = ccp.product_id')
            ->where('cea.is_filterable = ?', 1)
            ->where('ccp.category_id = ?', $categoryId)
            ->group('ea.attribute_id');

        $attributeIds = $connection->fetchCol($select);
        // attributeIds is empty.
        foreach ($attributeIds as $filter){
            echo "x";
        }
    }
}

I took this code from here: http://www.sivaschenko.com/magento2-category-layered-navigation-filterable-attributes/

Nothing is returned.

| improve this question | | | | |
3

Solved this with the help of this post: https://magecom.wordpress.com/2016/06/21/get-layer-filter-and-available-options/

<?php
namespace Sulman\Brands\Block;

class Collection extends \Magento\Framework\View\Element\Template
{

    protected $_registry;
    protected $_layerResolver;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        array $data = [])
    {
        $this->_registry = $registry;
        $this->_layerResolver = $layerResolver;
        parent::__construct($context, $data);
    }

    public function getBrands()
    {
        $category = $this->_registry->registry('current_category');//get current category
        $layer = $this->_layerResolver->get();
        $layer->setCurrentCategory($category->getId());

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $fill = $objectManager->create('Magento\Catalog\Model\Layer\Category\FilterableAttributeList');
        $filterList = new \Magento\Catalog\Model\Layer\FilterList($objectManager,$fill);
        $filterAttributes = $filterList->getFilters($layer);
        $j = 0;
        foreach($filterAttributes as $filter){
            if($filter->getName() == 'Brand'){
                $items = $filter->getItems();
                foreach($items as $item)
                {
                    $filterValues[$j]['display'] = strip_tags($item->getLabel());
                    $filterValues[$j]['label'] = $item->getValue();
                    $filterValues[$j]['count'] = $item->getCount(); //Gives no. of products in each filter options
                    $filterValues[$j]['image'] = $this->getBrandImage(strip_tags($item->getLabel()));
                    $j++;
                }
            }
        }
        return $filterValues;
    }

    private function getBrandImage($label){
        $mediaDir = $this->getUrl('pub/media');
        $lcase = strtolower(str_replace(" ", "-", $label));
        return $mediaDir."brands/".$lcase.".jpg";
    }
}
| improve this answer | | | | |

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.