#StackBounty: #magento2 #cart #event-observer #quote #plugin Magento 2: What is the best way to observe cart change to modify cart item…

Bounty: 50

I’m using Magento v2.3.4 and i want to modify the cart item, if there is an update on the cart (add to cart, update cart item, and remove cart item). Currently I’m using plugin for quote collectTotals() function.

Is this the correct way to do it, or is there any other method that i can use ?

etc/di.xml

<?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="MagentoQuoteModelQuote">
        <plugin disabled="false" name="Vendor_Module_Plugin_Magento_Quote_Model_Quote" sortOrder="10" type="VendorModulePluginMagentoQuoteModelQuote"/>
    </type>
</config>

Vendor/Module/Plugin/Magento/Quote/Model/Quote.php

public function beforeCollectTotals(MagentoQuoteModelQuote $subject)
{
    $quote = $this->getQuote();
    foreach($quote->getAllItems() as $item){
       $price = $this->getCustomPrice($item->getProductId());
       $item->setCustomPrice($price)->save();
    }
    return [];
}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.