Skip to main content
deleted 430 characters in body
Source Link
Qaisar Satti
  • 32.5k
  • 18
  • 87
  • 137

In addition of @rakesh answer no need of product loading

For direct approach (Object Method)

  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $_product = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product');
  $attr = $_product->getAttribute('model');
  if ($attr->usesSource()) {
        $option_id = $attr->getSource()->getOptionId($info->getMake());
  }

Use Factory Method

   protected $_attributeLoading;

   public function __construct( 
        .....
          \Magento\Catalog\Model\ResourceModel\ProductFactory   $attributeLoading,  
          ....
                                ) {
            parent::__construct($context);
        
            ....
            $this->_attributeLoading = $attributeLoading;
            ....

    }
  

   public function getAttributeOptionId($attribute,$label)
    {
        $poductReource=$this->_attributeLoading->create();
        $attr = $poductReource->getAttribute($attribute);
         if ($attr->usesSource()) {
                return  $option_id = $attr->getSource()->getOptionId($label);
         }
    }
   public function getAttributeOptionText($attribute,$label)
    {
        $poductReource=$this->_attributeLoading->create();
        $attr = $poductReource->getAttribute($attribute);
         if ($attr->usesSource()) {
                return  $option_Text = $attr->getSource()->getOptionText($label);
         }
    }

in phtml file

  $this->getAttributeOptionId('color','//optionLabel');
  $this->getAttributeOptionText('color','//optionId');

In addition of @rakesh answer no need of product loading

For direct approach (Object Method)

  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $_product = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product');
  $attr = $_product->getAttribute('model');
  if ($attr->usesSource()) {
        $option_id = $attr->getSource()->getOptionId($info->getMake());
  }

Factory Method

   protected $_attributeLoading;

   public function __construct( 
        .....
          \Magento\Catalog\Model\ResourceModel\ProductFactory   $attributeLoading,  
          ....
                                ) {
            parent::__construct($context);
        
            ....
            $this->_attributeLoading = $attributeLoading;
            ....

    }
  

   public function getAttributeOptionId($attribute,$label)
    {
        $poductReource=$this->_attributeLoading->create();
        $attr = $poductReource->getAttribute($attribute);
         if ($attr->usesSource()) {
                return  $option_id = $attr->getSource()->getOptionId($label);
         }
    }
   public function getAttributeOptionText($attribute,$label)
    {
        $poductReource=$this->_attributeLoading->create();
        $attr = $poductReource->getAttribute($attribute);
         if ($attr->usesSource()) {
                return  $option_Text = $attr->getSource()->getOptionText($label);
         }
    }

in phtml file

  $this->getAttributeOptionId('color','//optionLabel');
  $this->getAttributeOptionText('color','//optionId');

Use Factory Method

   protected $_attributeLoading;

   public function __construct( 
        .....
          \Magento\Catalog\Model\ResourceModel\ProductFactory   $attributeLoading,  
          ....
                                ) {
            parent::__construct($context);
        
            ....
            $this->_attributeLoading = $attributeLoading;
            ....

    }
  

   public function getAttributeOptionId($attribute,$label)
    {
        $poductReource=$this->_attributeLoading->create();
        $attr = $poductReource->getAttribute($attribute);
         if ($attr->usesSource()) {
                return  $option_id = $attr->getSource()->getOptionId($label);
         }
    }
   public function getAttributeOptionText($attribute,$label)
    {
        $poductReource=$this->_attributeLoading->create();
        $attr = $poductReource->getAttribute($attribute);
         if ($attr->usesSource()) {
                return  $option_Text = $attr->getSource()->getOptionText($label);
         }
    }

in phtml file

  $this->getAttributeOptionId('color','//optionLabel');
  $this->getAttributeOptionText('color','//optionId');
Source Link
Qaisar Satti
  • 32.5k
  • 18
  • 87
  • 137

In addition of @rakesh answer no need of product loading

For direct approach (Object Method)

  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $_product = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product');
  $attr = $_product->getAttribute('model');
  if ($attr->usesSource()) {
        $option_id = $attr->getSource()->getOptionId($info->getMake());
  }

Factory Method

   protected $_attributeLoading;

   public function __construct( 
        .....
          \Magento\Catalog\Model\ResourceModel\ProductFactory   $attributeLoading,  
          ....
                                ) {
            parent::__construct($context);
        
            ....
            $this->_attributeLoading = $attributeLoading;
            ....

    }
  

   public function getAttributeOptionId($attribute,$label)
    {
        $poductReource=$this->_attributeLoading->create();
        $attr = $poductReource->getAttribute($attribute);
         if ($attr->usesSource()) {
                return  $option_id = $attr->getSource()->getOptionId($label);
         }
    }
   public function getAttributeOptionText($attribute,$label)
    {
        $poductReource=$this->_attributeLoading->create();
        $attr = $poductReource->getAttribute($attribute);
         if ($attr->usesSource()) {
                return  $option_Text = $attr->getSource()->getOptionText($label);
         }
    }

in phtml file

  $this->getAttributeOptionId('color','//optionLabel');
  $this->getAttributeOptionText('color','//optionId');