/*************************************************************************************
* 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;
}