0

I have created backend form which has one dropdown field. How Do I get all the attributes and show it in a dropdown in magento 2

  • Which attribute - product or customer? – Shireen N Mar 14 '18 at 9:12
  • Are you using UI Form? – Prince Patel Mar 14 '18 at 10:44
  • No, normal php form – Jaisa Mar 14 '18 at 11:14
1

You can use below code...

 protected $_attributeFactory;

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

public function <func_name>()
{
    $attributeInfo = $this->_attributeFactory->create()->addVisibleFilter();

   foreach($attributeInfo as $attributes)
   {
        $attributeId = $attributes->getAttributeId();
        // You can get all fields of attribute here
        //e.g.  $attributes->getFrontendLabel();
   }
}

In above foreach code, you can add options in dropdown as per your requirement.

For filter in above code, I used \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeFactory and then below code

$collection = $this->_attributeFactory->create()->addVisibleFilter();
  • Thank you, It get the attribute collection but failed to add Visible filter to attribute – Jaisa Mar 15 '18 at 4:41
  • You can modify collection code to add filter. – Kishan Patadia Mar 15 '18 at 6:20
  • I have added but not working . So I have posted as a seperate question see below and answer magento.stackexchange.com/questions/217420/… – Jaisa Mar 15 '18 at 6:47
  • I have added like below $attributeInfo = $this->_attributeFactory->getCollection()->addVisibleFilter()->addIsFilterableFilter(); – Jaisa Mar 15 '18 at 6:47
  • I edited my answer for filter. Please check. – Kishan Patadia Mar 15 '18 at 8:12

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.