[英]How do I edit a product attribute using PHP during a cart checkout in Magento?
I have a two part question about customizing my Magento store. 我有两个关于自定义Magento商店的问题。
When someone buys a downloadable product, I want to generate a licence code and include it in the invoice. 当有人购买可下载产品时,我想生成许可证代码并将其包含在发票中。
I have added a product attribute called 'license_code' to my product's default attribute set and I want to set its value with php when a customer checks out. 我在产品的默认属性集中添加了一个名为“license_code”的产品属性,我希望在客户结账时使用php设置其值。
What is the event to observe that will let me access the products in the cart just after they are purchased but before the invoice is created? 请注意哪些事件可以让我在购买之后但在创建发票之前访问购物车中的产品?
I also need to know what script to use to set a product's attribute value during that event. 我还需要知道在该事件期间使用什么脚本来设置产品的属性值。
Thank you for your help! 谢谢您的帮助!
Possible events are sales_order_place_before
or sales_convert_quote_*
. 可能的事件是
sales_order_place_before
或sales_convert_quote_*
。
You cannot save your 'license_code' attribute because that will affect all products, a product does not store it's values when ordered. 您无法保存“license_code”属性,因为这会影响所有产品,产品在订购时不会存储它的值。 Instead a better idea would be to manipulate the options of an order item.
相反,更好的想法是操纵订单商品的选项。
function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer)
{
$orderItem = $observer->getOrderItem();
$options = $orderItem->getProductOptions();
$options['licence_code'] = YOUR-DOWNLOADABLE-CODE-HERE;
$orderItem->setProductOptions($options);
}
Retrieving the code later is essentially the same process with getProductOptions()
, the order item objects are already used on the order view pages so are easy to find and use in your theme. 稍后检索代码与
getProductOptions()
基本上是相同的过程,订单项对象已经在订单视图页面上使用,因此很容易在主题中查找和使用。
Ok I think I got it figured out. 好吧,我想我弄清楚了。
I set up my event observers as follows: 我按如下方式设置了我的事件观察者:
<events>
<sales_order_item_save_before>
<observers>
<downloadable_observer>
<class>Licensing_Catalog_Model_Observer</class>
<method>generate_licenses</method>
</downloadable_observer>
</observers>
</sales_order_item_save_before>
</events>
and then my observing function as: 然后我的观察功能如下:
public function generate_licenses($observer)
{
$orderItem = $observer->getEvent()->getItem();
$options = $orderItem->getProductOptions();
$options['licence_code'] = 'YOUR-DOWNLOADABLE-CODE-HERE';
$orderItem->setProductOptions($options);
return $this;
}
Thank you so much for the help, clockworkgeek! 非常感谢你的帮助,clockworkgeek!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.