Jump to content

[SOLVED] Inserting php in a custom module


Recommended Posts

Hi, I want to use insert some php code (this: http://www.finalwebsites.com/forums/topic/how-to-use-maxmind-geoip-for-websites#post-1591) in order to show the result in the right column of my site.

 

I've read quite a bit about this and I got the impression that the cleanest solution is to create a custom module (please, tell me if I'm wrong and there is a better solution).

 

I've created my custom module following the instructions of http://www.technoreply.com/how-to-create-a-prestashop-1-5-module/ and a little bit of http://doc.prestashop.com/display/PS15/Creating+a+PrestaShop+module.

 

It works OK for showing some text (either putting the text directly in the tpl file or putting something like 'mymodule_message' => $this->l('example text') in mymodule.php).

 

But I don't want show a "fixed" text message, I want to insert php code and show the result of that code. I don't have any idea of where shoud I put the php code to make it work.

 

Now the "interesting" part of mymodule.php says:

public function hookDisplayLeftColumn()
{
$this->context->smarty->assign(array(
'placement' => 'left',
'my_module_message' => $this->l('example text') // Do not forget to enclose your strings in the l() translation method
));

return $this->display(__FILE__, 'left.tpl');
}

Should I put the php code in this part, create a new php file and call it from here or what?

 

Thanks for your help and sorry for asking noobie questions, but that's what I am right now (I don't know much about prestashop or php, but willing to learn)...  :unsure:

 

 

Edited by pedrogon (see edit history)
Link to comment
Share on other sites

if you want a variable text in my_module_message

 

then before assiging the smart array variable

 

add php code above this assign and withing the function to set my_module_message = ($variable_message)

 

for example

if ($x == foo)

   $variable_message = 'equal to foo';

else

  $variable_message = 'not equal foo';

then assign:

'my_module_message' => $variable_message,


I hope I understood your question and helped accordingly.

Link to comment
Share on other sites

Thanks a lot for your quick responses!

 

I've tried El Patron's solution and works like a charm.

 

Enrique, could you please give me an example of what would be the code for calling a external function and assigning it? (or a link to the relevant documentation)

 

 

 

P.S.: Curious, 3 spanish speakers talking in the english forum...

Link to comment
Share on other sites

jajaja..two Spanish speakers...yo soy malo hablo espanol...but I have houses in Colombia and Spain.

 

as for your question, here is an example of obtaining a price

 

where Tools is the classand  displayPrice is the funciton within Tools

			$this->smarty->assign(array(
				'total_to_pay' => Tools::displayPrice($params['total_to_pay'], $params['currencyObj'], false),

Link to comment
Share on other sites

I was about to put a similar example.... The Autoload class makes available the php code in /classes and /controllers folder (and override folders) so you have available the utility static methods of Tools class. But you can also import your classes with for example

require_once( _PS_MODULE_DIR_ . 'your_module_folder/your_module_subfolder/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...