3

I want delete the existing attribute called Manufacturer. Check if this attribute exists before deleting.

How to do this.

Thanks.

1

Try this,

In order to check and remove attribute

if(!$setup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, 'attribute_code')) {
       $eav_setup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'attribute_code');                                                        
}

Complete code on InstallData.php

<?php                                                         
namespace Vendor\Module\Setup;                                      
use Magento\Eav\Setup\EavSetupFactory;                              
use Magento\Framework\Setup\InstallDataInterface;                   
use Magento\Framework\Setup\ModuleContextInterface;                 
use Magento\Framework\Setup\ModuleDataSetupInterface;             
class InstallData implements InstallDataInterface{
 private $eav_setup_factory;

public function __construct(EavSetupFactory $eav_setup_factory)
{
    $this->eav_setup_factory = $eav_setup_factory;
}

public function install(
    ModuleDataSetupInterface $setup,
    ModuleContextInterface $context
) {
    $eav_setup = $this->eav_setup_factory->create(['setup' => $setup]);
    if(!$setup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, 'attribute_code')) {
         $eav_setup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'attribute_code');
    }
    $eav_setup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'attribute_code',
        [
            'type'          => 'varchar',
            'frontend'      => '',
            'label'         => 'Allowed Customer Group',
            'input'         => 'multiselect',
            'class'         => '',
            'attribute_set' => 'Default',
            'global'        => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'visible'       => true,
            'required'      => false,
            'user_defined'  => true,
            'default'       => null,
            'searchable'    => false,
            'filterable'    => false,
            'comparable'    => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique'        => false,
            'apply_to'      => '',
            'system'        => 1,
            'group'         => 'Attributes List'
        ]
    );
    //add product attribute to existing attribute_sets
    $entity_type_id = $eav_setup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
    $attribute_set_ids = $eav_setup->getAllAttributeSetIds($entity_type_id);
    foreach ($attribute_set_ids as $attribute_set_id) {
        $group_id = $eav_setup->getAttributeGroupId($entity_type_id, $attribute_set_id, "group_name");
        $eav_setup->addAttributeToGroup(
            $entity_type_id,
            $attribute_set_id,
            $group_id,
            'attribute_name',
            20
        );
    }
}}

NOTE : If you have module exist already do the same via UpgradeData.php

Hope this helps :)

1

Using Magento\Eav\Api\AttributeRepositoryInterface,you can delete an attribute

it has delete() method which can able to delete the attribute.

Here the code: i assume that attribute entity type is product and attribute code is manufacturer

<?php
namespace {NameSpace};

class {ClassName} {

    /**
     * @var \Magento\Eav\Api\AttributeRepositoryInterface
     */
    private $attributeRepository;

    public function __construct(
       \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository
    ) {

        $this->attributeRepository = $attributeRepository;
    }
    public function deleteAttibute()
    {
        $entityTypeCode = \Magento\Catalog\Model\Product::ENTITY;
        $attributeCode = 'manufacturer';

        try{
            $attributeData = $this->attributeRepository->get($entityTypeCode, $attributeCode);
            $this->attributeRepository->delete($attributeData);

            // goto catch when attribute does not exits
        } catch (\Magento\Framework\Exception\NoSuchEntityException $ex) {
            // // goto catch of type StateException when getting error on Delete
        } catch (\Magento\Framework\Exception\StateException $ex) {

        }

    }
}

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.