14

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?

1
  • 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 Feb 27, 2018 at 9:58

2 Answers 2

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

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

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(
        'catalog_product',
        $searchCriteria
    );

    foreach ($attributeRepository->getItems() as $items) {
        $items->getAttributeCode();
        $items->getFrontendLabel();
    }

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

2
  • What is 'catalog_product' as an argument in getList()?? Oct 23, 2020 at 10:25
  • This should be the accepted answer! Jun 4, 2021 at 15:01

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.