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

查看:20
本文介绍了如何在 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. 如果你只需要存储它供内部代码使用,你可以将选项添加到数组数组中并重新设置:

  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!!
    

    调用保存操作的情况:

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

    享受 Magento 开发的乐趣

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

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