Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5499

How To Set Additional Options In Cart Item – Magento2

$
0
0

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.

<?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="catalog_product_load_after">
        <observer name="set_additional_options" instance="Webkul\Demo\Observer\SetAdditionalOptions"/>
    </event>
</config>

After this write following code in your observer file.

<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Demo
 * @author    Webkul
 * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Webkul\Demo\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class SetAdditionalOptions implements ObserverInterface
{
    /**
     * @var RequestInterface
     */
    protected $_request;
    
    /**
     * @param RequestInterface $request
     */
    public function __construct(
        RequestInterface $request
    ) {
        $this->_request = $request;
    }

    /**
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $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",
            );
            $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
        }
    }
}

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.

  • Cart Page
    Additional Options
  • Mini Cart
    Additional Options
  • Checkout Page
    Additional Options

If you have any query or issue, comment below.


Viewing all articles
Browse latest Browse all 5499

Trending Articles