Magento 设置属性的值


magento set the value of an attribute

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

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

以下是

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

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

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

您似乎正在设置属性,但是将项目保存到数据库的代码在哪里

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

您可以使用以下代码执行此操作 -

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

希望这能解决您的问题。