1

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

  • are you talking about attribute option value ? – Rakesh Jesadiya Sep 1 '17 at 5:26
  • Yes, i want to check attribute option exist of not.. – Chandra Kumar Sep 1 '17 at 5:27
  • Based on option label? – Rakesh Jesadiya Sep 1 '17 at 5:29
  • Exactly!! I want this – Chandra Kumar Sep 1 '17 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()); – Sameer Bhayani Sep 1 '17 at 5:40
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;      
}
  • please let me know if yoy have still issue – Rakesh Jesadiya Sep 1 '17 at 6:22
  • hi @Rakesh, How to check if option id exist or not? – jafar pinjar Feb 13 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, privacy policy and cookie policy

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