Question

I am updating produts programmatically and it sometimes happens that an attribute for a product is not used anymore. In that case, the attribute value should of course be removed. However, I can not find a way to do that. The update works just fine, but values aren't getting removed from the model.

I have looked around a bit and it seemed that the following would be the solution:

$product->unsetData($attributeCode);

But this does not remove the value. The attribute code is correct and everything else works perfectly fine. I also checked and it does use the right store.

I have gotten suggestions on doing this directly through the database, but I'd rather do it on the model for safety.

Was it helpful?

Solution

I suggest making the attribute's value empty:

$product->setData($attributeCode, '');

or:

$product->setAttributeCode('');

or, as you have saved the product you need to save the product object:

$product->unsetData($attributeCode);
$product->save();

This will take effect after the object is saved.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top