Author Topic: Gaining access to hooked up method's parameters  (Read 820 times)

Offline renato.aloi

  • Newbie
  • *
  • Posts: 11
  • Karma: +4/-0
    • View Profile
Gaining access to hooked up method's parameters
« on: April 06, 2017, 10:03:10 AM »
Hi there!

I am trying to create a Hook for update method of ModelCheckoutOrder class.

$this->extensions->hk_update($this, $order_id, $order_status_id, $comment, $notify);

I have learned I must do as follows:

public function beforeModelCheckoutOrder_update() { }

I need now gain access to $order_id parameter inside my hook function. How can I do it?

Thanks in advance!

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1747
  • Karma: +238/-10
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Gaining access to hooked up method's parameters
« Reply #1 on: April 06, 2017, 10:42:56 AM »
usually all controllers and models have public property $this->data.
You can access to it from your hook via
Code: [Select]
$this->baseObject->data
“No one is useless in this world who lightens the burdens of another.”
― Charles Dickens

Offline renato.aloi

  • Newbie
  • *
  • Posts: 11
  • Karma: +4/-0
    • View Profile
Re: Gaining access to hooked up method's parameters
« Reply #2 on: April 06, 2017, 12:51:50 PM »
Thank you for the quick reply, Dmitriy!

Unfortunely that controller in question does not make use of data public member.

I found a way to solve the problem. The hook declaration must be like this in order to work properly:

public function beforeModelCheckoutOrder_update($order_id, $order_status_id, $comment, $notify)
{
  $that = $this->baseObject;
  $method = $this->baseObject_method;

  echo print_r($order_id, true) . "<br/>";
  echo print_r($order_status_id, true) . "<br/>";
  echo print_r($comment, true) . "<br/>";
  echo print_r($notify, true) . "<br/>";
  // ...
}

I realized I was declaring the hook wrong with the first parameter $this. Then I saw this example at docs:

class ExtensionName extends Extension {
               public function afterClassNameMethodName($param1, $param2) {
      // hook $this->performSomeAction()
      $this->hkPerformSomeAction();
   }
   public function performSomeAction() {
      // ...
   }
}

And I realized the parameter $this should be skipped...

Now is working as expected.

Thank you!