magento 设置属性的值

Tar*_*ziz 2 php magento

我为所有产品创建了一个新的属性序列号。现在我不知道如何在订单完成时设置它的值。我知道在哪里更改它,但我没有该功能。这是代码。新属性的代码为“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');

}
}
Run Code Online (Sandbox Code Playgroud)

此外,它在管理中的设置应该是什么。当订单状态更改为完成时,我正在更改该值。

Hay*_*ing 5

以下是您如何以不同方式执行此操作:(颜色 = 属性名称,红色 = 属性 value_id)

让我们开始假设您已经有 $product 可用。

$attr = $product->getResource()->getAttribute('color');
if ($attr->usesSource()) {
$avid = $attr->getSource()->getOptionId('red');
$product->setData('color', $avid);
$product->save();
}
Run Code Online (Sandbox Code Playgroud)