1

I want to apply a custom price for the current order. I read that I should use sales_quote_add_item, But its looks like I am doing something wrong. I found some tutorials for magento1 but not for Magento 2. So please let me know what I am doing wrong.

app/code/Myvendor/HelloMagento/etc/adminhtml/events.xml

    <?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="sales_quote_add_item">
        <observer name="change_hello_magento_display" instance="Myvendor\Addtocart\Observer\Change" />
    </event>
 </config>

app/code/Myvendor/HelloMagento/Observer/Change.php

<?php
namespace Myvendor\Addtocart\Observer;

class Change implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $event = $observer->getEvent();
        $quote_item = $event->getQuoteItem();
        $new_price = 987;
        $quote_item->setOriginalCustomPrice($new_price);
        $quote_item->save();
    }
}

Let me know please what I am doing wrong. I want to achieve the same thing for Magento 2 given on below link for magento1.

https://stackoverflow.com/questions/9721583/changing-the-price-in-quote-while-adding-product-to-cart-magento

3
  • hi @rahul whether u would like to add in frontend? Commented Jul 4, 2017 at 4:07
  • whether you would like to change the price after add to cart please confirm thanks Commented Jul 4, 2017 at 4:09
  • hi @rahul i have used checkout_cart_product_add_after event please try and get back with your comments thanks Commented Jul 4, 2017 at 4:30

2 Answers 2

6

Please follow the below steps:

Step-1: Create an events.xml in the following below path

Note: Here i am considering the module called Ewall_Pricecalculations

app/code/Ewall/Pricecalculations/etc/frontend/events.xml

<?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="Ewall_Pricecalculations_Addtocart" instance="Ewall\Pricecalculations\Observer\PricecalculationsAfterAddtoCart" />
</event>
</config>

Step-2: Next Create a file in the name of PricecalculationsAfterAddtoCart.php keep the below code as shown below.

<?php
namespace Ewall\Pricecalculations\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
 
class PricecalculationsAfterAddtoCart implements ObserverInterface
{
     
public function execute(\Magento\Framework\Event\Observer $observer) 
{
            $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/stackexchange.log');
            $logger = new \Zend\Log\Logger();
            $logger->addWriter($writer);
            /* Code here */
            $quote_item = $observer->getEvent()->getQuoteItem();
            $price = 400; //set your price here
            $quote_item->setCustomPrice($price);
            $quote_item->setOriginalCustomPrice($price);
            $quote_item->getProduct()->setIsSuperMode(true);

            $logger->info("success !!!!");
            
    }
}

Step 3: please use php bin/magento setup:di:compile

clear cache and page_cache and check the same in the frontend.

6
  • 1
    checkout_cart_product_add_after this event is not working in case of admin order. Is there any other event to handle this scenario? Commented Jun 19, 2018 at 9:01
  • @RanjitShinde the above event is working at front end after add to cart. Commented Jun 19, 2018 at 11:02
  • @NagarajuKasa, can we set custom taxamount for the quote item using this event? Commented May 15, 2019 at 6:05
  • i am note sure @jafarpinjar i will check and getback to you. Commented May 15, 2019 at 6:21
  • @NagarajuKasa, okay thank you, here is my question, you can update if found anything please,magento.stackexchange.com/questions/274520/… Commented May 15, 2019 at 6:23
1

Old question, and posted answer is valid for sure

Just wanted to point that the mentioned event (sales_quote_add_item) in question is available in Magento2, and it works fine for the asked customization with very few code

public function execute(\Magento\Framework\Event\Observer $observer)
{
    // your logic to get $yourPrice
    $quoteItem = $observer->getEvent()->getQuoteItem();
    $quoteItem->setOriginalCustomPrice($yourPrice);
}

Note that you shouldn't save() QuoteItem object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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