0

I want to know the event after total calculation occurs in cart using plugin in Magento 2.

Here is my app/code/NAMESPACE/MODULE/etc/di.xml file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Model\Cart">
        <plugin name="cache_price_cart" type="NAMESPACE\MODULE\Plugin\Product" sortOrder="10"/>
    </type>
</config>

app/code/NAMESPACE/MODULE/Plugin/Product.php

<?php
namespace NAMESPACE\MODULE\Plugin;
use Magento\Framework\Exception\LocalizedException;

class Product
{
    protected $cart;

    public function __construct(
        \Magento\Checkout\Model\Cart $cart)
    {
        $this->_cart = $cart;
    }

    public function afterAddProduct($subject, $productInfo, $requestInfo = null)
    {
        $productInfo = $this->_cart->getQuote()->getItemsCollection();
        print_r($productInfo);die;
    }
}

it gives me cart item information after adding product to cart but I want to get cart item information whenever item price is calculated or whenever cart item is calculated. I am doing this because I have to add custom price (From third party) whenever cart item is calculated.

I don't want to add my custom price (From third party) on afterAddProduct event but want whenever cart item is calculated. Is there any plugin event that provides that works on after cart item is calculated?

0

So this is just an idea..not sure though but it may help you to get started

You need to add 2 logs in the dispatch method and in your plugin. The idea is there will be lot lot of events in your log file. You wont understand which gets fired after your plugin. Now if you add a log in your plugin too then you will know which gets fired exactly after your plugin log.Hence the same log file needed like xyz.log. Same file for both logs.

//Magento\Framework\Event/Manager - This is the file path

public function dispatch($eventName, array $data = [])
    {
        $eventName = mb_strtolower($eventName);
        \Magento\Framework\Profiler::start('EVENT:' . $eventName, ['group' => 'EVENT', 'name' => $eventName]);
        foreach ($this->_eventConfig->getObservers($eventName) as $observerConfig) {
            $event = new \Magento\Framework\Event($data);
            // log the event name here in a file xyz.log
            $event->setName($eventName);
            $wrapper = new Observer();
            $wrapper->setData(array_merge(['event' => $event], $data));

            \Magento\Framework\Profiler::start('OBSERVER:' . $observerConfig['name']);
            $this->_invoker->dispatch($observerConfig, $wrapper);
            \Magento\Framework\Profiler::stop('OBSERVER:' . $observerConfig['name']);
        }
        \Magento\Framework\Profiler::stop('EVENT:' . $eventName);
    }

So in the above code you can add a log to print any event.

Along with that

public function afterAddProduct($subject, $productInfo, $requestInfo = null)
    {
        // log something here too in the same file xyz.log
        $productInfo = $this->_cart->getQuote()->getItemsCollection();
        print_r($productInfo);die;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.