判断自定义的产品属性是否存在
判断自定义的产品属性是否存在
比如判断color属性是否存在。
_eavConfig = $eavConfig;
}
/**
* Returns true if attribute exists and false if it doesn't exist
*
* @param string $field
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function isProductAttributeExists($field)
{
$attr = $this->_eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $field);
return ($attr && $attr->getId()) ? true : false;
}
}
在phtml里可以这样调用
$hasColor = $this->helper('Zou\Demo\Helper\Custom')->isProductAttributeExists('color');
