In being careful, I want to check if my custom attribute exists before I attempt to utilize it in any way. I need to check against all attributes collection, not just the product collection. My attribute may exist on a customer, a product, or on a custom model of my own creation.

In magento 1.x, I'd use the following:

$attr = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('specialty')->getFirstItem();

if ($attr->getAttributeId() > 0) {
 Do some stuff....'

I can't find anything that explicitly does something like this


2 Answers 2


You need use \Magento\Eav\Api\AttributeRepositoryInterface::get method for this.

For example:

try {

   $attribute = $this->attributeRepository->get($entityType, $attributeCode);

} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {

    //  attribute does not exist

  • Hi Kandy , how to check the custom attribute assigned or not for the product? Commented Oct 10, 2018 at 3:08

You may also use Magento\Catalog\Api\ProductAttributeRepositoryInterface to determine the product attribute

    try {
        } catch (NoSuchEntityException $e) {

            //To-do create attribute

This is verified on 2.3.2

Your Answer

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

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