Get product attribute’s option id from label and get option label from id in magento 2.

In Magento 2 We can get product Attributes Option Id from the Label and Option label by Option id with Product  Factory object.

Occasionally when you work with the Configurable Product type you may face situations to get Option Id or Label from the Attribute code.

Lets We have taken a Color attribute to demonstrate the usage, Find a Color Label value based on Color Option id.

<?php
namespace Rbj\ProductAttribute\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Catalog\Model\ProductFactory $productFactory
    ) {
        $this->productFactory = $productFactory;
        parent::__construct($context);
    }

    /* Get Label by option id */
    public function getOptionLabelByValue($attributeCode,$optionId)
    {
        $product = $this->productFactory->create();
        $isAttributeExist = $product->getResource()->getAttribute($attributeCode); 
        $optionText = '';
        if ($isAttributeExist && $isAttributeExist->usesSource()) {
            $optionText = $isAttributeExist->getSource()->getOptionText($optionId);
        }
        return $optionText;
    }

   /* Get Option id by Option Label */
    public function getOptionIdByLabel($attributeCode,$optionLabel)
    {
        $product = $this->productFactory->create();
        $isAttributeExist = $product->getResource()->getAttribute($attributeCode);
        $optionId = '';
        if ($isAttributeExist && $isAttributeExist->usesSource()) {
            $optionId = $isAttributeExist->getSource()->getOptionId($optionLabel);
        }
        return $optionId;
    }

Call Method at the specific file,

$optionValue = $this->getOptionLabelByValue('color','50'); // result is blue
$optionId = $this->getOptionIdByLabel('color','Blue'); // result is 50

Assume that, We have Option id 50 and its value is Blue from Attribute code Color.

getOptionIdByLabel() and getOptionLabelByValue() method will be use to get Option Id By Label value and Option Label by Option Id  respectively.

You need to pass only the correct Product Attribute code you want to fetch the value. The given example is working fine for all the custom product attributes in Magento.

How many different types of Eav Entity Types are available in Magento 2?

Magento supports Out-of-the-box Eav entity types for different Entity types.

You can check the list of Eav Entity Type from the eav_entity_type database table.

Out of the box Entity type code supported by Magento,

entity_type_code
1. customer
2. customer_address
3. catalog_category
4. catalog_product
5. sales_order
6. invoice
7. creditmemo
8. shipment
9. rma_item (Magento Commerce)
10.gene_bluefoot_entity (Magento Commerce if Bluefoot is installed)

All of the above Eav Entity Types are available with Magento 2.

How to Load attribute data by attribute code in Magento 2?

We can get attribute data by attribute code by just passing the attribute code in Magento 2.

We have taken entity_type_code  value as Catalog_Product  for a demo in the below code, You can get any entity_type_code as per your requirement.

<?php

public function __construct(
    \Magento\Eav\Model\Entity\Attribute $entityAttribute
) {
    $this->entityAttribute = $entityAttribute;
}

/**
* Load attribute data by code
* @return  \Magento\Eav\Model\Entity\Attribute
*/
public function getAttributeInfo($attributeCode)
{
    return $this->entityAttribute->loadByCode('catalog_product', $attributeCode);
}

Now, Call getAttributeInfo() function from template,

$attributInfo = $this->getAttributeInfo('color');
echo '<pre>';print_r($attributInfo->debug());

The result will be like the below array for color product attributes,

Array
(
    [attribute_id] => 93
    [entity_type_id] => 4
    [attribute_code] => color
    [attribute_model] => Magento\Catalog\Model\ResourceModel\Eav\Attribute
    [backend_model] => Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend
    [backend_type] => int
    [frontend_input] => select
    [frontend_label] => Color
    [is_required] => 0
    [is_user_defined] => 1
    [is_unique] => 0
    [is_global] => 1
    [is_visible] => 1
    [is_searchable] => 1
    [is_filterable] => 1
    [is_comparable] => 1
    [is_visible_on_front] => 0
    [is_html_allowed_on_front] => 0
    [is_used_for_price_rules] => 0
    [is_filterable_in_search] => 1
    [used_in_product_listing] => 0
    [used_for_sort_by] => 0
    [apply_to] => simple,virtual,configurable
    [is_visible_in_advanced_search] => 0
    [position] => 0
    [is_wysiwyg_enabled] => 0
    [is_used_for_promo_rules] => 0
    [is_required_in_admin_store] => 0
    [is_used_in_grid] => 1
    [is_visible_in_grid] => 0
    [is_filterable_in_grid] => 1
    [search_weight] => 1
    [additional_data] => {"update_product_preview_image":"0","use_product_image_for_swatch":"0","swatch_input_type":"visual"}
)