Search code examples
phpmagentocart

Magento custom price value not converting by changing currency


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.


Solution

  • 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.