name = 'customsmartyfunctions'; $this->tab = 'front_office_features'; $this->version = '1.0'; $this->author = 'Antadis'; parent::__construct(); $this->displayName = $this->l('Custom smarty functions'); $this->description = $this->l('Allow the use of custom smarty functions.'); } public function install() { if ( !parent::install() || !$this->registerHook('header') || !$this->registerHook('displayTop') || !$this->registerHook('displayPriceFormat') ) return false; return true; } public function uninstall(){ return parent::uninstall(); } public function hookDisplayTop($params){ } /** * Smarty function declarations */ public function hookHeader($params) { global $smarty; smartyRegisterFunction($smarty, 'function', 'convertPriceFormat', array(&$this,'convertPriceFormat')); smartyRegisterFunction($smarty, 'function', 'vd', array(&$this,'formatVarDump')); } public function hookDisplayPriceFormat($params){ return $this->convertPriceFormat($params); } /** * =========================== * CUSTOM FUNTIONS * =========================== **/ /** * Return price converted to specified format * * @param float $price Product price * @param object $currency Current currency object * @param boolean $to_currency convert to currency or from currency to default currency * @param string $format format to display (%p for rounded price (without cents), %c for cents, %$ for currecny sign) * @param int $decimals number of decimals for cents */ public function convertPriceFormat($params) { $price = $params['price']; if ( isset($params['decimals']) ) $decimals = $params['decimals']; else $decimals = 2; if ( isset($params['format']) ) $format = $params['format']; else $format = '%p,%c%$'; if ( isset($params['currency']) ) $currency = $params['currency']; else $currency = null; if ( isset($params['to_currency']) ) $to_currency = $params['to_currency']; else $to_currency = true; if( isset($params['debug']) ) $debug = $params['debug']; else $debug = false; $default_currency = (int)Configuration::get('PS_CURRENCY_DEFAULT'); $context = Context::getContext(); if ($currency === null) $currency = $context->currency; elseif (is_numeric($currency)) $currency = Currency::getCurrencyInstance($currency); $c_id = (is_array($currency) ? $currency['id_currency'] : $currency->id); $c_sign = (is_array($currency) ? $currency['sign'] : $currency->sign); $c_rate = (is_array($currency) ? $currency['conversion_rate'] : $currency->conversion_rate); if ($c_id != $default_currency) { if ($to_currency) $price *= $c_rate; else $price /= $c_rate; } $round_price = floor($price); $price_cents = str_pad(round($price - $round_price,2) * pow(10, $decimals), $decimals, "0", STR_PAD_LEFT); if ($price_cents >= pow(10, $decimals) ){ $round_price++; $price_cents = str_pad($price_cents - pow(10, $decimals), $decimals, "0", STR_PAD_LEFT); } $displayPrice = str_replace('%$', $c_sign, str_replace('%c', $price_cents, str_replace('%p', $round_price, $format))); if($debug){ $tab = ' '; $debug_display = '
Debug {
'.$tab.'decimals: '.$decimals.'
'.$tab.'format: '.$format.'
'.$tab.'currency sign: '.$c_sign.'
'.$tab.'currency rate: '.$c_rate.'
'.$tab.'price: '.$price.'
'.$tab.'round_price: '.$round_price.'
'.$tab.'price_cents: '.$price_cents.'
'.$tab.'display_price: '.$displayPrice.'
}