I want to retrieve all product attributes those are available, then convert it to name & value for my select options field. In Magento 1 I can achieve it like this:

public function getMagentoAttributes()
    $values[] = array(
        'value' => '',
        'label' => 'Pick Product Attribute'

    $categories = Mage::getResourceModel('catalog/product_attribute_collection')->getItems();

    foreach ($categories as $category) {
        if ($category->getFrontendLabel() != '') {
            $label = $category->getFrontendLabel();
        } else {
            $label = $category->getAttributecode();

        $values[] = array(
            'value' => $category->getAttributecode(),
            'label' => $label
    return $values;

Is there a way in magento 2 to do the same thing?

  • I have used code according to "RonakChauhan" it is working fine in my block file But I am facing some issue i need help in that I am unable to filter attributes according to their visibility i.e I need the attributes whose status is set "visible => yes" in Admin...Any Help will be Appreciated... Here is my Code for getting Product Attribute's collection class ProductList extends \Magento\Framework\View\Element\Template{ protected $_attributeFactory; public function __construct( \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory ){ parent::__construct($context); $this->_attrib – Gurjeet Singh Feb 27 '18 at 9:58
protected $_attributeFactory;

 public function __construct(
    \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory,
) {
    $this->_attributeFactory = $attributeFactory;

public function <func_name>()
    $attributeInfo = $this->_attributeFactory->getCollection();

   foreach($attributeInfo as $attributes)
        $attributeId = $attributes->getAttributeId();
        // You can get all fields of attribute here

Here you can have whole collection of attributes, you can filter it as per your need.

|improve this answer|||||
  • how to get the attribute name and id? – simple guy Mar 10 '17 at 7:17
  • using foreach you can get getAttributeId() as well as getAttributeName() – Ronak Chauhan Mar 10 '17 at 7:19
  • Check updated answer – Ronak Chauhan Mar 10 '17 at 7:21
  • getAttributeName print blank – simple guy Mar 10 '17 at 7:24
  • 1
    echo "<pre>"; print_r($attributes);exit; use this in foreach and check – Ronak Chauhan Mar 10 '17 at 7:25

Another idea is that we should try with Service Contracts Layer.

Use Magento\Eav\Api\AttributeRepositoryInterface to get the eav attribute.

I have an answer already here: https://magento.stackexchange.com/a/161426/33057

For example:

    $searchCriteria = $this->searchCriteriaBuilder->create();
    $attributeRepository = $this->attributeRepository->getList(

    foreach ($attributeRepository->getItems() as $items) {

NOTE: For the entity type code in getList method, we can find in the eav_entity_type table.

|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.