I want to add additional options to the next product on the
sales_quote_remove_item
event while removing the first product from a cart.
First Product, I have already added the first product to cart with additional options
Second Product with no additional options(Normal Add to cart).
While removing First Product with additional options, I want to check if First product has additional options or not
if Yes, First Product will be removed from the cart, I want to add additional options to the Second Product in the cart as shown in the below screenshot.
app\code\Vendor\Module\etc\events.xml
<?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="sales_quote_remove_item">
<observer name="remove_item" instance="Vendor\Module\Observer\AddFeeToRemoveItemObserver" />
</event>
</config>
app\code\Vendor\Module\Observer\AddFeeToRemoveItemObserver.php
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Checkout\Model\Session;
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
class AddFeeToRemoveItemObserver implements ObserverInterface
{
/**
* @var RequestInterface
*/
protected $_request;
/**
* Json Serializer
*
* @var JsonSerializer
*/
protected $jsonSerializer;
/**
* Set payment fee to order
*
* @param EventObserver $observer
* @return $this
*/
public function __construct(
JsonSerializer $jsonSerializer,
RequestInterface $request,
Session $checkoutSession,
\Magento\Quote\Model\Quote\ItemFactory $quoteItemFactory,
\Magento\Quote\Model\ResourceModel\Quote\Item $itemResourceModel,
\Magento\Quote\Model\QuoteFactory $quoteFactory,
\Magento\Quote\Api\CartRepositoryInterface $itemRepository
) {
$this->_request = $request;
$this->jsonSerializer = $jsonSerializer;
$this->_checkoutSession = $checkoutSession;
$this->quoteItemFactory = $quoteItemFactory;
$this->itemResourceModel = $itemResourceModel;
$this->quoteFactory = $quoteFactory;
$this->itemRepository = $itemRepository;
}
/**
* @param \Magento\Framework\Event\Observer $observer
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info("==============>");
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');
$quoteItem = $observer->getQuoteItem();
$quote = $quoteItem->getQuote();
$product = $quoteItem->getProduct();
$additionalOptions = [];
if ($this->_request->getFullActionName() == 'checkout_cart_delete') {
if (count($quote->getAllVisibleItems())) {
foreach ($quote->getAllVisibleItems() as $loop_item) {
$helper = $objectManager->create('\Magento\Catalog\Helper\Product\Configuration');
$options=$helper->getCustomOptions($loop_item);
$quoteItemFeelabel = "";
foreach ($options as $option) {
$quoteItemFeelabel = $option['label'];
}
$price = 10;
$additionalOptions[] = array(
'label' => __("My Custom Label"),
'value' => $priceHelper->currency($price, true, false),
);
$loop_item->setProductOption(
'additional_options',
$this->jsonSerializer->serialize($additionalOptions)
);
}
}
}
}
}
I've tried the above code but It's not working.
If anyone has a solution, please let me know.