I faced the same issue while using the above code to add a custom price. I found the below GitHub link which helped me to solve it.
https://github.com/magento/magento2/issues/27161
https://github.com/magento/magento2/issues/33134
[Magento Version 2.4.x]
app/code/Msquare/ModuleName/etc/frontend
events.xml
<?xml version="1.0"?>
<!--
/**
* @category Msquare
* @package Msquare_CustomPriceFix
* @author Mahesh Makwana <[email protected]>
*/
-->
<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="custom_price_add" instance="Msquare\CustomPriceFix\Observer\CustomPrice" />
</event>
</config>
app/code/Msquare/ModuleName/Observer
CustomPrice.php
<?php
/**
* @category Msquare
* @package Msquare_CustomPriceFix
* @author Mahesh Makwana <[email protected]>
*/
namespace Msquare\CustomPriceFix\Observer;
use Msquare\CustomPriceFix\Helper\Data as DataHelper;
use Magento\Framework\Currency\Data\Currency as CurrencyData;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class CustomPrice implements ObserverInterface
{
/**
* @param DataHelper $dataHelper
*/
public function __construct(
protected DataHelper $dataHelper
) {}
public function execute(Observer $observer)
{
$item = $observer->getEvent()->getData('quote_item');
$item = ($item->getParentItem() ? $item->getParentItem() : $item);
$price = 100; //set your price here
$curentCurrency = $this->dataHelper->getCurrentCurrency();
$baseCurrency = $this->dataHelper->getBaseCurrency();
if ($curentCurrency != $baseCurrency) {
$currencyObj = $this->dataHelper->getCurrencyFactory();
$rate = $currencyObj->load($baseCurrency)->getAnyRate($curentCurrency);
$price *= $rate;
$price = (float) $currencyObj->format($price, ['display' => CurrencyData::NO_SYMBOL], false);
}
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);
}
}
app/code/Msquare/ModuleName/Helper
Data.php
<?php
/**
* @category Msquare
* @package Msquare_CustomPriceFix
* @author Mahesh Makwana <[email protected]>
*/
namespace Msquare\CustomPriceFix\Helper;
use Magento\Checkout\Model\CartFactory;
use Magento\Directory\Model\CurrencyFactory;
use Magento\Store\Model\StoreManagerInterface;
class Data
{
public function __construct(
protected StoreManagerInterface $storeManager,
protected CurrencyFactory $currencyFactory,
protected CartFactory $cartFactory
) {}
/**
* Get base currency code
*
* @return string
*/
public function getBaseCurrency()
{
return $this->storeManager->getStore()->getBaseCurrency()->getCurrencyCode();
}
/**
* Get current currency code
*
* @return string
*/
public function getCurrentCurrency()
{
return $this->storeManager->getStore()->getCurrentCurrency()->getCurrencyCode();
}
/**
* Get currency factory
*
* @return CurrencyFactory
*/
public function getCurrencyFactory()
{
return $this->currencyFactory->create();
}
/**
* Get cart items
*
* @return CartFactory
*/
public function getCartItems()
{
return $this->cartFactory->create()->getQuote()->getAllItems();
}
}
app/code/Msquare/ModuleName/etc/frontend
di.xml
<?xml version="1.0"?>
<!--
/**
* @category Msquare
* @package Msquare_CustomPriceFix
* @author Mahesh Makwana <[email protected]>
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Directory\Controller\Currency\SwitchAction">
<plugin name="custom_price_fix"
type="Msquare\CustomPriceFix\Plugin\Direcotry\Controller\Currency\SwitchAction"
sortOrder="10"/>
</type>
</config>
app/code/Msquare/ModuleName/Plugin/Direcotry/Controller/Currency
SwitchAction.php
<?php
/**
* @category Msquare
* @package Msquare_CustomPriceFix
* @author Mahesh Makwana <[email protected]>
*/
namespace Msquare\CustomPriceFix\Plugin\Direcotry\Controller\Currency;
use Msquare\CustomPriceFix\Helper\Data as DataHelper;
use Magento\Directory\Controller\Currency\SwitchAction as SwitchActionParent;
use Magento\Framework\Currency\Data\Currency as CurrencyData;
class SwitchAction
{
/**
* @param DataHelper $dataHelper
*/
public function __construct(
protected DataHelper $dataHelper
) {}
/**
* Currency switcher before plugin
*
* @param SwitchActionParent $subject
*
* @return void
*/
public function beforeExecute(SwitchActionParent $subject)
{
$items = $this->dataHelper->getCartItems();
$currencyObj = $this->dataHelper->getCurrencyFactory();
$baseCurrency = $this->dataHelper->getBaseCurrency();
$newCurrency = (string) $subject->getRequest()->getParam('currency');
if ($newCurrency != $baseCurrency) {
/* Update Price for Other Currency */
$rate = $currencyObj->load($baseCurrency)->getAnyRate($newCurrency);
foreach ($items as $item) {
$customPrice = $item->getBasePrice();
$customPrice *= $rate;
$customPrice = (float) $currencyObj->format($customPrice, ['display' => CurrencyData::NO_SYMBOL], false);
$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
$item->save();
}
} else {
/* Update Price for Base Currency */
foreach ($items as $item) {
$item->setCustomPrice($item->getBasePrice());
$item->setOriginalCustomPrice($item->getBasePrice());
$item->getProduct()->setIsSuperMode(true);
$item->save();
}
}
}
}
Github Issue : https://github.com/magento/magento2/issues/33134#issuecomment-1724410098
$price
in to the file only as a static value or it is a dynamic value coming from somewhere else?