It depends on the purpose of your setup. The Order element has custom parameters stored as a serialized array, and you can change it at any time.
Unlike the quote item, in the order element it has a different name for extraction. This method is called getProductOptions()
There is also another method that allows you to set them to setProductOptions(array $options)
.
Here are some examples of using this method in different test cases:
If you need to save it only for internal use of the code, you can simply add the parameter to the array of arrays and set it back:
$existentOptions = $orderItem->getProductOptions(); $existentOptions['your_custom_option'] = $yourCustomValue; $orderItem->setProductOptions($existentOptions);
If you need to show your custom option in a printed document, you need to add your custom parameter to a special option option that has a structure to display its value in the interface, pdf documents, list of elements
$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 can even be combined if you need one parameter that is visible to the client, and another parameter that is necessary for your code.
Also do not forget to save your order / order item after you make changes.
Tip
If you saved the order and did not change the order model, you need to at least change some data in it in order to force the order model to save all sub-entities. To make this possible, you can even simply set some non-existent attributes.
The case when the save operation will not be called:
$order->load($id); $orderItem->getItemById($itemId); $orderItem->setSomething(111); $order->save();
Case when calling the save operation:
$order->load($id); $orderItem->getItemById($itemId); $orderItem->setSomething(111); $order->setSomeNonExistentProperty(true); $order->save();
Good luck developing Magento
source share