Jump to content

Executing display hook after an action hook


Recommended Posts

Hello, is there a way to execute a custom hook after an action hook happens? I tried Hook::Exec('myhook') but didnt seem to work.

I'm currently building a  module that would allow me to track what users do in a prestashop website, for now i need to execute a script which will send datas to my server after a user is registered to the site or when a user add an item to his cart. both these are action hooks and i have no idea how to activate my script on these actions

Link to comment
Share on other sites

Hello, is there a way to execute a custom hook after an action hook happens? I tried Hook::Exec('myhook') but didnt seem to work.

I'm currently building a  module that would allow me to track what users do in a prestashop website, for now i need to execute a script which will send datas to my server after a user is registered to the site or when a user add an item to his cart. both these are action hooks and i have no idea how to activate my script on these actions

HI...

 

First you need register hook with your module install method. like as

 

public function install()
    {
        return parent::install() && $this->registerHook('header') && $this->registerHook('backOfficeHeader') && $this->registerHook('actionAuthentication') && $this->registerHook('actionCartSave');
    }

Then After you both hook in module file.like as

 

public function hookActionAuthentication()
    {
        /* Place your code here. */
    }


public function hookActionCartSave()
    {
        /* Place your code here. */
    }

Thanks

Link to comment
Share on other sites

Hello, thanks for your reply.

I already hooked those two hook but d they are action hooks and i need display hooks that does the same thing, and to my understunding they dont exist, so i created custom hooks and tried to execute them inside  the CartSave and Authentication but it did not work, is there a way to do what i'm trying to do? 

Link to comment
Share on other sites

  • 1 year later...

Hi,

 

You have to create the Hook as Nishith said.

Then you can use your action whenever you want is your files.

 

Example in Cart.php.

Hook::exec('actionCartSave');

It execute the function inside the hook actionCartSave after you add or midify the cart.

 

In the Hook.php you can find this function with these parameters:

 /**
     * Execute modules for specified hook
     *
     * @param string $hook_name Hook Name
     * @param array $hook_args Parameters for the functions
     * @param int $id_module Execute hook for this module only
     * @param bool $array_return If specified, module output will be set by name in an array
     * @param bool $check_exceptions Check permission exceptions
     * @param bool $use_push Force change to be refreshed on Dashboard widgets
     * @param int $id_shop If specified, hook will be execute the shop with this ID
     * @param bool $chain If specified, hook will chain the return of hook module
     *
     * @throws PrestaShopException
     *
     * @return string/array modules output
     */
    public static function exec(
        $hook_name,
        $hook_args = array(),
        $id_module = null,
        $array_return = false,
        $check_exceptions = true,
        $use_push = false,
        $id_shop = null,
        $chain = false
    )

Thanks to that you can execute your hook when you want.

 

For example in your module myModule.php I have a hook actionSendResForm.

public function hookActionSendResForm($params)
    {      
        
         Db::getInstance()->insert('reservation', array(
                'id_customer'       => $params['id_customer'],
                'id_product'        => $params['id_product'],
                'contact_number'    => $params['contact_number'],
                'date_expire'       => $params['date_expire'],
                'product_color'     => $params['product_color'],
                'product_size'      => $params['product_size']
            ));
    }

 

This Hook is called in my ajax.php but it could be on any of your action (after a click on a button for example).

$date_expire = date('Y-m-d', strtotime(date('Y-m-d').' +'. Configuration::get('GP_RES_DAYS') . ' days'));
            
Hook::exec('actionSendResForm', array(
       'id_customer'       => (int)Context::getContext()->customer->id,
       'id_product'        => (int)Tools::getValue('id_product'),
       'contact_number'    => pSQL(Tools::getValue('contact_number')),
       'date_expire'       => pSQL($date_expire),
       'product_color'     => (int)Tools::getValue('color'),
       'product_size'      => (int)Tools::getValue('size')
));

 

Hope this can help you, 

 

Bruno-

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