To set an attribute val I would use:


Now I have the name of my attribute in a string.

So how can I merge:

$attribute = 'Nameofmyattribute';
$product->set[here comes my string]('somestuff');

You can use setDataUsingMethod() defined in Varien_Object.
For example, if your attribute is called some_attribute the following are identical:

$product->setDataUsingMethod('some_attribute', 'some_value');

[Edit: and a bit off topic]
It works the same way for getters:


is the same as

  • That's a really creative means of handling it. – philwinkle Jul 8 '13 at 14:02
  • Thanks, this works great. And no, it's not off topic but very helpful! :-) – Bob van Luijt Jul 8 '13 at 14:54
  • Great help! If one uses the setDataUsingMethod method ... how then can we save the value? do we issue saveAttribute or need to product->save (rather not the last) ..... help appreciated – snh_nl Jun 26 '17 at 7:46

PHP can interpret this automatically, but you'll have to prepend the set keyword:

$attribute = 'Nameofmyattribute';
$setter = 'set' . $attribute;

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.