Similar to my solution to add additional options in Magento 2, I have come up with the code for Magento 1 in this post.
The Magento 1 store, owing to the limitations of the platform, may require customization to offer excellent features. Hence, the method to add additional options in Magento!
We know that making changes in the core functionalities is not a good practice. Therefore follow the below method to custom develop features that allow collecting customer details, implement out-of-box functionalities and improve the shopping experience!
You can dynamically add options based on specific products without adding them from the backend using this method! You can also dynamically add customer account navigation in Magento 2 it allows you to customize and enhance the customer’s account dashboard by adding custom links or sections based on specific business needs and requirements.
Steps to Add Additional Options in Magento:
1. Create config.xml file at app/code/local/Vendor/Extension/etc/ folder
<?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Vendor_Extension> <version>1.0.0</version> </Vendor_Extension> </modules> <global> <models> <extension> <class>Vendor_Extension_Model</class> </extension> </models> <events> <checkout_cart_product_add_after> <observers> <Vendor_Extension_Model_Observer> <type>singleton</type> <class>Vendor_Extension_Model_Observer</class> <method>checkout_cart_product_add_after</method> </Vendor_Extension_Model_Observer> </observers> </checkout_cart_product_add_after> <sales_model_service_quote_submit_before> <observers> <Vendor_Extension_Model_Observer_Set_Order> <type>singleton</type> <class>Vendor_Extension_Model_Observer</class> <method>setAdditionalDataToOrder</method> </Vendor_Extension_Model_Observer_Set_Order> </observers> </sales_model_service_quote_submit_before> </events> </global> </config>
2. Create Observer.php file at app/code/local/Vendor/Extension/Model/ folder
<?php class Vendor_Extension_Model_Observer { public function checkout_cart_product_add_after($observer) { $event = $observer->getEvent(); $quoteItem = $event->getQuoteItem(); $messageOption = array( 'label' => 'Message', 'option_value' => 'Custom Value', 'value' => 'Custom Value', 'print_value' => 'Custom Value', ); $value = array('message' => $messageOption); $value = serialize($value); $quoteItem->addOption(array('code' => 'additional_options', 'product_id' => $quoteItem->getProductId(), 'value' => $value)); } public function setAdditionalDataToOrder($observer) { try { $quote = $observer->getEvent()->getQuote(); $order = $observer->getEvent()->getOrder(); $quoteItems = []; foreach ($quote->getAllVisibleItems() as $quoteItem) { $quoteItems[$quoteItem->getId()] = $quoteItem; } foreach ($order->getAllVisibleItems() as $orderItem) { $quoteItemId = $orderItem->getQuoteItemId(); $quoteItem = $quoteItems[$quoteItemId]; $additionalOptions = $quoteItem->getOptionByCode('additional_options'); if (count($additionalOptions) > 0) { $options = $orderItem->getProductOptions(); $options['additional_options'] = unserialize($additionalOptions->getValue()); $orderItem->setProductOptions($options); } } } catch (\Exception $e) { Mage::log($e->getMessage()); } } }
That’s it.
I’d be very grateful if you helped share this helpful post on social media to fellow developers!
Thanks!