How to check if attribute value exists in magento2...
-
are you talking about attribute option value ?– Rakesh JesadiyaSep 1, 2017 at 5:26
-
Yes, i want to check attribute option exist of not..– Chandra KumarSep 1, 2017 at 5:27
-
Based on option label?– Rakesh JesadiyaSep 1, 2017 at 5:29
-
Exactly!! I want this– Chandra KumarSep 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());– Sameer BhayaniSep 1, 2017 at 5:40
Add a comment
|
2 Answers
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;
}
try
$this->getProduct()->getAttributeText('attribute');
and this for multiselect
$this->getProduct()->getResource()->getAttributeRawValue($this->getProduct()->getId(),'attribute',$this->_storeManager->getStore()->getId());