Jump to content

Moving a hook function to a function called within 2 hooks


a-ok
 Share

Recommended Posts

I had a hook method set up to do a few things, hooked to a lazycron of every 30 minutes:

wire()->addHook('LazyCron::every30Minutes', function($event) {
	// functions etc
}

This was working fine but I needed to call the content of the function on a specific page save also so I made it into a public function and set up two hooks for it... one on the lazycron and one on page.

function shopifyInit(HookEvent $event) {
	// functions etc
}
wire()->addHook('LazyCron::every30Minutes', null, 'shopifyInit');
wire()->addHookAfter('Pages::save', function($event) {
	$page = $event->arguments(0);
	if ($page->template->name == 'shop') { // Shop
		shopifyInit();
		$page->message('Shopify cache has been cleared for all products');
	}
});

However on save I am getting this error...

ArgumentCountError Too few arguments to function ProcessWire\shopifyInit(), 0 passed and exactly 1 expected

Any thoughts where I'm going wrong?

Link to comment
Share on other sites

33 minutes ago, a-ok said:

Any thoughts where I'm going wrong?

It says it right there in the error ?,

33 minutes ago, a-ok said:

ArgumentCountError Too few arguments to function ProcessWire\shopifyInit(), 0 passed and exactly 1 expected

Your function shopifyInit() expects a single argument/parameter but  here:

33 minutes ago, a-ok said:

if ($page->template->name == 'shop') { // Shop shopifyInit(); $page->message('Shopify cache has been cleared for all products'); }

you are not passing it the $event.

 

edit: I'm too slow; what @flydev said ?.

Edited by kongondo
  • Haha 1
Link to comment
Share on other sites

Thanks folks.

I tried this but received an error:

Using $this when not in object context

function shopifyInit(HookEvent $event) {
83:    
84:        $collections = $this->shopifyHttp->getJSON($this->shopifyBase . "custom_collections.json");

I'm using $this to grab my API keys etc outside of the scope of the hook

$this->shopifyHttp = new WireHttp();
$this->shopifyAPIKey = blahblah

Using $this works fine when using it directly in a hook but not within a function that's called within a hook?

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...