Domanda

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.

È stato utile?

Soluzione

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);
}

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top