如何设置GBP作为magento中美国客户的默认货币?

时间:2014-03-28 19:06:53

标签: magento currency geoip

我有一个有多种货币的洋红色商店。产品价格根据IP地址以相关货币显示。 我想将GBP设置为美国客户的默认货币,即代替美元(根据IP地址),价格应以美元显示为美国客户。

我是magento的新手,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

我为这个作品创作了一个,你可以试试这个...

步骤1:在app/code/local/Amit/AutoCurrency/etc/config.xml

下创建config.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Amit_AutoCurrency>
            <version>1.0.0</version>
        </Amit_AutoCurrency>
    </modules>
    <global>
        <models>            
            <core>
                <rewrite>
                    <store>Amit_AutoCurrency_Model_Store</store>
                </rewrite>
            </core>
        </models>        
        <helpers>
            <autocurrency>
                <class>Amit_AutoCurrency_Helper</class>
            </autocurrency>
        </helpers>      
    </global>
</config>

在store.php中是app / code / local / Amit / AutoCurrency / Model / Store.php

<?php

class Amit_AutoCurrency_Model_Store extends Mage_Core_Model_Store
{       
    /**
     * Update default store currency code
     *
     * @return string
     */
    public function getDefaultCurrencyCode()
    {
        $result = $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_DEFAULT);       

        return $this->getCurrencyCodeByIp($result);     
    }

    /**
     * Get Currency code by IP Address
     *
     * @return string
     */
    public function getCurrencyCodeByIp($result = '') 
    {
        // load GeoIP binary data file
        $geoIp = Mage::helper('autocurrency')->loadGeoIp();

        $ipAddress = Mage::helper('autocurrency')->getIpAddress();

        // get country code from ip address
        $countryCode = geoip_country_code_by_addr($geoIp, $ipAddress);

        if($countryCode == '') {
            return $result;
        }

        // get currency code from country code
        $currencyCode = geoip_currency_code_by_country_code($geoIp, $countryCode);

        // close the geo database  
        geoip_close($geoIp);    

        // if currencyCode is not present in allowedCurrencies
        // then return the default currency code
        $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();      
        if(!in_array($currencyCode, $allowedCurrencies)) {
                if($currencyCode="USD"){
                return "GBP";
                }

            return $result;
        }
        if($currencyCode="USD"){
        return "GBP";
        }


        return $currencyCode;
    }
}

帮助功能是app / code / local / Amit / AutoCurrency / Helper / Data.php

<?php
 class Amit_AutoCurrency_Helper_Data extends Mage_Core_Helper_Abstract
{
    /**
     * Load GeoIP binary data file
     *
     * @return string
     */
    public function loadGeoIp() 
    {   
        // Load geoip.inc
        include_once(Mage::getBaseDir().'/var/geoip/geoip.inc');

        // Open Geo IP binary data file
        $geoIp = geoip_open(Mage::getBaseDir().'/var/geoip/GeoIP.dat',GEOIP_STANDARD);

        return $geoIp;
    }

    /**
     * Get IP Address
     *
     * @return string
     */
    public function getIpAddress() 
    {   
        //return "124.41.230.51";
        return $_SERVER['REMOTE_ADDR'];
    }
}

应用程序的/ etc /模块/ Amit_AutoCurrency.php

<?xml version="1.0"?>
<config>
  <modules>
      <Amit_AutoCurrency>
          <active>true</active>
          <codePool>local</codePool>
      </Amit_AutoCurrency>
  </modules>
</config>

下载geoip文件转到

https://github.com/maxmind/geoip-api-php

我希望它对你有用

相关问题