Jump to content

Add parameters to an 'addHookMethod'?


a-ok
 Share

Recommended Posts

I've added a page hook method (addHookMethod) in ready.php and I am calling it on a page. Simple enough. See below for the current code.

I now need to pass parameters into the hook but can't figure out how this is possible?

So with the current usage here:

$product = $page->shopifyGetProductById();

I'd like to pass some parameters into the hook to then use within the hook function itself:

$product = $page->shopifyGetProductById($data);
wire()->addHookMethod('Page::shopifyGetProductById', function($event) {
	$page = $event->object;

	$gid = "gid://shopify/Product/{$page->shopifyProductId}";
	$query = <<<GQL
	query {
	  product(id: "$gid") {
		title
	  }
	}
	GQL;

	$result = wire('cache')->get("shopifyGetProductById{$page->shopifyProductId}");
	if (!$result) {
		$result = shopifyGQL($query);
		wire('cache')->save("shopifyGetProductById{$page->shopifyProductId}", $result, "template=shopifyProduct, shopifyProductId={$page->shopifyProductId}");
	}
	$event->return = $result;
});

 

Link to comment
Share on other sites

5 hours ago, elabx said:

This works??


$data = $event->arguments(0);

 

This does work! I'm sorry for the clearly amateur question! Thanks!

  • Like 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
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...