I am using a source model for fields of the configuration. I want to get all product attributes (no matter the product or the attribute set) in this source model.
1 Answer
Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
Magento\Eav\Model\Entity\Attribute;
pass the AttributeCollectionFactory to your constructor.
ex :
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributecollectionFactory
.
.
.
//other arguments
) {
$this->attributeCollectionFactory = $attributecollectionFactory;
}
and add this function.
protected function getAllAttributes()
{
$attributeCollection = $this->attributecollectionFactory->create();
/**
* If you want only filterable attributes.
* and add filters as per your requirement.
*/
$attributeCollection->addIsFilterableFilter();
return $attributeCollection->getItems();
}
-
Perfect! Exactly what I was looking for! Just needed to modify the
di.xmlfile to pass the AttributeCollectionFactory to the constructor (what I was missing).Thomas F– Thomas F2017-02-20 09:14:22 +00:00Commented Feb 20, 2017 at 9:14