Jump to content

[Solved]What paramaters does the standard hooks take?


User1J

Recommended Posts

What paramaters does the hooks take?

 

I´ll give you a simplified problem to illustrate my problem...

 

Lets say every time hookLeftColumn runs, I want to update the quantity in ps_product by 3. So...

 

function hookLeftColumn($params)

SQL to update ps_product here.

}

 

But now the quantity is updated, but everything relying on hookUpdateQuantity() fails since I "cheated" and did it via SQL right? So I try to do

function hookLeftColumn($params)

SQL to update ps_product here.

Module::hookExec('addProduct', array('product' => $product));

}

 

Which is fine, except I have no clue what to pass into Module::hookExec(name, $var). So what is $var? Obviously and object, obviously containing values gotten from the DB, but what exactly?

 

So to sum up, what EXACTLY is given in $params when a hook is executed, and what should I pass if I want to call a hook?

 

Thanks a lot for reading, I hope I got my message across without to much confusion/rambling. :)

Link to comment
Share on other sites

The hook parameters vary depending on the hook unfortunately, so there are no "standards" across them all. I'm a little confused by your example though as you appear to be calling a hook from within a hook which is a little unusual.

 

To update the quantity of the product you would normally instantiate the product, change the value and then "update" it again.

 

e.g. (greatly simplified with no error checking):

 

$product = new Product($product_id_of_product_you_want_to_change);
$product->quantity = $product->quantity + 3;
$product->update();

 

Obviously you need to have a valid product_id to do this. I'm puzzled why you would choose the left column hook as this is on every page many of which do not relate to a specific product.

 

Maybe if you post what you're trying to do we could give more specific help on how to implement it? :)

 

Paul

Link to comment
Share on other sites

Hi and thanks for the reply!

 

Let me see if I can explain this a bit clearer...

 

Lets say I want to send an email to myself every time the quantity of a product changes. That would be:

 

function hookUpdateQuantity(){

//email stuff here

}

 

Now, every time I change the quantity of a product from the back office in PS I will get an email.

 

However, if I update the quantity via:

 

$product = new Product($product_id_of_product_you_want_to_change);

$product->quantity = $product->quantity + 3;

$product->update();

 

I will get no email, because hookUpdateQuantity will not execute. Also, other modules unknown to me, will not get to do their stuff which relies on hookUpdateQuantity.

 

Hopefully this makes more sense to you. :)

 

//J.

Link to comment
Share on other sites

I think I understand.

 

Caveat: Normally however you would never call Module::hookExec() in your own code as these have been placed in the code to be triggered under certain specific circumstances. In fact calling them manually may cause duplication. This only applies to the hooks that are defined in the core of course; If you create your own hook, then you have complete control over the circumstances that trigger it.

 

Getting back to the original question though, as I said before, what you pass in the Module::hookExec function depends (somewhat) on the implementation of the hook. Using the example above you could omit the second parameter completely e.g.

 

Module::hookExec('updateQuantity');

 

The above would then assume an empty array for the second parameter.

 

In practice however (and this is historical so could change in the future) module hook functions always get some mandatory parameters passed to them (if they are declared to accept them). Within the implementation of the hookExec function in the Module class the second parameter gets the indices 'cart' and 'cookie' added to it (or if it is empty it will only contain those elements). Both of these are populated by the global 'cart' and 'cookie' variables respectively.

 

This means that if you could have declared your hook function as:

 

function hookUpdateQuantity($params){

$cookie = $params['cookie'];
//email stuff here using $cookie->id_lang for internationalisation

}

 

Unless those two variables are the only ones your hook function needs though this isn't going to be much help. If you're interested in a core hook function (but see caveat above)then you will need to look at either an implementation in a module, or at what is passed as the second parameter to determine what they are designed to accept. If you wanted to create the "updateQuantity" hook yourself you would have more control and could decide that $params needs to contain a product id and a quantity. Your function (in your module, and any other modules that rely on this) would then look something like:

 

function hookUpdateQuantity($params){

$cookie = $params['cookie'];
$product = new Product($params['product_id']);
$new_quantity = $params['quantity'];

//email stuff here using $cookie->id_lang for internationalisation
//Now we have acces to the product details and the new quantity

}

 

At the point this needs to be triggered you would then need:

 

$args['product_id'] = $variable_containing_product_id;
$args['quantity'] = $variable_containing_new_quantity_for_this_product;

Module::hookExec('updateQuantity', $args);

 

 

Does this make sense?

 

Paul

Link to comment
Share on other sites

It didnt all make sense, but I learned a lot, thank you for taking the time to write that up, it's much appreciated.

 

 

If some one is interested in updating the quantity in ps_product, I found a way that does call the relevant hooks and also logs it in ps_stock_mvt.

 

$product = new Product($idOfProduct);

addStockMvt($quantity, $id_reason, $id_product_attribute = NULL, $id_order = NULL, $id_employee = NULL);

 

(will mark this as solved as soon as I figure out how to do that)

Link to comment
Share on other sites

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