Jump to content

[Solved] Add a new function in class/Tools.php


cactusman2

Recommended Posts

Hi,

I'm trying to add a new function in the file class/Tools.php
So 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

Thanks for your answer

I 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

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!

22934_Ev6MFWuIqDXrEmFBJGfq_t

22935_QkM8eKsEUSI7lgibqren_t

Link to comment
Share on other sites

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

Thanks for your answer.

Actually I just did the same way as convertPrice() which is declared in Tools.php not in Product.php
Even 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 function

I 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 function

I'm lost!
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...