Magento – How to check if attribute value exists in Magento2

attributesmagento2

How to check if attribute value exists in magento2…

Best Answer

public function __construct(
        \Magento\Catalog\Model\ProductFactory $productFactory
    ) {       
        $this->_productFactory = $productFactory;    
    }

Now,

$attr = $this->_productFactory->create()->getResource()->getAttribute('manufacturer');
if($attr){
    $label = 'test';
    $optionId = '';
    $getId = $attr->getSource()->getOptionId($label);
    if(!empty($getId)){
        $optionId = $getId;
    }
    return $optionId;      
}