I have a custom product attribute named additional_cost. Now I want to delete this additional_cost attribute or I want to update it. In order to do this both I need to first check if this attribute already exist.

I want to check the attribute in UpgradeData.php script. So how can I solve this problem

    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);


The removing code is ready. What will be the if statement?

  • 1
    please show your some code of upgradedata.php – Narendra Vyas Mar 26 at 12:11
  • Try using my answer if it works for you. – Narendra Vyas Mar 26 at 12:14
  • Resolved or still facing any issue – Narendra Vyas Mar 26 at 12:23

Try this way:

if($eavSetup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, 'attr_code')) {
   //Create the attribute

This will attempt to retrieve the attribute ID for the attribute with the code attr_code.

  • Thank you it works. I request you to change the if statement. if(!$setup->....) to if($setup->....) – Avesh Naik Mar 26 at 12:32
  • Done. Glad that you made it done. – Narendra Vyas Mar 26 at 12:55

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.