Jump to content

What's in a hook's $params argument?


chrisdac

Recommended Posts

Hi,

 

We're working on a new module, but when implementing its hooks we can't figure out what each hook's $params argument actually contains.

 

For example, we want to do some additional processing after creating of a new order, which involves iterating through that order's details and posting some of that info to an external API. We're using the actionValidateOrder hook to do this, as follows:

 

 

public function hookActionValidateOrder($params)

{

...some code in here

}

 

In this case, what does the $params object contain? Does it contain the same object in every hook, or is it different for each hook? Without knowing the contents, it's very hard to figure out how to access the order data.

 

We've tried searching through the documentation and forums for this but no luck so far - any help very much appreciated.

 

Thanks

Link to comment
Share on other sites

Saech your code for strings like

$params =

Here you find the definition (mostly something boring like

$params = array();

 

But a little below that, many times the array will be filled, like :

$params['total_to_pay'] = $cart->getOrderTotal();

$params['currency'] = $currency->sign;

$params['objOrder'] = $order;

$params['currencyObj'] = $currency;

 

etc.

 

also, you can try to search for:

$params[

 

(this one has many false matches, as sometimes they use the value, instead of assign, but it's a good try.

 

To start with, maybe have a look at the code of /classes/hook.php .

 

my 2 cents,

Pascal

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

Thanks Pascal - I appreciate the help.

 

I think this is one area where Prestashop desperately needs some documentation! A simple reference showing the contents of each hook's $params object would save so much time and make the platform much easier to develop for.

 

Thanks again

  • Like 1
Link to comment
Share on other sites

Thanks Pascal - I appreciate the help.

 

I think this is one area where Prestashop desperately needs some documentation! A simple reference showing the contents of each hook's $params object would save so much time and make the platform much easier to develop for.

 

Thanks again

 

I totally agree.

 

In an action hook we can't do a var_dump.

But in other hooks you can var_dump($params) and see everything. That's how i usually do it, even if the page gets weird, and sometimes it shows the params on top of the page.

 

But, yes we need more documentation on that, and other things (like a glossary of functions for quick reference, instead of having to go through the files...). But things are improving...

 

Pedro

Link to comment
Share on other sites

the easiest way is to just search the classes/controller folder and find the hook you are trying to use, which is actionValidateOrder

 

It actually takes the same/less time to just search the code, then it would to search through documentation online. It took all of 5 seconds to find the parameters passed in

 

              	 // Hook validate order
                   Hook::exec('actionValidateOrder', array(
                       'cart' => $this->context->cart,
                       'order' => $order,
                       'customer' => $this->context->customer,
                       'currency' => $this->context->currency,
                       'orderStatus' => $order_status
                   ));

  • Like 2
Link to comment
Share on other sites

I understand what you're saying, but the problem is that the existing documentation doesn't have any information at all, not even a line of text telling us where to start looking for more info.

 

I'm clearly not the only one who has got stuck here, so it's not unreasonable to suggest that a small amount of additional documentation from Prestashop could save hours of searching by people who are trying to use the system. I'd also disagree that it takes less time to search code than it does to search well structured official docs - this is not the way that a lot of people tend to start looking for help, even though it does make sense.

 

Thanks for your reply.

  • Like 4
Link to comment
Share on other sites

  • 6 months later...

Prestashop have a customized php function for die() and print_r(), it became d() and p()
Therefore you can easily check and know what variables inside $params object
You can use d($params) or p($params) and you'll see all variables inside $params object
e.g

public function hookWhatever($params)
{
      d($params);
}
Edited by gonebdg - webindoshop.com (see edit history)
  • Like 2
  • Confused 1
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...