Magento

How to Get all Options of a Custom Attribute in Magento 2

Written by Jigar Patel

Oct 13

How to Get all Options of a Custom Attribute in Magento 2

Magento 2 product attributes are generally the properties of the Magento 2 product that help customers choose between product variations and find the best suitable option. It could be color, size, weight, material, etc.

To learn more about creating and managing product attributes, see:

There are two types of attributes used by Magento 2:

  • Default or system attributes are required for a store’s proper functioning and can never be deleted. In the Stores> Attributes> Product section, you can find default attributes (including name, price, URL, SKU, descriptions, images, etc.).
  • Custom attributes are created by the store admin. You can add any number of attributes to this section.

There are 2 ways to add new product attributes in Magento 2:

  • When working on a specific product edit page
  • From the Product Attributes page

Under Attribute properties, describe basic information of the product attributes Magento 2.

  • Attribute code: the unique identifier to manage the attribute: search, import/export, get attribute ID/name/value, etc.
  • Scope: Decide how the attribute value is displayed and edited based on a specific level: Global ⇒ Website ⇒ Store View
  • Global: The value of the attribute is shared by all websites and store views.
  • Website: The value varies per website but stays the same for multiple store views under that website.
  • Store View: Tailor different values per store view, primarily based on the languages.
  • Set Unique Value to Yes to prevent duplication. Recommended for ID-number fields like SKU.
  • Attributes used for the configurable products must have Catalog Input Type to be drop-down and the Global scope.

Following are Catalog Input Types to define the product attributes Magento 2.

    • Text Field: A single-line field to enter text
    • Text Area: A multiple-line field to enter text directly or using  WYSIWYG Editor
    • Text Editor: Add WYSIWYG Editor to any custom field
    • Date: Date & Time can be picked from the calendar or typed directly into a field
    • Yes/No: A “switch” button between two pre-defined options: Yes or No
    • Dropdown: A drop-down list to choose one option at a time
    • Multiple Select:  Used to create price fields. The currency used is determined in the system configuration
    • Media Image: A media image attribute becomes an additional image type, along with Base, Small, and Thumbnail
    • Fixed Product Tax: Used to define FPT rates based on the locale requirements
    • Visual Swatch: For configurable products, the corresponding image is shown per swatch on visual option (e.g color)
    • Text Swatch: For configurable products, the corresponding image is shown per swatch on text option (e.g size)

    How To Get All Options Of a Custom Attribute in Magento 2

    Using the below codes can Get All Options Of Attribute By Attribute Code In Magento 2.

    <?php
    
    namespace Dolphin\Mymodule\Ui\Component\Form\Color;
    
    use Magento\Framework\Data\OptionSourceInterface;
    use \Magento\Eav\Model\Config;
    
    class Options implements OptionSourceInterface
    {
        /**
         * @var Config
         */
        protected $_eavConfig;
    
        /**
         * @param Config $eavConfig
         */
        public function __construct(
            Config           $eavConfig
        )
        {
            $this->_eavConfig = $eavConfig;
        }
    
        /**
         * {@inheritdoc}
         */
        public function toOptionArray()
        {
            $coloOptions = $this->getColorOption();
            return $coloOptions;
        }
    
        protected function getColorOption()
        {
            $attribute = $this->_eavConfig->getAttribute('catalog_product', 'color'); //color is product attribute.
            $options = $attribute->getSource()->getAllOptions();
            $optionsExists = array();
            /*foreach ($options as $option) {
                if ($option['value'] > 0) {
                    $optionsExists[] = $option['label'];
                }
            }*/
            return $options;
        }
    
    }
    • $this->_eavConfig->getAttribute: This method have two parameter first is entityType and second is attribute code.
    • Color attribute from Catalog_Product Eav Entity Types. Fetch all the options of the color attribute.

    Like to read: Product Custom Option Templates for Magento 2

    Result:

    Array
    (
        [0] => Array
            (
                [label] =>  
                [value] => 
            )
    
        [1] => Array
            (
                [value] => 49
                [label] => Black
            )
    
        [2] => Array
            (
                [value] => 50
                [label] => Blue
            )
    
        [3] => Array
            (
                [value] => 51
                [label] => Brown
            )
    
        [4] => Array
            (
                [value] => 52
                [label] => Gray
            )
    
        [5] => Array
            (
                [value] => 53
                [label] => Green
            )
    
    )

    I Hope, This instruction will be helpful for you.

    If you have any difficulties regarding this blog, do consider them posting in the Comments section below!

    I’m here to help.

    Thank you!

Written by Jigar Patel

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.

TO TOP