3

My base currency is USD. I want to change according to customer-group i.e. If customer group "Retailer" the currency =EUR.

So far I managed to get customer's group code:

$groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$customer = Mage::getModel('customer/group')->load($groupId)->getCustomerGroupCode();

if($customer == 'Retailer') { /* Change currency to EUR */ }

Any help would be appreciated!

1

1 Answer 1

2

You can use an observer and set currency is below way. You just have to set your customer group ID here: if ($groupId == 1)

app/code/local/My/Module/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <My_Module>
            <version>0.0.1</version>
        </My_Module>
    </modules>
    <global>
        <models>
            <my_module>
                <class>My_Module_Model</class>
            </my_module>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_predispatch>
                <observers>
                    <change_currency>
                        <class>my_module/observer</class>
                        <method>changeCurrency</method>
                    </change_currency>
                </observers>
            </controller_action_predispatch>
        </events>
    </frontend>
</config>

app/code/local/My/Module/Model/Observer.php

<?php
class My_Module_Model_Observer extends Mage_Core_Model_Observer
{
    public function changeCurrency(Varien_Event_Observer $observer)
    {
        $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
        if ($groupId == 1) {
            Mage::app()->getStore()->setCurrentCurrencyCode('EUR');
        } else {
            Mage::app()->getStore()->setCurrentCurrencyCode('USD');
        }
    }
}
9
  • Had the same idea ... but was to slow :P
    – sv3n
    Jun 8, 2017 at 13:02
  • Thats good sv3n. But I try to give logic to user not the whole code. Don't have all time to write there peace of code :)
    – Priyank
    Jun 8, 2017 at 13:03
  • I understand. But if it results in question where to put the code it possbly takes more time :)
    – sv3n
    Jun 8, 2017 at 13:07
  • 1
    You can consider controller_action_predispatch as the first event dispatched in magento. Although there are few others. So It will check your customer group and set the currency in store front runtime. You can also check this event controller_front_init_before.
    – Priyank
    Jun 8, 2017 at 13:38
  • 1
    Is Euro set in allowed currency list
    – Priyank
    Jun 8, 2017 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.