3

I've created a new product attribute 'brand'. This attribute is within my default attribute set. Now I'm trying to get all brands from a product collection within a category.

I'm fetching the product collection:

$products = $this->_productCollectionFactory
    ->addAttributeToSelect('brand')
    ->addCategoryFilter($activeCategory)
    ->load();

But the brand is not displayed? The attribute brand is just a simpel text attribute. How can I fetch all brands from this collection?

Thanks

2
  • did you rebuild the indexes? If you have flat catalog enabled, did you mark the attribute as "used in product listing"?
    – Marius
    Feb 29, 2016 at 15:58
  • Marius, thanks a lot. The option 'Used in product listing' was the fix!
    – Silvan
    Feb 29, 2016 at 17:10

1 Answer 1

8

Based on the comments...
when using an attribute in a product collection, if the flat catalog is enabled, the attribute must be marked as Used in product listing in order to be picked up by the collection.

6
  • What if flat catalog isn't enabled? May 17, 2017 at 21:27
  • @SteveRobbins with flat catalog disabled it should work with out any additional actions.
    – Marius
    May 18, 2017 at 6:01
  • Update for 2019: Flat Catalog is considered a performance drag. It should be disabled. Dec 11, 2019 at 17:35
  • Hello @Marius, I have disabled Flat Catalog, And in my custom collection I want few attributes like: $collection->addAttributeToSelect(['special_discount', 'special_discount_value', 'special_offer']);. But this is not working. Once I enabled Flat Catalog it's working. How can I achieve this with Flat Catalog disabled value. Can you please suggest.
    – Ravi Soni
    Feb 25, 2020 at 10:01
  • with flat catalog disabled it should work as you do it. I have no idea why it doesn't
    – Marius
    Feb 25, 2020 at 11:11

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.