Jump to content

Getting The Event Details That Has Been Triggered


Recommended Posts

Posted

I'm trying to create something so that when a transaction is created I trigger another function, which is working, but I'm having trouble understanding how to get the event details of the transaction.

 

For example, in my plugin handler file I have this:

public function getEvents() {
	return array(
		array(
			'event' => "Transactions.add",
			'callback' => array("this", "run")
		)
	);
}

public function run($event) {
	
	ob_start();
	var_dump($event);
	$content = ob_end_clean();
	
	file_put_contents(dirname(__FILE__) . '/output.txt', $content);
	
}	

All I get in the contents of output.txt is:

1

How can I get the event details such as how much they paid etc?

Posted

If you look at the EventObject class, you should be able to call the following methods.

$event->getName();
$event->getParams();
$event->getReturnVal();

A useful thing to note is that you can use

print_r($event, true);

and it will return the data instead of printing it.

Posted

If you look at the EventObject class, you should be able to call the following methods.

$event->getName();
$event->getParams();
$event->getReturnVal();

A useful thing to note is that you can use

print_r($event, true);

and it will return the data instead of printing it.

 

Hmmmmmm....... why doesn't the var_dump() output the object data then?

 

Anyway....... I tried those and it didn't give me much information; $event->getParams() just returned an ID, which I assume is the id of the transaction?

Posted

The EventObject vars that store the info are protected so that might be var_dump() doesn't output them.

 

Yes, if you look at where the transaction event is registered and triggered you can see it is the transaction id (seems there is actually a transaction.transaction_id and transaction.id, this is the former.

$this->Events->register("Transactions.add", array("EventsTransactionsCallback", "add"));
$this->Events->trigger(new EventObject("Transactions.add", compact("transaction_id")));
$this->Events->register("Transactions.edit", array("EventsTransactionsCallback", "edit"));
$this->Events->trigger(new EventObject("Transactions.edit", compact("transaction_id")));

You can get the actual transaction like this

if (!isset($this->Transactions))
    Loader::loadModels($this, array("Transactions"));
$this->Transactions->getByTransactionId($transaction_id);
Posted

 

The EventObject vars that store the info are protected so that might be var_dump() doesn't output them.

 

Yes, if you look at where the transaction event is registered and triggered you can see it is the transaction id (seems there is actually a transaction.transaction_id and transaction.id, this is the former.

$this->Events->register("Transactions.add", array("EventsTransactionsCallback", "add"));
$this->Events->trigger(new EventObject("Transactions.add", compact("transaction_id")));
$this->Events->register("Transactions.edit", array("EventsTransactionsCallback", "edit"));
$this->Events->trigger(new EventObject("Transactions.edit", compact("transaction_id")));

You can get the actual transaction like this

if (!isset($this->Transactions))
    Loader::loadModels($this, array("Transactions"));
$this->Transactions->getByTransactionId($transaction_id);

 

Awesome - thank you!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...