• My shop has an attribute manufacturer.
  • This attribute has option values like adidas and nike etc.
  • I have a custom module with a block
  • I placed the block on a specific category through a widget
  • The category has several products assigned to it
  • The products each have an attribute value set to it (adidas and nike etc.)

How can I retrieve all the option value ID's (for adidas and nike etc.) in my custom block for the category which is currently loaded?

  • Are you looking for product collection with category and attribute filter right?
    – S.P
    May 9, 2022 at 15:18
  • No, I just want a list of the attribute option value ID's
    – Akif
    May 9, 2022 at 15:20
  • $attribute = $this->eavConfig->getAttribute('catalog_product', 'attribute_code_here'); $options = $attribute->getSource()->getAllOptions();
    – S.P
    May 9, 2022 at 15:27
  • \Magento\Eav\Model\Config $eavConfig $this->eavConfig = $eavConfig; $attribute = $this->eavConfig->getAttribute('catalog_product', 'attribute_code_here'); $options = $attribute->getSource()->getAllOptions();
    – S.P
    May 9, 2022 at 15:28
  • Thanks for the suggestions. This retrieves all the option values in the system. I only want the one assigned to the current category I am in. So this is not the solution I am looking for.
    – Akif
    May 9, 2022 at 15:39

1 Answer 1

 protected $eavConfig;
 protected $_productCollectionFactory;
 protected $_registry;
 public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Framework\Registry $registry,
        \Magento\Eav\Model\Config $eavConfig
       ) {
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->eavConfig = $eavConfig;
    $this->_registry = $registry;        

If your requirement for multple category then pass category ids in below product collections.

Use below code in your function.

        $category = $this->_registry->registry('current_category');
        $ids = $category->getId();
        $collection = $this->_productCollectionFactory->create();
        $collection->addCategoriesFilter(['in' => $ids]);
        $collection->addAttributeToFilter('manufacturer',  ['neq' => 'NULL']);
        $categoryoptions = $collection->getColumnValues('manufacturer');        
        $attribute = $this->eavConfig->getAttribute('catalog_product', 'manufacturer');
        $options = $attribute->getSource()->getAllOptions();        
        $optionsExists = array();
        foreach($options as $option)
            if(in_array($option['value'] , $categoryoptions))
                $optionsExists[$option['value']] = $option['label'];
        echo "<pre>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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