0

I have defined a custom attribute for my products, and now would like to get a list of all values I have entered for this attribute, not for a specific product, but in general.

It is a text field, it doesn't have any predefined options. Thus, getAllOptions() on the attribute object does not work.

How do I get a list of all entered values for the attribute?

Edit:

I got it to work now. You have to use the \Magento\Catalog\Model\ResourceModel\Product\Collection, then you can alter the collection like this:

    $productCollection = $this->productCollectionFactory->create()->addAttributeToSelect('attributeName')->groupByAttribute('attributeName')->load();
    return $productCollection->getColumnValues('attributeName');

This returns an array of all values saved for this attribute.

  • You need to load the collection and get all column values. – Milind Singh Dec 11 '18 at 7:34
  • 1
    Thanks, that and the usage of the ProductCollection instead of accessing the attributes themselves did the trick. – Dorni Dec 11 '18 at 8:06
1

I got it to work now. You have to use the \Magento\Catalog\Model\ResourceModel\Product\Collection, then you can alter the collection like this:

    $productCollection = $this->productCollectionFactory->create()->addAttributeToSelect('attributeName')->groupByAttribute('attributeName')->load();
    return $productCollection->getColumnValues('attributeName');

This returns an array of all values saved for this attribute.

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.