---
title: "How to add additional options in magento 2 cart"
url: "https://dolphinwebsolution.com/how-to-add-additional-options-in-cart"
date: "2021-10-25T13:58:38+05:30"
modified: "2023-08-25T09:14:16+05:30"
author:
  name: "Jigar Patel"
categories:
  - "Magento"
word_count: 685
reading_time: "4 min read"
summary: "Magento 2 is an e-commerce platform that improves the shopping cart. Magento supports lots of customization and development. Recently, one of our clients was willing to add additional options from ..."
description: "Recently, one of our clients was willing to add additional options from the customers so, he can easily deliver customized products fit to their requirements..."
keywords: "add additional options,add additional options magento 2,set additional options,add additional options magento Cart,set additional options in magento 2 cart, Magento"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Remove Specific Category From Category Menu programmatically in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-remove-specific-category-from-category-menu-programmatically-in-magento-2"
  - title: "How To Add Or Create Slide-out(Sliding) ,Modal Windows Panels in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-add-or-create-slide-outsliding-modal-windows-panels-in-magento-2"
  - title: "How to Use Grunt in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-use-grunt-in-magento-2"
---

# How to add additional options in magento 2 cart

_Published: October 25, 2021_  
_Author: Jigar Patel_  

![](https://dolphinwebsolution.com/wp-content/uploads/2021/10/How-to-add-additional-options-in-magento-2-cart-1024x536.png)

[Magento 2 is an e-commerce platform](https://dolphinwebsolution.com/magento-2-development) that improves the shopping cart. Magento supports lots of customization and development. Recently, one of our clients was willing to add additional options from the customers so, he can easily deliver customized products fit to their requirements without changing core Magento functionalities. After spending some time coding, finally, we have successfully delivered a smile to a client’s face. But today I want to share that code with you guys.

We are already learned [how to create a basic module in Magento 2](https://dolphinwebsolution.com/how-to-create-module-in-magento-2). We need to create **module.xml and** **registration.php** files. You can create **module.xml and** **registration.php** from this tutorial.

Here we are using **Dolphin** as Vendor name and **Additional** as the name of the module. You can change this according to your Vendor and Module name.

#### Step 1: Create Events XML File
The first thing you need to do is to create an **events.xml** file in the folder **Dolphin/Additional/etc/** and use the first event **checkout\_cart\_product\_add\_after** which will be called after adding a product to the cart and second event **sales\_model\_service\_quote\_submit\_before** which will be called quote submit before.

```
<?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="dolphin_checkout_cart_product_add_after"
                  instance="Dolphin\Additional\Observer\CheckoutCartAdd"/>
    </event>
    <event name="sales_model_service_quote_submit_before">
        <observer name="dolphin_add" instance="Dolphin\Additional\Observer\AddOptionToOrder"/>
    </event>
</config>
```

Here you declare the first observer named **CheckoutCartAdd** that will handle the event **checkout\_cart\_product\_add\_after** which Magento dispatches every time a product’s added to the cart and declare the second observer named **AddOptionToOrder** that will handle the event **sales\_model\_service\_quote\_submit\_before** which Magento dispatches every time a quote submit before.

Now we need to create an Observer as defined in the events.xml file

Read This: [Magento 2 add ifconfig in override block XML](https://dolphinwebsolution.com/magento-2-add-ifconfig-in-override-block-xml)

#### Step 2: Create CheckoutCartAdd.php File
Create observer class file **CheckoutCartAdd.php** in **app/code/Dolphin/Additional/Observer** folder with the following code.

```
<?php

namespace Dolphin\Additional\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 CheckoutCartAdd 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' => 'Option Label',
            'value' => 'Option Value'
        ];

        if (count($additionalOptions) > 0) {
            $item->addOption(array(
                'product_id' => $item->getProductId(),
                'code' => 'additional_options',
                'value' => $this->serializer->serialize($additionalOptions)
            ));
        }
    }
}
```

#### Step 3: Create AddOptionToOrder.php File
Create observer class file [**AddOptionToOrder.php**](https://magento.stackexchange.com/questions/181480/how-to-add-additional-options-in-magento-2-cart/254847) in **app/code/Dolphin/Additional/Observer** folder with the following code.

```
<?php

namespace Dolphin\Additional\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Serialize\SerializerInterface;

class AddOptionToOrder implements ObserverInterface
{
    private $serializer;

    public function __construct(
        SerializerInterface $serializer
    ){
        $this->serializer = $serializer;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if ($this->helperData->isModuleEnabled()) {
            $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;
        }
    }
}
```

**Result :**

![AI Integration in Magento: Benefits, Use Cases and Business Impact](https://dolphinwebsolution.com/wp-content/uploads/2021/10/additional-img-1-1-e1634894482857.png)

I Hope, This instruction will be helpful for you.

If you have any difficulties regarding this blog, do consider them posting in the Comments section below!

I’m here to help.

**Thank you!**


---

_View the original post at: [https://dolphinwebsolution.com/how-to-add-additional-options-in-cart](https://dolphinwebsolution.com/how-to-add-additional-options-in-cart)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 06:54:03 UTC_  
