I am trying to modify the subtotal in the checkout/cart page when the item gets updated using the Update Cart action button on the product page. After the product has been successfully added to the cart for the very first time, I tried to intercept the below events when I click on the Edit button from the checkout/cart page then I click on Update Cart button from product page. I really want to learn this concept it would be really helpful for me if someone can guide me. Though I will try my best to solve and update here.
checkout_cart_update_items_before
checkout_cart_update_items_after
checkout_cart_product_update_after
checkout_cart_update_items_before
and checkout_cart_update_items_after
does not get called at all not sure why no errors or warnings. Well, I was able to intercept checkout_cart_product_update_after where I added my custom code and results in me the highlighted part in the image. Now I am trying to update the subtotal by adding that digitization fee to it. So the subtotal should show around $30 on the checkout/cart page after the subtotal has been updated. Also, this is a configurable product that I am working with
\app\code\Noname\Checkout\etc\events.xml
<event name="checkout_cart_product_update_after">
<observer name="noname_cart_product_update_after" instance="Noname\Checkout\Observer\CartUpdateProductAfter" />
</event>
\app\code\Noname\Checkout\Observer\CartUpdateProductAfter.php
<?php
namespace Noname\Checkout\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
class CartUpdateProductAfter implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Noname\Core\Helper\CustomerHelper$customerSession */
$customerSession = $objectManager->create("\Noname\Core\Helper\CustomerHelper");
//THIS BELOW CODE ADD A CUSTOM EXTRA OPTION TO THE ITEM
$item = $observer->getQuoteItem();
$additionalOptions = array();
if ($additionalOption = $item->getOptionByCode('additional_options')){
$additionalOptions = (array) unserialize($additionalOption->getValue());
}
$infoBuyRequest = $item->getBuyRequest()->getData();
$digitizationfee = $infoBuyRequest['digitizationfee'];
$additionalOptions[] = [
'label' => 'Digitization Fee',
'value' => $digitizationfee
];
if(count($additionalOptions) > 0) {
$item->addOption(array(
'product_id' => $item->getProductId(),
'code' => 'additional_options',
'value' => serialize($additionalOptions)
));
$customerSession->getSessionManager()->setData("last_updated_item",$item->getQuoteId());
}
}
}
?>
$item = $observer->getQuoteItem(); $quote=$item->getQuote(); foreach($quote->getAllVisibleItems() as $i){ var_dump($i->getId()); }
but it is giving NULL for itemid