How to Add Additional Options in Magento 2
Magento 2 is a feature-rich platform that helps improve the shopping experience. However, many times it lacks in fulfilling the business requirements now that various types of business have its online presence. Luckily, the developers can offer customization to implement out of box functionalities.
As a part of such customization, you may want to add additional options in Magento 2 product page, cart page, etc. For example, you want to display a text box to allow customers to add comment box in the product page or to show installments selected by the customers on the cart page.
Also, it is important that you implement the customizations without affecting the core functionalities as it is not a good practice. The below method allows to add additional options in Magento 2 considering it.
Steps to Add Additional Options in Magento 2:
- Create events.xml at app\code\Vendor\Extension\etc\
12345678910<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"><event name="checkout_cart_product_add_after"><observer name="extension_checkout_cart_product_add_after" instance="Vendor\Extension\Observer\CheckoutCartAddObserver" /></event><event name="sales_model_service_quote_submit_before"><observer name="extesnionadd" instance="Vendor\Extension\Observer\AddOptionToOrderObserver" /></event></config> - Create CheckoutCartAddObserver.php at app\code\Vendor\Extension\Observer\
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556namespace Vendor\Extension\Observer;use Magento\Framework\Event\Observer as EventObserver;use Magento\Framework\Event\ObserverInterface;use Magento\Store\Model\StoreManagerInterface;use Magento\Framework\View\LayoutInterface;use Magento\Framework\App\RequestInterface;use Magento\Framework\Serialize\SerializerInterface;class CheckoutCartAddObserver implements ObserverInterface{protected $layout;protected $storeManager;protected $request;private $serializer;public function __construct(StoreManagerInterface $storeManager,LayoutInterface $layout,RequestInterface $request,SerializerInterface $serializer){$this->layout = $layout;$this->storeManager = $storeManager;$this->request = $request;$this->serializer = $serializer;}public function execute(EventObserver $observer){$item = $observer->getQuoteItem();$post = $this->request->getPost();$additionalOptions = array();if ($additionalOption = $item->getOptionByCode('additional_options')) {$additionalOptions = $this->serializer->unserialize($additionalOption->getValue());}$additionalOptions[] = ['label' => 'Size','value' => 'XL'];if (count($additionalOptions) > 0) {$item->addOption(array('product_id' => $item->getProductId(),'code' => 'additional_options','value' => $this->serializer->serialize($additionalOptions)));}}} - Create AddOptionToOrderObserver.php at app\code\Vendor\Extension\Observer\
12345678910111213141516171819202122232425262728293031323334353637383940<?phpnamespace Vendor\Extension\Observer;use Magento\Framework\Event\ObserverInterface;use Magento\Framework\Serialize\SerializerInterface;class AddOptionToOrderObserver implements ObserverInterface{private $serializer;public function __construct(SerializerInterface $serializer){$this->serializer = $serializer;}public function execute(\Magento\Framework\Event\Observer $observer){$quote = $observer->getQuote();$order = $observer->getOrder();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'] = $this->serializer->unserialize($additionalOptions->getValue());$orderItem->setProductOptions($options);}return $this;}}}
That’s it. Adding options in Magento 2 was never this easy, isn’t it?
The below image shows how I implemented this method to show the number of installments:
For a similar solution in Magento, check add additonal options in Magento.
You may, however, ask your doubts in the Comments section below and I’d be happy to help 🙂
Thank You.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
8 Comments
How can I add the additional options when click Reorder In MY Account
Hello Viji,
Override this controller Magento\Sales\Controller\AbstractController\Reorder
or create a plugin and add the product option.
Thank You.
I don’t think it will work without registration.php and module.xml
Hello, the above solution is to use in your extension and not the entire extension.
Thanks.
Hi,
what if the new option has a price? So say that the product price is 100 and the additional option has to add 50 so the total in cart is 150, how can this be achieved?
Hello,
You need to set custom price for the required solution.
Thanks.
Hi,
what if the new option has a price? So say that the product price is 100 and the additional option has to add 50 so the total in cart is 150, how can this be achieved?
Hi Lounik,
you need to set a custom price for that.