I am using Magento 2.

I made an observer that observes anytime a new item is added to the cart. The observer works properly, I also managed to create a single product discount with this observer, here is the events.xml file:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
    <observer name="PerfectStorm_Pricecalculations_Addtocart" instance="PerfectStorm\Pricecalculations\Observer\PricecalculationsAfterAddtoCart" />

The problem is, I want to do the same in order to change and intercept the calculation of the subtotals in the cart for each item. But I don't know what event should I observe.

Can you please advice?



I suggest to create a plugin for the class Magento\Quote\Model\Quote\Address\Total\Subtotal, and to add some functionality to the collect function.

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\Quote\Model\Quote\Address\Total\Subtotal">
        <plugin name="myPluginUniquId" type="Vendor\Module\Plugin\Model\Quote\Address\Total\Subtotal"/>

and the Vendor/Module/Plugin/Model/Quote/Address/Total.php file:

namespace Vendor/Module/Plugin/Model/Quote/Address/Total;

class Subtotal{

public function aroundCollect(
        \Magento\Quote\Model\Quote\Address\Total\Subtotal $subject,
        \Closure $proceed,
        \Magento\Quote\Model\Quote $quote,
        \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
        \Magento\Quote\Model\Quote\Address\Total $total
        //do something before the subtotal calculation

        $returnValue = $proceed($quote, $shippingAssignment, total);

       //do something after the subtotal calculation

        return $returnValue;


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.