0

I want to apply custom price for the current order. I read that I should use sales_quote_add_item, But its look like I am doing something Wrong. I found some tutorial 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 magento2 given on below link for magento1.

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

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

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.

  • 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? – Ranjit Shinde Jun 19 '18 at 9:01
  • @RanjitShinde the above event is working at front end after add to cart. – Nagaraju Kasa Jun 19 '18 at 11:02
  • @NagarajuKasa, can we set custom taxamount for the quote item using this event? – jafar pinjar May 15 at 6:05
  • i am note sure @jafarpinjar i will check and getback to you. – Nagaraju Kasa May 15 at 6:21
  • @NagarajuKasa, okay thank you, here is my question, you can update if found anything please,magento.stackexchange.com/questions/274520/… – jafar pinjar May 15 at 6:23

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.