Jump to content

Custom PHP code, where to place


Recommended Posts

Hi All,

I have a piece of custom PHP code that I need to place somewhere in the file structure that would be accessible on all pages (cart, cats, cms, etc...). I have done digging around and see all fingers pointed at the controllers directory. I'm not really sure where/how to make the addition. I've tried to create a module, but getting stuck - abandoning this idea due to time crunch. Any help would be extremely appreciated.

Also I should note that I would need to access the output of the custom PHP code via AJAX from the frontend. I have all this hooked up and working on just a simple setup (XAMPP) php.php & js.js just need to know how to integrate it within Prestashop's structure.

Ideally this code would run early on in the lifecycles, and if I don't have to override any core file/s, even better.

Thanks so much for any help.

EDIT: Using PS version 1.7.5

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

If you read my initial post, I did not ask for anyone to teach me how to develop a module or anything. I simply asked where (semantically) can I place custom PHP code that can be accessible on all pages (cart, cats, cms, etc...). Then I said "I've tried to create a module, but getting stuck - abandoning this idea due to time crunch".

Thanks.

Link to comment
Share on other sites

Hi Simetria,

The best way is to develop a module. If you use the generator (https://validator.prestashop.com/generator) it should be rather easy - to have your code executed on every page you can add it to diplayHeader hook for example. If you want to use any output on the frontend then assign it to smarty and insert these in the theme template file.

Best regards

Link to comment
Share on other sites

Thank you JBW, I have tried to create a module via the generator, but when I do a simple test I get a 500 error.

For example, in the hookDisplayHeader function, if I write this:

public function hookDisplayHeader(){
  /* Place your code here. */
  Media::addJsDef('simVar' => 'Test.');
}

or instead this:

public function hookDisplayHeader(){
  /* Place your code here. */
  $this->context->smarty->assign('simVar' => 'Hello');
}

both examples above breaks the site and I get a 500 error. Am I missing something? 

Link to comment
Share on other sites

  • 1 year later...
On 2/25/2019 at 11:17 PM, tdsoft said:

Please summary your question for your problem because nobody can take much time to teach you how to develope a module

@tdsoft I don't see anybody putting a gun in your head and asking to teach someone to be a developer. If your are no interested in the topic, just close the window. Even more if you are from the buy-addons team, which don't comment their code and when you ask them for a modification the say its too hard to do..  How could you to teach someone if you can't do it by yourself???

  • Like 1
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...