我想在添加到购物车时更改自定义属性集(凭证)的自定义属性(voucher_value)值。我在观察者事件checkout_cart_product_add_after中尝试过。我可以设法更改价格,但无法更改自定义属性voucher_value的值。
这是我的功能
public function change_price($observer) {
// Get the quote item
$item = $observer->getQuoteItem();
// Ensure we have the parent item, if it has one
$item = ($item->getParentItem() ? $item->getParentItem() : $item );
Mage::log("Item Details: ".print_r($item, true), null, 'item_before1.log');
// Load the custom price
$price = Mage::app()->getRequest()->getPost('custom_voucher_price');
if(!empty($price)){
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$product->addAttributeUpdate('voucher_value', 39 , 1);
//$product->addCustomOption('voucher_value_value', 259 , $product);
$product->save();
$voucher_discount = $product->getAttributeText('voucher_discount');
$voucher_value = $product->getAttributeText('voucher_value');
Mage::log("voucher_value: ".print_r($voucher_value, true), null, 'voucher_value.log');
$voucher_discount_amount = 0;
if($voucher_discount!=null && (!empty($voucher_discount))){
$voucher_discount = floatval($voucher_discount);
$voucher_discount_amount = $price * $voucher_discount/100;
}
else{
$voucher_discount = 0;
}
$custom_price = $price - $voucher_discount_amount;
$item->setCustomPrice($custom_price);
$item->setOriginalCustomPrice($custom_price);
$info = $item->getOptions();
$option = Mage::getModel('sales/quote_item_option')
->setProductId($product->getId())
->setCode('voucher_value')
->setProduct($product) // needed for EE only ?
->setValue(39);
$item->addOption($option);
$product = $observer->getProduct();
$item->setCustomAttribute($product->getCustomAttribute());
// $item->setOptions($option);
// $item->setData('voucher_value_value', $price ); */
}
$item->save();
// Enable super mode on the product.
$item->getProduct()->setIsSuperMode(true);
}
请帮助如何更改自定义属性集的自定义属性的值。
答案 0 :(得分:0)
您可以尝试使用:
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$product->setVoucherCode($new_value);
$product->save();