Jump to content

how to execute some code after a post(product) is created/updated in prestashop


sinai

Recommended Posts

Hi all,

 I want to know how can I execute some codes after a product is created/updated in prestashop.

I have done such things for wordpress via the functions.php file inside a theme. I want to know whether there is a similar way in prestashop or not?

I want to purge the cache server after a product is updated/created.

Thanks in advanced.

Link to comment
Share on other sites

You can use action hooks, actionProductAdd and actionProductUpdate
You need this hooks on the module install and add function for each hook

Hook::exec are called on AdminProductController.php

Hook::exec('actionProductAdd', array('id_product' => (int)$this->object->id, 'product' => $this->object));
public function HookActionProductUpdate($params){
Var_dump($params['product']);exit;
}

Cordially.

Link to comment
Share on other sites

2 hours ago, tarek.fellah said:

You can use action hooks, actionProductAdd and actionProductUpdate
You need this hooks on the module install and add function for each hook

Hook::exec are called on AdminProductController.php


Hook::exec('actionProductAdd', array('id_product' => (int)$this->object->id, 'product' => $this->object));
public function HookActionProductUpdate($params){
Var_dump($params['product']);exit;
}

Cordially.

Thank you tarek.fellah.

I found the AdminProductsController.php which is located in the path /home/username/public_html/controllers/admin.

I want to know whether this file is updated or not if someone updates its prestashop or change its theme? Because if it is updated whenever presta release a new version or by changing a theme, all my codes will disappear!

Thank you.

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

4 hours ago, tarek.fellah said:

You can do like this on your module 


public function HookActionProductUpdate($params){
Var_dump($params['product']);exit;
}

This is what i do and it's working.
I don't think Prestashop will change hook names.

I am a little confused! because I am not an expert PHP developer. I just want to execute some simple php codes. May you please tell me where should I add the function and the hook?

I added a function at the end of the AdminProductsController.php file, and call it just after the line

Hook::exec('actionProductAdd', array('id_product' => (int)$this->object->id, 'product' => $this->object));

in function processAdd()

the function is as follows:

public function update_test()
    {
      file_put_contents("/home/username/public_html/controllers/admin/test.txt","OK");
      return ;
    }

but I get the following error:

Got error 'PHP message: PHP Fatal error:  Call to undefined function update_test() in /home/smilehyper/public_html/controllers/admin/AdminProductsController.php on line 1889

Link to comment
Share on other sites

  • 2 years later...
  • 3 months later...

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...