如何在Magento中更新订单商品的自定义选项? [英] Howto update order item's custom option in Magento?

查看:64
本文介绍了如何在Magento中更新订单商品的自定义选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从订单中更新产品自定义选项值? 我知道可以在购物车中放入商品(结帐前),但是我不确定是否可以在订单中购买.

Is it possible to update a product custom option value from an order? I know it is possible when items are in cart (before checkout), but I'm not sure if it is possible in a order.

我们的应用正在出售一项服务,并且有一种情况,即仅在结帐后才能获得所需的数据.

Our app is selling a service and we have a case when a required data is available after checkout only.

推荐答案

这取决于您自定义的目的.订单商品具有存储为序列化数组的自定义选项,您可以随时对其进行修改.

It depends on the purpose of your customization. Order Item has custom options stored as serialized array and at any time it is possible for your to modify it.

与报价项目不同,在订购项目中,它具有用于检索它们的不同名称.该方法称为getProductOptions()

Unlike quote item, in order item it has different name for retrieving them. The method is called getProductOptions()

还有另一种方法可以让您设置它们setProductOptions(array $options).

Also there is another method that allows you to set them setProductOptions(array $options).

以下是在不同测试用例中使用此方法的一些示例:

Here is some examples of this method usage in different test cases:

  1. 如果只需要存储供内部代码使用,则可以将选项添加到数组array中并重新设置:

  1. If you need to store it only for internal code usage, you can just add option into array array and set it back:

$existentOptions = $orderItem->getProductOptions();
$existentOptions['your_custom_option'] = $yourCustomValue;
$orderItem->setProductOptions($existentOptions);

  • 如果需要在打印文档中显示自定义选项,则需要将自定义选项添加到选项的特殊选项中,这些选项的结构用于在前端,pdf文档,项目列表中显示其值

  • If you need to show your custom option in the printed document, you need to add your custom option into special option of options, that have structure for showing up its value on the frontend, pdf documents, items list

    $existentOptions = $orderItem->getProductOptions();
    if (!isset($existentOptions['additional_options'])) {
        // If special options of options array is set before, create it.
        $existentOptions['additional_options'] = array(); 
    }
    // Adding visible options value
    $existentOptions['additional_options'][] = array(
        'label' => 'Your Option Label',
        'value' => 'Your Option Value',
        // The last one that is optional (if not set, value is used)
        'print_value' => 'Your Option Value shown in printed documents'
    );
    $orderItem->setProductOptions($existentOptions);
    

  • 如果您需要客户可见的一个选项和代码所需的另一个选项,则甚至可以将这两种方法组合使用.

    Both of these methods even can be combined, if you need one option that is visible to customer and another option that is required for your code.

    在完成修改后,也不要忘记保存您的订单/订单商品.

    Also don't forget to save your order/order item after you deed your modifications.

    建议

    如果保存订单并且尚未修改订单模型本身,则至少需要更改其中的一些数据,以强制订单模型保存所有子实体.为了使之成为可能,您甚至可以设置一些不存在的属性.

    If you save order and haven't modified the order model itself, you need at least change some data in it, to force order model save all sub entities. For making this possible, you even can just set some non existent attribute.

    在保存操作将不被调用的情况下:

    Case when save operation will not be invoked:

    $order->load($id);
    $orderItem->getItemById($itemId);
    $orderItem->setSomething(111);
    $order->save(); // Order Item will not be saved!!
    

    在调用保存操作时的情况:

    Case when save operation will be invoked:

    $order->load($id);
    $orderItem->getItemById($itemId);
    $orderItem->setSomething(111);
    $order->setSomeNonExistentProperty(true); 
    $order->save(); // Now it will be saved
    

    享受Magento开发的乐趣

    这篇关于如何在Magento中更新订单商品的自定义选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆