How to change the price in quote while adding product to cart?
-
15-04-2021 - |
Вопрос
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.
Решение
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.
Другие советы
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