Marco
Marco

Reputation: 237

Magento: Programmatically remove attribute from attribute-set

I need a function or some code to remove an attribute from a set where it is assigned to. I know the function, to assign an attribute:

$setup->addAttributeToSet($entityTypeId, $setId, $groupId, $attributeId, $sortOrder=null)

or to remove an Attribute:

$setup->removeAttribute($entityTypeId, $code)

but the attribute should not be deleted. It must no longer be possible to see the attribute in the AttributeSet 'Default' (group 'General').

I don't find any function like:

removeAttributeFromAttributeSet()

or sth. like that

Upvotes: 7

Views: 9463

Answers (4)

Digisha
Digisha

Reputation: 365

Below will remove attribute from attributeset

<?php 
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $setup->getEntityTypeId('catalog_product');
$attributeId = $setup->getAttributeId('catalog_product', 'feature'); //feature is attribute code
$attributeSetId = $setup->getAttributeSetId($entityTypeId, 'Default');
$installer->deleteTableRow('eav/entity_attribute', 'attribute_id', $attributeId, 'attribute_set_id', $attributeSetId);                    
$installer->endSetup();
?>

Upvotes: 1

Rani Das
Rani Das

Reputation: 19

// It will permanently remove your attribute.

$Attributes = array('accessories_size','accessories_type','apparel_type','author_artist','bag_luggage_type','bedding_pattern','bed_bath_type','books_music_type','camera_megapixels',
'camera_type','coater','color','colors','cost','decor_type','ebizmarts_mark_visited','electronic_type','featured','fit','format','frame_style','gender','gendered','genre','homeware_style',
'home_decor_type','impressions','is_sold','jewelry_type','length','lens_type','luggage_size','luggage_style','luggage_travel_style','make','manufacturer','material','model','necklace_length',
'occasion','perfector','sample_item_1','sheet_size','shoe_size','shoe_type','size','sleeve_length','style');

$entityType = 'catalog_product';

foreach($Attributes as $index => $attrCode){

        $attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode($entityType,$attrCode);   // it return false when attribute not exist.

        if($attributeId){
            Mage::getModel('catalog/product_attribute_api')->remove($attributeId);          
        }
}

Upvotes: 0

Lee Saferite
Lee Saferite

Reputation: 3124

You could try this code inside your setup script

<?php
/** @var $this Mage_Eav_Model_Entity_Setup */
$this->startSetup();

$this->deleteTableRow(
    'eav/entity_attribute', 
    'attribute_id', 
    $this->getAttributeId('catalog_product', 'attribute_code_here'), 
    'attribute_set_id', 
    $this->getAttributeSetId('catalog_product', 'Default')
);

$this->endSetup();

Upvotes: 9

Marco
Marco

Reputation: 237

This is now the complete code I use and wich works:

$installer = $this;
$installer->startSetup();

$attributeType = 'catalog_product';

$attribute_set_name = 'Default';

$attributeCode='my_attribute';

$setId = $installer->getAttributeSetId('catalog_product', $attribute_set_name);

$attributeId=$installer->getAttributeId($attributeType, $attributeCode);

$installer->deleteTableRow('eav/entity_attribute', 'attribute_id', $attributeId, 'attribute_set_id', $setId);

$installer->endSetup();

Upvotes: 0

Related Questions