最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > Magento > Magento教程 >

Magento货币设置与货币切换

时间:2014-06-02 08:04来源:未知 作者:最模板zuimoban 点击:
每个网站都会有一个基础货币,还可以有其它的多种货币,其它的货币根据相对于基础货币的汇率计算切换的结果值。 举例: 假如基本货币是美元,同时还有欧元和英镑,那么基础货币的汇率

每个网站都会有一个基础货币,还可以有其它的多种货币,其它的货币根据相对于基础货币的汇率计算切换的结果值。

举例:
假如基本货币是美元,同时还有欧元和英镑,那么基础货币的汇率总是1,欧元和英镑的汇率都应该设置为相对人民币的汇率,说得通俗点就是1基础货币,能换多少欧元或英镑,这个值就是要设置的汇率(这里是1美金->0.78欧元, 1美金->0.66英镑)。

由于Magento是多网站系统,每个网站都可以设置基础货币。比如网站A的基础货币是美元,B网站的基础货币是英镑。

但是Magento中默认所有网站都是使用相同的基础货币。比如基础货币为美元,A和B网站的基础货币都是美元,如果改为英镑,A和B都跟着改。所有这里有一个基础货币域的概念。

这个设置在System->Configuration->Catalog->Price->Catalog Price Scope,这里有两个选项:Global 和 Website。默认是Global,意思是全部网站都使用同一基础货币,如果是Website,那么就可以针对网站指定基础货币。

货币的设置在System->Configuration->Currency Setup:
Magento货币设置安装

在默认配置下,可以修改基础货币,默认显示的货币(一般应该是和基础货币一致),然后是允许的货币。当切换到具体的Website时:

Magento货币设置

可以看到,基础货币可以修改,默认显示货币也可以修改,允许的货币也是。这里只所以允许修改基础货币,那是因为我修改了基础货币作用域为Website,如果是Global,这里的Base Currency就不显示了(使用全局的配置)。

在Allowed Currencies里面选择货币,然后去System->Manage Currency设置货币汇率和符号:
Magento汇率设置
Magento货币符号设置

另外,货币的汇率设置是存放在directory_currency_rate表中的:
Magento汇率表

货币设置就这些内容(需要理解基础货币 和 基础货币作用域 和 显示货币的汇率)。
以上的货币的配置信息对应到全局XML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<currency>
<options>
<allow>EUR,USD</allow>
<base>USD</base>
<default>USD</default>
</options>
<webservicex>
<timeout>100</timeout>
</webservicex>
<import>
<enabled>0</enabled>
<error_email/>
<error_email_identity>general</error_email_identity>
<error_email_template>currency_import_error_email_template</error_email_template><service>webservicex</service>
<time>00,00,00</time>
<frequency>D</frequency>
</import>
</currency>

在默认的安装模板中,我们看到,当显示货币大于1时,边框就会出现货币切换选择框。通过查看包布局XML(http://blog.ifeeline.com/showLayout=package)可以获取如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<catalog_category_default>
<reference name="left">
<block type="directory/currency" name="currency" before="catalog.leftnav" template="directory/currency.phtml"/>
</reference>
</catalog_category_default>
 
<catalog_category_layered>
<reference name="left">
<block type="directory/currency" name="currency" before="catalog.leftnav" template="directory/currency.phtml"/>
</reference>
</catalog_category_layered>
 
<catalogsearch_advanced_index>
<reference name="left">
<block type="directory/currency" name="right_currency" before="-" template="directory/currency.phtml"/>
</reference>
</catalogsearch_advanced_index>
 
<catalogsearch_result_index>
<reference name="left">
<block type="directory/currency" name="currency" before="-" template="directory/currency.phtml"/>
</reference>
</catalogsearch_result_index>
 
<catalogsearch_advanced_result>
<reference name="right">
<block type="directory/currency" name="right_currency" before="-" template="directory/currency.phtml"/>
</reference>
</catalogsearch_advanced_result>

有5个地方出现货币切换。
Magento货币切换块

如果想把它放置在header中如何弄?
很简单,只要在header中添加这个Block:
Magento块配置

然后在布局文件的local.xml中添加:

1
2
3
4
5
6
7
8
<default>
    <reference name="header">
        <block type="directory/currency" name="custom_currency_selector" template="directory/currency.phtml"/>
    </reference>
    <!-- 
    <remove name="currency"/>
    -->
</default>

模板还是使用系统默认的directory/currency.phtml,也可以修改下。然后刷新缓存,头部就会出现货币下拉选项了。

如果头部出现了货币下拉选项,应该就不需要边框再出现货币选项了吧。所以,可以在default节点下添加remove节点用name指定为currency即可。

对于货币,我们继续深入一些,看看Mage_Directory_Block_Currency这个块类提供的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Mage_Directory_Block_Currency extends Mage_Core_Block_Template
{
    // 有多少种货币
    public function getCurrencyCount()
    {
        return count($this->getCurrencies());
    }
    // 获取全部已经安装的货币(允许的货币(可能不包含基础货币)并已经设置了汇率)
    public function getCurrencies()
    {
        $currencies = $this->getData('currencies');
        if (is_null($currencies)) {
            $currencies = array();
            // getAvailableCurrencyCodes(true)就是返回允许的货币(true表示直接取得允许列表,不管是否包含基础货币)
            $codes = Mage::app()->getStore()->getAvailableCurrencyCodes(true);
            if (is_array($codes) && count($codes) > 1) {
                // 货币的汇率保存在数据库,这里是获取汇率(指定了基础货币)
                $rates = Mage::getModel('directory/currency')->getCurrencyRates(
                    Mage::app()->getStore()->getBaseCurrency(),
                    $codes
                );
                // 然后对获取的货币代码进行验证(是否已经设置了汇率)
                foreach ($codes as $code) {
                    if (isset($rates[$code])) {
                        $currencies[$code] = Mage::app()->getLocale()
                            ->getTranslation($code, 'nametocurrency');
                    }
                }
            }
 
            $this->setData('currencies', $currencies);
        }
        return $currencies;
    }
    // 获取切换的URL
    public function getSwitchUrl()
    {
        return $this->getUrl('directory/currency/switch');
    }
    // 获取切换货币链接
    public function getSwitchCurrencyUrl($code)
    {
        return Mage::helper('directory/url')->getSwitchCurrencyUrl(array('currency' => $code));
    }
 
    // 获取当前货币代码
    public function getCurrentCurrencyCode()
    {
        if (is_null($this->_getData('current_currency_code'))) {
            // do not use Mage::app()->getStore()->getCurrentCurrencyCode() because of probability
            // to get an invalid (without base rate) currency from code saved in session
            $this->setData('current_currency_code', Mage::app()->getStore()->getCurrentCurrency()->getCode());
        }
 
        return $this->_getData('current_currency_code');
    }
}

看看Mage_Core_Model_Store对货币提供的方法,这些方法虽然不少,但是都非常简单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*************************************************************************************
   * Store currency interface
   */
 
  /**
   * Retrieve store base currency code
   *
   * @return string
   */
  // 首先判断基础货币的作用域,如果是Global,就使用全局的设置,否则才使用网站或商店的设置
  public function getBaseCurrencyCode()
  {
      $configValue = $this->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
      if ($configValue == Mage_Core_Model_Store::PRICE_SCOPE_GLOBAL) {
          return Mage::app()->getBaseCurrencyCode();
      } else {
          return $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
      }
  }
 
  /**
   * Retrieve store base currency
   *
   * @return Mage_Directory_Model_Currency
   */
  // 一个保存了基础货币的Mage_Directory_Model_Currency对象
  public function getBaseCurrency()
  {
      $currency = $this->getData('base_currency');
      if (is_null($currency)) {
          $currency = Mage::getModel('directory/currency')->load($this->getBaseCurrencyCode());
          $this->setData('base_currency', $currency);
      }
      return $currency;
  }
 
  /**
   * Get default store currency code
   *
   * @return string
   */
  // 直接从配置中获取,这个就是默认显示的货币的代码
  public function getDefaultCurrencyCode()
  {
      $result = $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_DEFAULT);
      return $result;
  }
 
  /**
   * Retrieve store default currency
   *
   * @return Mage_Directory_Model_Currency
   */
  // 一个保存了默认显示货币的Mage_Directory_Model_Currency对象
  public function getDefaultCurrency()
  {
      $currency = $this->getData('default_currency');
      if (is_null($currency)) {
          $currency = Mage::getModel('directory/currency')->load($this->getDefaultCurrencyCode());
          $this->setData('default_currency', $currency);
      }
      return $currency;
  }
 
  /**
   * Set current store currency code
   *
   * @param   string $code
   * @return  string
   */
  // 设置当前货币代码,写入会话  和  cookie
  public function setCurrentCurrencyCode($code)
  {
      $code = strtoupper($code);
      if (in_array($code, $this->getAvailableCurrencyCodes())) {
          // 把这个代码写入session中
          $this->_getSession()->setCurrencyCode($code);
          // 如果设置的代码和默认显示的一样,删除currency的cookie,否则设置一个cookie
          if ($code == $this->getDefaultCurrency()) {
              Mage::app()->getCookie()->delete(self::COOKIE_CURRENCY, $code);
          } else {
              Mage::app()->getCookie()->set(self::COOKIE_CURRENCY, $code);
          }
      }
      return $this;
  }
 
  /**
   * Get current store currency code
   *
   * @return string
   */
  // 获取当前货币代码,首先从会话中获取,否则就是默认显示的货币代码,如果默认显示的不在允许列表中,返回允许的第一个货币代码
  public function getCurrentCurrencyCode()
  {
      // try to get currently set code among allowed
      $code = $this->_getSession()->getCurrencyCode();
      if (empty($code)) {
          $code = $this->getDefaultCurrencyCode();
      }
      if (in_array($code, $this->getAvailableCurrencyCodes(true))) {
          return $code;
      }
 
      // take first one of allowed codes
      $codes = array_values($this->getAvailableCurrencyCodes(true));
      if (empty($codes)) {
          // return default code, if no codes specified at all
          return $this->getDefaultCurrencyCode();
      }
      return array_shift($codes);
  }
 
  /**
   * Get allowed store currency codes
   *
   * If base currency is not allowed in current website config scope,
   * then it can be disabled with $skipBaseNotAllowed
   *
   * @param bool $skipBaseNotAllowed
   * @return array
   */
  // 获取所有可用的货币代码,如果true,直接返回允许的货币代码,否则如果基础货币不在允许列表中,把基础货币添加到允许列表一起返回
  public function getAvailableCurrencyCodes($skipBaseNotAllowed = false)
  {
      $codes = $this->getData('available_currency_codes');
      if (is_null($codes)) {
          $codes = explode(',', $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_ALLOW));
          // add base currency, if it is not in allowed currencies
          $baseCurrencyCode = $this->getBaseCurrencyCode();
          if (!in_array($baseCurrencyCode, $codes)) {
              $codes[] = $baseCurrencyCode;
 
              // save base currency code index for further usage
              $disallowedBaseCodeIndex = array_keys($codes);
              $disallowedBaseCodeIndex = array_pop($disallowedBaseCodeIndex);
              $this->setData('disallowed_base_currency_code_index', $disallowedBaseCodeIndex); //记录了不允许的货币的下标
          }
          $this->setData('available_currency_codes', $codes);
      }
 
      // remove base currency code, if it is not allowed by config (optional)
      if ($skipBaseNotAllowed) {
          $disallowedBaseCodeIndex = $this->getData('disallowed_base_currency_code_index');
          if (null !== $disallowedBaseCodeIndex) {
              unset($codes[$disallowedBaseCodeIndex]);
          }
      }
      return $codes;
  }
 
  /**
   * Retrieve store current currency
   *
   * @return Mage_Directory_Model_Currency
   */
  // 获取当前货币对象,如果汇率没有设置就修改成基础货币,意思是说,完全可以设置一个没有汇率的货币代码为当前货币代码
  public function getCurrentCurrency()
  {
      $currency = $this->getData('current_currency');
 
      if (is_null($currency)) {
          $currency     = Mage::getModel('directory/currency')->load($this->getCurrentCurrencyCode());
          $baseCurrency = $this->getBaseCurrency();
 
          if (! $baseCurrency->getRate($currency)) {
              $currency = $baseCurrency;
              $this->setCurrentCurrencyCode($baseCurrency->getCode());
          }
 
          $this->setData('current_currency', $currency);
      }
 
      return $currency;
  }
 
  /**
   * Retrieve current currency rate
   *
   * @return float
   */
  // 获取当前货币的汇率,看到,首先使用基础货币对象,然后调用它的getRate()方法,把当前货币对象传入,这个过程表示基础货币到当前货币的汇率
  public function getCurrentCurrencyRate()
  {
      return $this->getBaseCurrency()->getRate($this->getCurrentCurrency());
  }
 
  /**
   * Convert price from default currency to current currency
   *
   * @param   double $price
   * @param   boolean $format             Format price to currency format
   * @param   boolean $includeContainer   Enclose into <span class="price"><span>
   * @return  double
   */
  // 把提供的价格转换为当前货币值的值(提供的价格被看做是基础价格,转换为当前货币表示的数值)
  public function convertPrice($price, $format = false, $includeContainer = true)
  {
      if ($this->getCurrentCurrency() && $this->getBaseCurrency()) {
          $value = $this->getBaseCurrency()->convert($price, $this->getCurrentCurrency());
      } else {
          $value = $price;
      }
 
      if ($this->getCurrentCurrency() && $format) {
          $value = $this->formatPrice($value, $includeContainer);
      }
      return $value;
  }
 
  /**
   * Round price
   *
   * @param mixed $price
   * @return double
   */
  //对价格四舍五入
  public function roundPrice($price)
  {
      return round($price, 2);
  }
 
  /**
   * Format price with currency filter (taking rate into consideration)
   *
   * @param   double $price
   * @param   bool $includeContainer
   * @return  string
   */
  // 格式化价格,这个价格是当前设置货币的值,实际调用当前货币对象的format方法
  public function formatPrice($price, $includeContainer = true)
  {
      if ($this->getCurrentCurrency()) {
          return $this->getCurrentCurrency()->format($price, array(), $includeContainer);
      }
      return $price;
  }

Mage_Directory_Model_Currency类中提供的方法(部分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class Mage_Directory_Model_Currency extends Mage_Core_Model_Abstract
{
    // 获取代码
    public function getCode()
    {
        return $this->_getData('currency_code');
    }
 
    public function getCurrencyCode()
    {
        return $this->_getData('currency_code');
}
    /**
     * Currency Rates getter
     *
     * @return array
     */
    // 获取汇率
    public function getRates()
    {
        return $this->_rates;
    }
 
    /**
     * Currency Rates setter
     *
     * @param array Currency Rates
     * @return Mage_Directory_Model_Currency
     */
    //设置汇率
    public function setRates(array $rates)
    {
        $this->_rates = $rates;
        return $this;
    }
 
    /**
     * Loading currency data
     *
     * @param   string $id
     * @param   string $field
     * @return  Mage_Directory_Model_Currency
     */
    public function load($id, $field=null)
    {
        $this->unsRate();
        $this->setData('currency_code', $id);
        return $this;
    }
 
    /**
     * Get currency rate (only base=>allowed)
     *
     * @param   string $toCurrency
     * @return  double
     */
    //获取某个货币的汇率(基础货币到允许的货币汇率)
    public function getRate($toCurrency)
    {
        if (is_string($toCurrency)) {
            $code = $toCurrency;
        } elseif ($toCurrency instanceof Mage_Directory_Model_Currency) {
            $code = $toCurrency->getCurrencyCode();
        } else {
            throw Mage::exception('Mage_Directory', Mage::helper('directory')->__('Invalid target currency.'));
        }
        $rates = $this->getRates();
        if (!isset($rates[$code])) {
            $rates[$code] = $this->_getResource()->getRate($this->getCode(), $toCurrency);
            $this->setRates($rates);
        }
        return $rates[$code];
    }
 
    /**
     * Get currency rate (base=>allowed or allowed=>base)
     *
     * @param   string $toCurrency
     * @return  double
     */
    public function getAnyRate($toCurrency)
    {
        if (is_string($toCurrency)) {
            $code = $toCurrency;
        } elseif ($toCurrency instanceof Mage_Directory_Model_Currency) {
            $code = $toCurrency->getCurrencyCode();
        } else {
            throw Mage::exception('Mage_Directory', Mage::helper('directory')->__('Invalid target currency.'));
        }
        $rates = $this->getRates();
        if (!isset($rates[$code])) {
            $rates[$code] = $this->_getResource()->getAnyRate($this->getCode(), $toCurrency);
            $this->setRates($rates);
        }
        return $rates[$code];
}
 
    /**
     * Convert price to currency format
     *
     * @param   double $price
     * @param   string $toCurrency
     * @return  double
     */
    // 货币值转换
    public function convert($price, $toCurrency=null)
    {
        if (is_null($toCurrency)) {
            return $price;
        }
        elseif ($rate = $this->getRate($toCurrency)) {
            return $price*$rate;
        }
 
        throw new Exception(Mage::helper('directory')->__('Undefined rate from "%s-%s".', $this->getCode(), $toCurrency->getCode()));
    }
}
 

(责任编辑:最模板)
------分隔线----------------------------
栏目列表
推荐内容