Php – magento set the value of an attribute

magentoPHP

I have created a new attribute serial number for all the products. Now I don't know how to set its value when order is completed. I know where to change it but I don't have the function. Here is the code. The new attribute have the code "serial_number".

if($status == 'complete'){

    foreach ($this->getAllItems() as $item) {
      // Here I want to update the value, I am sure something like the following will work.         
      $this->setAttribute($item, 'serial_number', '123');

}
}

Also what should be the settings for it in admin. I am changing the value when the status for the order is changed to complete.

Best Answer

here's how you do it differently: (color = attribute name, red = attribute value_id)

Let's start assuming you already have $product available.

$attr = $product->getResource()->getAttribute('color');
if ($attr->usesSource()) {
$avid = $attr->getSource()->getOptionId('red');
$product->setData('color', $avid);
$product->save();
}
Related Topic