简体   繁体   English

如何在Magento的购物车结帐时使用PHP编辑产品属性?

[英]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_beforesales_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.

相关问题 Magento产品属性未在结帐中显示,在购物车中显示 - Magento product attribute not showing in checkout, shows in cart 如何在Magento事件checkout_cart_product_add_after中获取客户详细信息? - How can I get customer details in Magento event checkout_cart_product_add_after? magento 1.8使用php将产品添加到购物车 - magento 1.8 add product to cart using php 添加到购物车并侦听“ checkout_cart_product_add_after”事件时,如何获取产品数据 - how do I get product data when adding to cart and listening for the “checkout_cart_product_add_after” event 如何从magento检索自定义属性产品信息? - How do I retrieve custom attribute product info from magento? Magento - 如何运行此自定义产品属性脚本 - Magento - How do I run this custom product attribute script 如何根据产品属性将magento添加到购物车按钮 - How to change magento add to cart button based on product attribute 在 Woocommerce 购物车和结帐中显示产品缩略图和属性 - Displaying product thumbnail and attribute in Woocommerce cart and checkout 使用“ Mage_Checkout_Model_Cart_Product_Api”更新magento购物车吗? - Update magento cart with “Mage_Checkout_Model_Cart_Product_Api”? WooCommerce - 在购物车、结帐和“浮动购物车”插件上编辑产品名称 - WooCommerce - Edit Product Name on Cart, Checkout and “Floating Cart” Plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM