1

How to check if attribute value exists in magento2...

5
  • are you talking about attribute option value ? Sep 1, 2017 at 5:26
  • Yes, i want to check attribute option exist of not.. Sep 1, 2017 at 5:27
  • Based on option label? Sep 1, 2017 at 5:29
  • Exactly!! I want this Sep 1, 2017 at 5:30
  • try $this->getProduct()->getAttributeText('attribute'); and also this for multiselect $this->getProduct()->getResource()->getAttributeRawValue($this->getProduct()->getId(),'attribute',$this->_storeManager->getStore()->getId()); Sep 1, 2017 at 5:40

2 Answers 2

1
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;      
}
2
  • please let me know if yoy have still issue Sep 1, 2017 at 6:22
  • hi @Rakesh, How to check if option id exist or not? Feb 13, 2019 at 6:55
0

try

$this->getProduct()->getAttributeText('attribute');

and this for multiselect

$this->getProduct()->getResource()->getAttributeRawValue($this->getProduct()->getId(),'attribute',$this->_storeManager->getStore()->getId());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.