2

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="Magento\Quote\Model\Quote">
        <plugin disabled="false" name="Vendor_Module_Plugin_Magento_Quote_Model_Quote" sortOrder="10" type="Vendor\Module\Plugin\Magento\Quote\Model\Quote"/>
    </type>
</config>

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

public function beforeCollectTotals(\Magento\Quote\Model\Quote $subject)
{
    $quote = $this->getQuote();
    foreach($quote->getAllItems() as $item){
       $price = $this->getCustomPrice($item->getProductId());
       if($price){
          $item->setCustomPrice($price);
          $item->setOriginalCustomPrice($price);
       }  
    }
    return [];
}

the problem with this plugin is, i got this error after i placed an order:

Please check the billing address information. "firstname" is required. Enter and try again. "lastname" is required. Enter and try again. "street" is required. Enter and try again. "city" is required. Enter and try again. "telephone" is required. Enter and try again. "postcode" is required. Enter and try again. "countryId" is required. Enter and try again.

i check the quote address in the database and those field column is empty

  • You should use checkout_cart_save_before or checkout_cart_save_after event observer which calls every time when cart modified. – Hiren Bhalodiya Sep 23 at 22:11
0

As far I know

1: If you need to override a single method first priority will be plugin and in plugin the first will be before plugin and then after and if both are not possible then use around

2: If plugin is not working then we can override whole class but this is the least thing if other solution are not working

while overriding whole class if possible override the only method that you want to override and if not possible with the specific method that you want then override the whole class with all methods

| improve this answer | |
0

have you used the below two events observer ?

checkout_cart_product_add_after and checkout_cart_product_update_after from this events you can from this observer you can get the $item = $observer->getQuoteItem(); and then you can use below code to add your custom price you can use the same observer file for both events

$item = ($item->getParentItem() ? $item->getParentItem() : $item);
$orignalprice = $product->getFinalPrice();
$customPrice = 10; // your price
$price = $orignalprice + $customPrice;
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);
| improve this answer | |
0

Use checkout_cart_product_add_after event.

<?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="checkout_cart_product_add_after">
        <observer name="customprice" instance="Dolphin\Hello\Observer\CustomPrice" />
    </event>
</config>

have to create CustomPrice.php file that override your price in the Observer Folder. <?php

        namespace Dolphin\Hello\Observer;
     
        use Magento\Framework\Event\ObserverInterface;
        use Magento\Framework\App\RequestInterface;
     
        class CustomPrice implements ObserverInterface
        {
            public function execute(\Magento\Framework\Event\Observer $observer) {
                $item = $observer->getEvent()->getData('quote_item');         
                $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
                $price = 100; //set your price here
                $item->setCustomPrice($price);
                $item->setOriginalCustomPrice($price);
                $item->getProduct()->setIsSuperMode(true);
            }
     
        }
| improve this answer | |

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.