1

I want to remove an attribute from its attribute set, programmatically. I have this code:

Mage::getModel('catalog/product_attribute_set_api')
    ->attributeRemove($attributeId, $attributeSetId);

I want this code to be run only one time. Where should I put it? It seems I can't put it in a SQL upgrade file.

  • 1
    And why can't you put it into SQL upgrade ? That seems to be a logical place for this. – Prateek Dec 30 '15 at 14:34
4

I think it should not be a problem to put the attribute removal intro an sql upgrade script.

Just make sure to check if the attribute exists before you remove it. Then you should not have an issue with multiple users calling/triggerin the sql upgrade script at the same time:

Something like this should help:

$entity = Mage_Catalog_Model_Product::ENTITY;
$attributeCode = 'status';
$attributeSetId = '4'; //your attribute set value

$attr = Mage::getResourceModel('catalog/eav_attribute')
    ->loadByCode($entity,$attributeCode);

if ($attr->getId()) {
    // atttribute exists, remove it
    Mage::getModel('catalog/product_attribute_set_api')
      ->attributeRemove($attr->getId(), $attributeSetId);
}
1

You can put any code in the upgrade scripts, so it would work. But it does not make much sense to use the API there.

The setup resource model has a method removeAttribute($entityCode, $attributeCode) that you can use:

$this->removeAttribute("catalog_pruduct", "the_attribute") ;

Update: sorry, I misread the question. The code above deletes the attribute. You should also be able to remove it from a set with the setup resource but I'm on mobile right now and can't look it up

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.