Magento provides the ability to add parameters that are not product attributes or custom product parameters. They are set on product elements and quotes with the option code additional_options .
Two steps must be completed, each of which can be processed through an event observer. If you want additional options to perform reordering, you will also need to watch the third event.
Add options to item
The first step is to add an event observer to set additional parameters for the downloaded product before adding it to the basket. One option is to use the catalog_product_load_after event.
<catalog_product_load_after> <observers> <extra_options> <type>model</type> <class>extra_options/observer</class> <method>catalogProductLoadAfter</method> </extra_options> </observers> </catalog_product_load_after>
In case the observer can add additional checks, the requested page is really an addition to the action in the basket. The main theme of this observer method is to add your special options selection to the additional_options option in the product model.
public function catalogProductLoadAfter(Varien_Event_Observer $observer) { // set the additional options on the product $action = Mage::app()->getFrontController()->getAction(); if ($action->getFullActionName() == 'checkout_cart_add') { // assuming you are posting your custom form values in an array called extra_options... if ($options = $action->getRequest()->getParam('extra_options')) { $product = $observer->getProduct(); // add to the additional options array $additionalOptions = array(); if ($additionalOption = $product->getCustomOption('additional_options')) { $additionalOptions = (array) unserialize($additionalOption->getValue()); } foreach ($options as $key => $value) { $additionalOptions[] = array( 'label' => $key, 'value' => $value, ); } // add the additional options array with the option code additional_options $observer->getProduct() ->addCustomOption('additional_options', serialize($additionalOptions)); } } }
Additional parameters will be automatically moved from the product to the quotation element. With the help of this observer, your options will appear in the basket and view the check.
Add order options
To keep them, another observer is needed (only with Magento 1.5).
<sales_convert_quote_item_to_order_item> <observers> <extra_options> <type>model</type> <class>extra_options/observer</class> <method>salesConvertQuoteItemToOrderItem</method> </extra_options> </observers> </sales_convert_quote_item_to_order_item>
Here we move the option from the quote position to the order element.
public function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer) { $quoteItem = $observer->getItem(); if ($additionalOptions = $quoteItem->getOptionByCode('additional_options')) { $orderItem = $observer->getOrderItem(); $options = $orderItem->getProductOptions(); $options['additional_options'] = unserialize($additionalOptions->getValue()); $orderItem->setProductOptions($options); } }
From this moment, additional parameters will be displayed in the history of customer orders in the interface and email of the order, as well as in the presentation of the administrator interface order, invoices, deliveries, loans and PDF files.
Add reordering support
To shift fingerprints to a new order during reordering, you need to take care to copy them. Here is one possibility with the checkout_cart_product_add_after event.
<checkout_cart_product_add_after> <observers> <extra_options> <type>singleton</type> <class>extra_options/observer</class> <method>checkoutCartProductAddAfter</method> </extra_options> </observers> </checkout_cart_product_add_after>
The analysis of additional parameters and the construction of an array of additional options should be transferred to a separate function to avoid code duplication, but for this example I will leave the logic for each method in place for clarity.
public function checkoutCartProductAddAfter(Varien_Event_Observer $observer) { $action = Mage::app()->getFrontController()->getAction(); if ($action->getFullActionName() == 'sales_order_reorder') { $item = $observer->getQuoteItem(); $buyInfo = $item->getBuyRequest(); if ($options = $buyInfo->getExtraOptions()) { $additionalOptions = array(); if ($additionalOption = $item->getOptionByCode('additional_options')) { $additionalOptions = (array) unserialize($additionalOption->getValue()); } foreach ($options as $key => $value) { $additionalOptions[] = array( 'label' => $key, 'value' => $value, ); } $item->addOption(array( 'code' => 'additional_options', 'value' => serialize($additionalOptions) )); } } }
Transfer:
There is no mechanism for translating these labels or values. Here are a few ideas that may be helpful in this regard.
In the event observer quote_item_load_after, get an additional array of parameters and set $option['print_value'] = $helper->__($option['value']); . If print_value set, Magento will use this to display the display.
You can do the same with order items.
There is no such thing as print_label , but you can set your own index ( label_source possible) and set the label on the fly, using this as a source, for example. $option['label'] = $helper->__($option['label_source']); .
In addition to this, you may have to resort to modifying the templates (grep for getItemOptions() ) or overriding block classes (grep additional_options ).