Tariq Aziz
Tariq Aziz

Reputation: 798

magento set the value of an attribute

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.

Upvotes: 2

Views: 15006

Answers (3)

Hayden Thring
Hayden Thring

Reputation: 1810

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

Upvotes: 5

MagePal Extensions
MagePal Extensions

Reputation: 17656

You seem to be setting the attribute, but where is the code to save() the item to the db

if($status == 'complete'){   
    foreach ($this->getAllItems() as $item) {
         $item->setSerialNumber('123');
         $item->save()   
     }
}

Upvotes: 1

Tejas Shah
Tejas Shah

Reputation: 563

You can do this by using following code -

if($status == 'complete'){

    foreach ($this->getAllItems() as $item) {

         $item->setSerialNumber('123');

     }
}

Hope this will solve your problem.

Upvotes: -2

Related Questions