How to customize order item in Magento?

Can I update the value of a custom product option from an order? I know that this is possible when the goods are in the basket (before checking), but I am not sure that this is possible in the order.

Our application sells the service, and we have a case when the required data is available only after placing the order.

+6
source share
2 answers

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(); // Order Item will not be saved!! 

Case when calling the save operation:

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

Good luck developing Magento

+17
source

You can do this on the addProduct function before ordering.

  try { $cart = Mage::getModel('checkout/cart'); $previousItemCount = $cart->getQuote()->getItemsCount(); if ($previousItemCount <= 0) { $cart->init(); } $params = $this->getRequest()->getParams(); $product = Mage::getModel('catalog/product')->load($params['product_id']); $date = explode('/', $params['product_dtinvoice']); $date = array( 'month' => $date[0], 'day' => $date[1], 'year' => $date[2], ); $cart->addProduct( $product, new Varien_Object(array( 'product' => $product->getId(), 'qty' => 1, 'options' => array( '4' => array( 'month' => $date['month'], 'day' => $date['day'], 'year' => $date['year'] ), '2' => $params['product_ean'], '3' => $params['product_serialnumber'], '1' => $params['product_seller'], ), )) ); $cart->save(); if ($previousItemCount < $cart->getQuote()->getItemsCount()) { $return = array('result' => true, 'msg' => ''); } else { $return = array('result' => false, 'msg' => 'Did not possible to add this product to cart. Please contact the administrator'); } $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($return)); } catch(Exception $e) { Mage::throwException($e->getMessage()); } 
0
source

Source: https://habr.com/ru/post/921485/


All Articles