Read More
Read More
Menu Close

    How To Set Additional Options In Cart Item – Magento2

    In this blog we will see how to add additional options in cart item.
    In some situations we want to display specific details with some products in cart.
    We can either create a custom option on product or we can override template file and display desired information.
    But there are some issues in both situations.

    Custom option on product

    If you want to display additional information using custom option then you have to create custom option on product and custom option will be visible everywhere on product. So you have to hide options from product and it will be additional work to do.

    Override template

    If you want to display information by overriding template then you have to override so many template files for this.
    Some of the templates files are

    • Cart page template files
    • Checkout page template files
    • Minicart template files

    But luckily there is another way in magento by which you can achieve this without overriding templates and creating custom option.
    You can achieve this using observer.

    Additional options using Observer

    First of all define event in events.xml file.

    After this write following code in your observer file.

    You can set the multiple options using the same approach.
    Using this approach you can set additional options in cart item and you will be able to view this information on all pages.

    Searching for an experienced
    Magento 2 Company ?
    Read More
    • Cart Page
      Additional Options
    • Mini Cart
      Additional Options
    • Checkout Page
      Additional Options

    If you want to display this additional options in order item.
    Check the blog –
    How To Set Additional Options In Order Item – Magento2

    If you have any query or issue, comment below.

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    8 comments

  • salik shastikar
    Hi,

    I am new in magento 2 can anyone help me to how and where and which folders need to add those files

    Thanks

  • vipul
    Hi,

    I have added additional options as per given example and its working fine for me, its visible on cart and checkout summary, but its not visible in admin order view and my account of customer, how can i show these additional information over there?

    Thanks

  • Patryk
    Fixed solution (right way in M2.2):
    Clear COOCIE because of session -_-

    namespace WebkulDemoObserver;

    use MagentoFrameworkEventObserverInterface;
    use MagentoFrameworkAppRequestInterface;
    use MagentoFrameworkSerializeSerializerJson;
    use MagentoFrameworkAppObjectManager;

    class SetAdditionalOptions implements ObserverInterface
    {
    /**
    * @var RequestInterface
    */
    protected $_request;

    private $_serializer;

    /**
    * @param RequestInterface $request
    */
    public function __construct(
    RequestInterface $request,
    Json $serializer
    ) {
    $this->_request = $request;
    $this->_serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
    }

    /**
    * @param MagentoFrameworkEventObserver $observer
    */
    public function execute(MagentoFrameworkEventObserver $observer)
    {
    // Check and set information according to your need
    if ($this->_request->getFullActionName() == ‘checkout_cart_add’) { //checking when product is adding to cart
    $product = $observer->getProduct();

    $additionalOptions = [];
    $additionalOptions[] = array(
    ‘label’ => “Some Label”,
    ‘value’ => “Your Information”,
    );

    $product->addCustomOption(‘additional_options’, $this->_serializer->serialize($additionalOptions));
    }
    }
    }

  • subhranil92
    Parse error: syntax error, unexpected ‘use’ (T_USE) in C:xampphtdocsmagento216appcodeWebkulDemoObserverSetAdditionalOptions.php on line 13
  • san
    how to do the same for grouped products
  • Trilok
    This Code is not working with Configurable products
  • Neeraj
    What we need to do If we need to show same in order email or in backend for same order ?
  • Back to Top