cactusman2 Posted May 1, 2010 Share Posted May 1, 2010 Hi,I'm trying to add a new function in the file class/Tools.phpSo first to test a little bit I copied the function ConvertPrice() and create ConvertPrice2() just under.ConvertPrice2() is the exact copy of ConvertPrice(), only the name is different.Then I opened a module template file using this function (modules/blocknewproducts/newproducts.tpl) and I replaced {convertPrice price=$product.price} by {convertPrice2 price=$product.price}.Unfortunately this doesn't work. Do I need to declare or load the new function somewhere? Is there a way to make it work.I can not create a module neither modify the template file. The only way to do what I want is to add a new function in Tools.php.Thanks ! Link to comment Share on other sites More sharing options...
Burhan BVK Posted May 1, 2010 Share Posted May 1, 2010 You need to register this function with smarty. Check init.php, it is where the presta specific functions are added to smarty. Link to comment Share on other sites More sharing options...
cactusman2 Posted May 2, 2010 Author Share Posted May 2, 2010 Thanks for your answerI still have a problem.I have added this line of code to init.php: $smarty->register_function('convertPrice2', array('Product', 'convertPrice2')); After I did that I don't have any error but the price doesn't appear. If I rename the function ConvertPrice() in modules/blocknewproducts/newproducts.tpl the price appear.Any idea? Link to comment Share on other sites More sharing options...
Burhan BVK Posted May 2, 2010 Share Posted May 2, 2010 The class name in the register_function should match the class you added the new function. Link to comment Share on other sites More sharing options...
cactusman2 Posted May 3, 2010 Author Share Posted May 3, 2010 Thanks again!I'm not sure I understand what you mean so Il will be more specific.Here is the part of the code in class/Tools.php (function convertPrice2 added) static public function convertPrice($price, $currency = NULL) { if ($currency === NULL) $currency = Currency::getCurrent(); $c_id = (is_array($currency) ? $currency['id_currency'] : $currency->id); $c_rate = (is_array($currency) ? $currency['conversion_rate'] : $currency->conversion_rate); if ($c_id != intval(Configuration::get('PS_CURRENCY_DEFAULT'))) $price *= $c_rate; return $price; } static public function convertPrice2($price, $currency = NULL) { if ($currency === NULL) $currency = Currency::getCurrent(); $c_id = (is_array($currency) ? $currency['id_currency'] : $currency->id); $c_rate = (is_array($currency) ? $currency['conversion_rate'] : $currency->conversion_rate); if ($c_id != intval(Configuration::get('PS_CURRENCY_DEFAULT'))) $price *= $c_rate; return $price; } Here is the part of the code in init.php (register the function) $smarty->register_function('convertPrice', array('Product', 'convertPrice')); $smarty->register_function('convertPrice2', array('Product', 'convertPrice2')); Here is part of the code in modules/blocknewproducts/newproducts.tpl (use new function in template) {if !$priceDisplay || $priceDisplay == 2} {convertPrice price=$product.price}{if $priceDisplay == 2} {l s='+Tx' mod='homefeatured'}{/if} {convertPrice2 price=$product.price}{/if} After I did that nothing change the second price does not appear (see picture image1.png)Now if i change the code this way: {if !$priceDisplay || $priceDisplay == 2} {convertPrice price=$product.price}{if $priceDisplay == 2} {l s='+Tx' mod='homefeatured'}{/if} {convertPrice price=$product.price}{/if} the second price appear (because I'm using convertPrice() and no convertPrice2() (see image2.png)Thanks again! Link to comment Share on other sites More sharing options...
Eihwaz Posted May 3, 2010 Share Posted May 3, 2010 As whitelighter said, $smarty->register_function('convertPrice2', array('Product', 'convertPrice2')); This line means convertPrice2 is defined in classes/Product.php, in your case, this function is defined in classes/Tools.php. Link to comment Share on other sites More sharing options...
cactusman2 Posted May 3, 2010 Author Share Posted May 3, 2010 Thanks for your answer.Actually I just did the same way as convertPrice() which is declared in Tools.php not in Product.phpEven this function being declared in Tools.php the line in init.php is $smarty->register_function('convertPrice', array('Product', 'convertPrice')); That's why I did that too for convertPrice2.Anyway I tried to replace Product by Tools this way $smarty->register_function('convertPrice2', array('Tools', 'convertPrice2')); but it doesn't work the page stop loading when calling the functionI also tried to keep the line $smarty->register_function('convertPrice2', array('Product', 'convertPrice2')); and to move the function to Product.php but same problem the page stop loading when calling the functionI'm lost! Link to comment Share on other sites More sharing options...
Burhan BVK Posted May 3, 2010 Share Posted May 3, 2010 If you want modify the convertPrice functionality that is in the smarty, you need to duplicate the convertprice functions that is declared in the Product.php class file, not the one in the Tools class. Link to comment Share on other sites More sharing options...
cactusman2 Posted May 3, 2010 Author Share Posted May 3, 2010 What I actually want to do is to copy the convertPrice function which is declared in Tools.php because this is the one which display price in module newproducts. In addition I can't find anywhere in product.php where the function is declared :-(Maybe the function convertPrice I'm talking about (the one in Tools) is not registered to smarty with init.php? Link to comment Share on other sites More sharing options...
cactusman2 Posted May 4, 2010 Author Share Posted May 4, 2010 Sorry to both of us I found the function in Product.phpThanks again ! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now