Reputation: 1030
Following code is used to set custom price for simple product. Custom price set in cart as needed but when i switch currency then custom price value remains same with current currency symbol.
$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
Is there any way to set custom price that work with currency switching.
Upvotes: 1
Views: 2529
Reputation: 488
Use below code hope it will help you ...
First Step:
//you need to find base currency code and then find current currency code...
$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$price = $customPrice;
Second Step:
//convert price from base currency to current currency
$customPrice = $Current_currency_price = Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode);
3rd Step:
then you can use your code:
$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
Upvotes: 1
Reputation: 1030
I found a solution to do this.
First Step:
Add item to quote with custom price using following code suggested by @Ashish Raj
$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$price = $customPrice;
$customPrice = $Current_currency_price = Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode);
$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
Second Step:
Second step is to create an controller post dispatch observer by adding following code in config.xml file of module
<events>
<controller_action_postdispatch>
<observers>
<frontend_currency_change>
<class>modulename/observer</class>
<method>hookToControllerActionPostDispatch</method>
</frontend_currency_change>
</observers>
</controller_action_postdispatch>
</events>
And add following code to observer class
public function hookToControllerActionPostDispatch($observer) {
if ($observer->getEvent()->getControllerAction()->getFullActionName() == 'directory_currency_switch') {
$quote = Mage::getSingleton('checkout/session')->getQuote();
if ($quote && $quote->hasItems()) {
foreach ($quote->getAllVisibleItems() as $item):
//add condition for target item
$customPrice = 23;//use custom price logic
$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$customPrice = Mage::helper('directory')->currencyConvert($customPrice, $baseCurrencyCode, $currentCurrencyCode);
$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
$quote->collectTotals()->save();
endforeach;
}
}
}
This is working for me. Hope this will help to someone having same issue. I will prefer if somebody have better solution. Thanks.
Upvotes: 3