Zencart货币类函数介绍

currencies 类就是zencart关于货币处理的PHP类。代码位于\includes\classes\currencies.php 文件中。
该类的函数有一个构造函数和7个成员函数,他们分别为
function currencies()
function display_price($products_price, $products_tax, $quantity = 1)
function get_decimal_places($code)
function get_value($code)
function is_set($code)
function value($number, $calculate_currency_value = true, $currency_type = ‘’, $currency_value = ‘’)
function rateAdjusted($number, $calculate_currency_value = true, $currency_type = ‘’, $currency_value = ‘’)
function format($number, $calculate_currency_value = true, $currency_type = ‘’, $currency_value = ‘’)
下面就简单的介绍这些函数的功能。
currencies 类的构造函数主要就是给一些成员变量赋初始化的值。该类跟数据库表currencies是相关的,所以成员变量的初始值就是从数据库currencies表读取内容的。
构造函数的原代码如下: function currencies() { global $db; $this->currencies = array();///该数组变量就是保存货币类型的,因为Zencart支持多货币。 $currencies_query = "select code, title, symbol_left, symbol_right, decimal_point, thousands_point, decimal_places, value from " . TABLE_CURRENCIES;///查询currencies表获取内容 $currencies = $db->Execute($currencies_query); while (!$currencies->EOF) {///填充currencies数组,货币代码为该数组的键值 $this->currencies[$currencies->fields['code']] = array('title' => $currencies->fields['title'],//货币的名称 'symbol_left' => $currencies->fields['symbol_left'],//货币的左边符号 'symbol_right' => $currencies->fields['symbol_right'],//右边符号 'decimal_point' => $currencies->fields['decimal_point'],///十分点符号 'thousands_point' => $currencies->fields['thousands_point'],///千分点符号 'decimal_places' => $currencies->fields['decimal_places'],//小数点有效位数 'value' => $currencies->fields['value']);//货币的汇率 $currencies->MoveNext(); } } function format($number, $calculate_currency_value = true, $currency_type = ‘’, $currency_value = ‘’)
该函数就是格式化输出一个数字对应货币的价格。比如把10按美元的形式显示出来。
$number :就是要输出的数字,比如10
$calculate_currency_value :是否按照当前货币的汇率来计算该数值。默认是要按照当前货币的汇率来计算
$currency_type :货币类型,就是货币代码,比如美元就是USD,如果没有给这个参数赋值的话,就使用当前选定的货币类型。
$currency_value: 该货币的汇率。
该函数的原型代码: function format($number, $calculate_currency_value = true, $currency_type = '', $currency_value = '') { if (empty($currency_type)) $currency_type = $_SESSION['currency'];///如果货币类型没有指定,就使用当前选定的货币类型。 if ($calculate_currency_value == true) { $rate = (zen_not_null($currency_value)) ? $currency_value : $this->currencies[$currency_type]['value']; $format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(zen_round($number * $rate, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right']; } else { $format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(zen_round($number, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right']; } if ((DOWN_FOR_MAINTENANCE=='true' and DOWN_FOR_MAINTENANCE_PRICES_OFF=='true') and (!strstr(EXCLUDE_ADMIN_IP_FOR_MAINTENANCE, $_SERVER['REMOTE_ADDR']))) { $format_string= ''; } return $format_string;///返回该该显示的字符串,比如把10按美元的形式显示,那么这个函数返回的字符串就是$10.00 } function rateAdjusted($number, $calculate_currency_value = true, $currency_type = '', $currency_value = '') 该函数跟format()函数一样的功能,不同的是这个函数没有显示货币的符号,只是返回一个数字
function value($number, $calculate_currency_value = true, $currency_type = ‘’, $currency_value = ‘’)
同上。
function is_set($code)
该函数就是判断是否存在某个货币类型。输入参数为货币的代码function is_set($code) { if (isset($this->currencies[$code]) && zen_not_null($this->currencies[$code])) { return true; } else { return false; } } 获取该货币的汇率 function get_value($code) { return $this->currencies[$code]['value']; } 获取该货币的小数点位数 function get_decimal_places($code) { return $this->currencies[$code]['decimal_places']; } 显示当前货币类型的产品价格,输入产品的ID和税率,还有产品的数量。 function display_price($products_price, $products_tax, $quantity = 1) { return $this->format(zen_add_tax($products_price, $products_tax) * $quantity); }