Jump to content

PrestaShop custom action-hook for Front Office


alex_u

Recommended Posts

Hi, I was searching for answer how to create new custom action-hook and didn't find how to implement it properly. So the main function of it is when customer visits product the module should save product's id into custom module's table in database and send through API to another database with some additional information. I'm using latest version of PrestaShop.

 

Already added function for it in module 

hookActionMyModuleCustomerVisitProduct($params)

with basic sql-based operations code in it and registered it on install() section

if (!$this->installDb() || !$this->installTab() || !parent::install() || !$this->registerHook('displayBackOfficeHeader') || !$this->registerHook('actionMyModuleCustomerVisitProduct')) {
  return false;
}

Hook is in database too.. and can I see it on Positions menu. But when I trying to visit some products as a customer nothing happens

 

Maybe I am missing something here.. 

Link to comment
Share on other sites

I think you've forgotten to call your hook. You could do this by overriding ProductController.php and adding code like the following:

<?php

class ProductController extends ProductControllerCore
{
    public function init()
    {
        parent::init();

        if (isset($this->context->customer) && (int)$this->context->customer->id > 0) {
            Hook::exec('actionMyModuleCustomerVisitProduct', array(
                'id_customer' => (int)$this->context->customer->id,
                'id_product' => (int)Tools::getValue('id_product'))
            );
        }
    }
}

This should call your hook and then allow you to use $params['id_customer'] and $params['id_product'] in your function.

Link to comment
Share on other sites

if customer will not be logged it will spawn some php warning probably. I mean that this will not exist: $this->context->customer->id, so in effect php warn will occur

am i right? or if we cast variable as on the example - with (int) - warn will be muted?

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