enter image description here

How do I get all product attribute in select option in the custom form in Magento 2. Do you have any idea.

  • have you created that form using UI component? – Balwant Singh Aug 27 '19 at 13:55
  • No, This is Magento's product attribute table. and I want to get these properties to put in the select option in my custom form. – MichaelHa Aug 28 '19 at 1:24
  • I am asking about your custom form. Is that form created using UI Component? – Balwant Singh Aug 28 '19 at 10:56
  • oh yes, sorry for not understanding you, do you have any idea. – MichaelHa Aug 28 '19 at 13:12
  • i guess below my answer will fulfil your requirement. Try once. – Balwant Singh Aug 28 '19 at 14:07

Try below code:

In your UI form


<field name="pro_attr">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Namespace\ModuleName\Model\Config\Source\Proatr</item>
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Product Attribute List</item>
                    <item name="componentType" xsi:type="string">field</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</item>
                    <item name="elementTmpl" xsi:type="string">ui/grid/filters/elements/ui-select</item>
                    <item name="dataScope" xsi:type="string">coupon_code</item>
                    <item name="filterOptions" xsi:type="boolean">false</item>
                    <item name="showCheckbox" xsi:type="boolean">true</item>
                    <item name="chipsEnabled" xsi:type="boolean">true</item>
                    <item name="disableLabel" xsi:type="boolean">true</item>
                    <item name="levelsVisibility" xsi:type="number">1</item>
                    <item name="multiple" xsi:type="boolean">0</item>
                    <item name="sortOrder" xsi:type="number">2</item>
                    <item name="listens" xsi:type="array">
                        <item name="newOption" xsi:type="string">toggleOptionSelected</item>
                    <item name="required" xsi:type="boolean">0</item>
                    <item name="source" xsi:type="string">conditions</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>


 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
namespace Namespace\ModuleName\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

 * Class Coupon
class Proatr implements OptionSourceInterface
    protected $_attributeFactory;

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

     * Get options
     * @return array
    public function toOptionArray()
        $availableOptions[] = array('value' => 0, 'label' => '--- Please Select ---');
        $attributeInfo = $this->_attributeFactory->create()->addVisibleFilter();

       foreach($attributeInfo as $attributes)
            $attributeId        = $attributes->getAttributeId();
            // You can get all fields of attribute here
            $attrlabel          = $attributes->getFrontendLabel();
            $availableOptions[] = array('value' => $attributeId, 'label' => $attrlabel);
        //echo '<pre>';print_r($availableOptions);die;
        return $availableOptions;   

I hope it will help...!!!

  • It works, Thank you, Keeps helping. – MichaelHa Aug 29 '19 at 5:00
  • @MichaelHa : welcome :) happy coding...!!! – Balwant Singh Aug 29 '19 at 5:15

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.