Prestashop: hookActionProductAdd does not trigger

Created on 28 Aug 2020  路  7Comments  路  Source: PrestaShop/PrestaShop

Describe the bug

I'm trying to register hook actionProductAdd. In BO it shows that my module is transplanted in hook, but when I create a product, it does not trigger.

Expected behavior

When I create a product, I should get a log that says 'hookActionProductAdd()' which indicates that my hook has been triggered.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Create a module
  2. Register hook 'actionProductAdd'
  3. Add a log into hook:
public function hookActionProductAdd($params) {
    Logger::addLog('hookActionProductAdd()', 1, null, null,  null, true);
}
  1. Create product
  2. Check logs - log does not show

Additional information

  • PrestaShop version: 1.7.6.5
  • PHP version: 7.2
1.7.6.5 Bug CO Products waiting for dev

Most helpful comment

Hi @iibs

I tried this hook with the https://github.com/PrestaShop/ps_qualityassurance module, and indeed I don't see the actionProductAdd triggered, it does work with actionProductUpdate however.

I made some digging into this turns out actionProductAdd is not a generic hook used by all ObjectModels, it's a custom one and the only places I was able to find it were when we duplicate a product (both in legacy controller and migrated controller). So maybe the hook is not very well named but anyway that explains why it's not triggered

However you could try one of this two generic hooks

  • actionObjectProductAddBefore
  • actionObjectProductAddAfter

I tested both of them and they work as expected.

However keep in mind that the product page has a special behaviour, when you click on "New Product" a "ghost" product is automatically and instantaneously created, however it is marked with a temporary state, it is only when you save the product the first time that the product will stop being a ghost/temporary product and will appear in the catalog (wether it's active or not).

So I don't know your use case, but it might not be the product addition hook that you will need, because when products are created from the BackOffice they are always empty shells. Although this might be more useful if it's added by an import, an external module or an API.

TLDR

The hook is triggered when called, it's just the name is confusing so it's not called when you would expect to, it is only called when a product is duplicated. So I suggest to:

  • update the documentation to note this behaviour
  • update hook descriptions in the core
  • deprecated this hook, replace it with actionProductDuplicate and create an alias to the original actionProductAdd

If this explanation is satisfactory for you we can either close this issue and open a new one to create the actionProductDuplicate or we can update this issue's description and purpose to manage this change.

All 7 comments

Thanks for opening this issue! We will help you to keep its state consistent

Hi @iibs,

I tried with ps_featuredproduct module
image
I tried to edit this line
From
$this->_clearCache('*');
To
$this->_clearCache('*')

When I try to save a product, I have this exception
image

Thanks to check and feedback.

I managed to get same error. I also tried to remove ; from my log, and then create product page crashed even before it rendered. However, I can't still get my log to show.

I also tried renaming my hook to hookAddProduct(), like in ps_featuredproduct, and replanting the module into hook, but it still does not log.

Ping @PrestaShop/prestashop-core-developers what do you think?

Thanks!

Hi @iibs

I tried this hook with the https://github.com/PrestaShop/ps_qualityassurance module, and indeed I don't see the actionProductAdd triggered, it does work with actionProductUpdate however.

I made some digging into this turns out actionProductAdd is not a generic hook used by all ObjectModels, it's a custom one and the only places I was able to find it were when we duplicate a product (both in legacy controller and migrated controller). So maybe the hook is not very well named but anyway that explains why it's not triggered

However you could try one of this two generic hooks

  • actionObjectProductAddBefore
  • actionObjectProductAddAfter

I tested both of them and they work as expected.

However keep in mind that the product page has a special behaviour, when you click on "New Product" a "ghost" product is automatically and instantaneously created, however it is marked with a temporary state, it is only when you save the product the first time that the product will stop being a ghost/temporary product and will appear in the catalog (wether it's active or not).

So I don't know your use case, but it might not be the product addition hook that you will need, because when products are created from the BackOffice they are always empty shells. Although this might be more useful if it's added by an import, an external module or an API.

TLDR

The hook is triggered when called, it's just the name is confusing so it's not called when you would expect to, it is only called when a product is duplicated. So I suggest to:

  • update the documentation to note this behaviour
  • update hook descriptions in the core
  • deprecated this hook, replace it with actionProductDuplicate and create an alias to the original actionProductAdd

If this explanation is satisfactory for you we can either close this issue and open a new one to create the actionProductDuplicate or we can update this issue's description and purpose to manage this change.

By the way I forgot to mention when you duplicate the product (there's a button in the product page), you do see the log :)

Hi @jolelievre

So I don't know your use case, but it might not be the product addition hook that you will need

This is what I want to achieve:

  1. Create product pack
  2. Add items into pack, additional info etc.
  3. Save product
  4. Trigger hook to count product pack quantity based on products included in pack

I have already hooked into actionProductUpdate, but that does not trigger when product is saved for the first time. That's why I tried to explore different add hooks.

When I try to transplant hookActionObjectProductAddAfter, it's not shown on hook list, even though I have implemented it in code. I would prefer not to reset module, because I have additional tables and columns that include tons of information I don't want to lose.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matks picture matks  路  3Comments

khouloudbelguith picture khouloudbelguith  路  3Comments

marionf picture marionf  路  3Comments

matks picture matks  路  3Comments

nrcjea001 picture nrcjea001  路  3Comments