How to get option label from dropdown in Magento 2

- Fire development, Magento 2

get option label from dropdown in Magento 2

A frequent search task among Magento developers is to get an option label from a dropdown. However, it is quite confusing for beginners since the actual value of a drop-down product attribute is a number. Many newcomers expect that it is a text. But there is nothing to worry about. Below, we explain how to get an option label from a dropdown in Magento 2 as well as shed light on everything you need to know to begin working with dropdown product attributes.  More coding tips are available here: Magento 2 Developer’s Cookbook.

'

How to get option label from dropdown in Magento 2

If you are familiar with how to get an option label from a dropdown in Magento 1, you can leverage the same procedure for Magento 2. If not, use getOptionTextfunction. The only nuance is that you need to use it step by step. First, get a product using the code below:

You can find other methods here: How to get a product by ID in Magento 2.

Now, you can get an option label from a dropdown with the following line of code:

Note that design_color is the name of a dropdown attribute. Replace it with your unique name.

The code sample above lets you get a label of an attribute which is set in a product backend. However, there are situations when you need to get a label based on an option ID that you know.

How to get a label based on an option ID in Magento 2

Since a dropdown product attribute value is an option ID, you can use an internal database ID to get the corresponding value regarding your local store. Use the following code snippet:

How to get an option ID based on a label

If you know a label, you can get the corresponding option ID as well. To do that, use the following code snippet:

How to create a new dropdown product attribute

And if you do not know how to create new dropdown product attributes, here is a step-by-step guide:

  1. Go to Stores –> Product;
  2. Hit the “Add New Attribute” button;
  3. Define your new product attribute;
  4. Save your new attribute;
  5. Add it to your attribute set.

Additional Improvements

Since we are talking about options in a dropdown menu, which is frequently used in case of configurable products, it is necessary to say a few words about them. Below, we explain how to use configurable products to full potential. You will discover how to take advantage of B2B bulk ordering, dynamic attribute changes, improved SEO, advertising options, etc.

Although the default functionality of configurable products in Magento 2 is impressive, there is still room for improvement. Therefore, we’d like to introduce you to the Improved Configurable Product Magento 2 extension. The module offers the following features and enhancements:

  • Dynamic content update for category and product pages. Every time a new option is selected, the extension changes a page URL, product name, SKU, description, and other attributes without reloads;
  • Flexible product price range display is utilized to inform customers about the minimal and maximal price of the configurable product option;
  • Default variation of a configurable product dramatically simplifies shopping since you can provide the most popular option configuration out-of-the-box;
  • Matrix grid for configurable product options enables bulk ordering – a vital B2B feature that allows customers to purchase multiple product options from a single page simultaneously;
  • Enhanced bundle enables you to add configurable products to Magento 2 bundle products;
  • Custom options for configurable products are supported!
  • Product options pre-select with page URL enables simple sharing of each configurable product option;
  • Target advertising of specific products;
  • Full support for tier and group prices;
  • Custom shipping logic;
  • SEO features with metadata and Schema.org dynamic updates.

Now, let’s describe the extension’s core improvements individually.

Product matrix

Magento 2 Configurable Product Matrix grid lets customers add multiple product options to the cart simultaneously, enabling Magento 2 quick order functionality. As a store administrator, you can allow tier prices in the matrix grid as well as the display of product quantity. Thus, it’s a must-have B2B improvement unavailable in Magento 2 by default. 

Every wholesaler with configurable products can dramatically improve the lives of their customers but introducing the enhanced shopping experience.  The Magento 2 Improved Configurable Products extension eliminates the necessity to add each product option to the cart individually. Instead, customers can place multiple options of the same configurable product simultaneously. Such a vital enhancement dramatically speeds up the ordering process. The gif below illustrates how everything happens on the frontend:

get option label from dropdown in Magento 2

As you can see, the Improved Configurable Product extension also adds the ‘Available Qty’ column to the grid informing your clients about the number of available configurable product options. 

Each option also includes a stock status and individual price. Furthermore, you can enable tier prices for the grid.

Enhanced bundles

With our extension, you can also add configurable products to bundle product options without any headaches. After the module is installed, you can find the corresponding option in bundle configurations.

On a bundle product page, customers can use the ‘customize’ button to select necessary options, which are displayed as thumbnails. Although only one configurable product variation can be added to the cart at a time, multiple bundle options are possible. Thus, a customer can add various bundle variation to cart one by one.

get option label from dropdown in Magento 2

Custom shipping logic

Another vital feature of the Improved Configurable Product Magento 2 extension is the ability to display custom messages for every option of a configurable product.

The module shows a specified message if the current date of the customer’s visit to the product page is between the specified start and end date set per product. Thus, you inform customers about the possible delivery date of in real-time.

get option label from dropdown in Magento 2

Conclusion

You can find more information about how to get an option label from a dropdown in Magento 2 in this article: Magento 2 – get option label from the dropdown. For further information about attributes in Magento 2, follow these links: 

'