Jump to content
MarcoPLY

Add a hook for user's registration

Recommended Posts

I'm trying to add an action in the moment a new user registers and his page is created. Specifically, when a user is registered with the Facabook module, send the notification to Mailchimp and add it to the list, this with the mailchimp module.

I think the problem could be in how I set the hook and in the page data.

 

I hope I didn't make too many mistakes! sorry for this.

wire()->addHook('Pages::saved', function($page) {

	if($page->template == "user"){

		$user = $page;

		if ($user->hasRole('login-facebook')) {
			$mc = $modules->get("SubscribeToMailchimp");
			$email = $user->email;
			$subscriber = [
				'FNAME' => $user->pad_firstname,
				'MMERGE4' => $user->$country_title,
			];
			$mc->subscribe($email, $subscriber);
		}
	}

});

I try also to use LoginFacebook::createNewUser but I'm not sure how use it. For this I move to do an hook directly when a user page is created. (that also coulde help me for others projects) .

 

Share this post


Link to post
Share on other sites

Thank you @flydev.

I try but there are some point that sure I do not understand. I'm reading the hook post. But probably I still make more then one error and this make hard debug the hook.

wire()->addHookAfter('Pages::saved', function($event) {  

	// Use wire() because the function is outside of a class. It's in my ready.php
	// Use addHookAfter becasue I need to have all the fields populated before I can use them.

  $page = $event->arguments(0);  	// here set to have the access to all the data in the page (rigth?)

	if($page->template == "user"){ // if the page saved it's a user page 

		if ($user->hasRole('login-facebook')) { // (I'm not sure if I have to use $user or $page->fields)

			$mc = $modules->get("SubscribeToMailchimp");  // call the module take the fields and send it 
			$email = $page->email;
			$subscriber = [
				'FNAME' => $page->pad_firstname,
				'MMERGE4' => $page->$country_title,
			];
			$mc->subscribe($email, $subscriber);
		}
	}

});

 

UPDATE

I keep try to find a way to do the hook, but not with great result. I have also try to use Users::saveReady.

wire()->addHookAfter('Users::saveReady', function(HookEvent $event) {

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

	if($user->hasRole('login-facebook')){

			$mc = wire('modules')->get("SubscribeToMailchimp");
			$email = $this->wire('user')->email;
			$subscriber = [
				'FNAME' => $this->wire('user')->pad_firstname,
			];
			$mc->subscribe($email, $subscriber);

	}

});

 I think to have a bit of confiution about the structure to how make a hook.

Share this post


Link to post
Share on other sites

Sorry @flydev, I think to have read all the documentation and forum post. I have trie all the code that I thinked was correct, using the method Pages::saved, Users::saveReady, saveReady.  Try trying to start the hook with $this->addHookAfter, $pages->addHookAfter, wire()->addHookAfter but no one worked.
I also thought the problem was how I was trying to access the data so I try $email = wire('input')->post->email,  $email = $page->email, 
$email = $this->wire('user')->email. 

Could you tell me pleace where I wrong? 

This should not work?

$this->addHookAfter('Pages::saveReady', function($event) {

	$page = $event->arguments[0];

	if ($page->template == "user") {

		$mc = wire('modules')->get("SubscribeToMailchimp");
		$email = $this->wire('user')->email;
		$subscriber = [
			'FNAME' => $this->wire('user')->pad_firstname,
		];
		$mc->subscribe($email, $subscriber);
	}
});

All the method that I used I think are correct. So, my error need is in how I call the hook and/or how I access the data. The file is in my ready.php so like the documentation I have to use $this-> but not work. In the documentation about hook I read to use wire() if the hook it's ouside the class like it's the module. I try to follow your exemple here and write the hook like you did and call teh module using wire().

So, sure there are something that I do not understand. :( 

Share this post


Link to post
Share on other sites

Sorry @MarcoPLY I didn't saw your last ping!

 

This is the correct way to call the hook, put it in ready.php as you did then, go to Modules > Refresh if you still can't see the hook.

Code :

wire()->addHookAfter('Pages::saved', function(HookEvent $event) {
    // Get the object the event occurred on, if needed
    $pages = $event->object;

    // Get values of arguments sent to hook (if needed)
    $page = $event->arguments(0); // your code: $page = $event->arguments[0];
    bd($page); // tracy debug: User page object

    if ($page->template == "user") {

        $mc = wire('modules')->get("SubscribeToMailchimp");
        $email = wire('user')->email;
        $subscriber = [
            'FNAME' => wire('user')->pad_firstname,
        ];
        $mc->subscribe($email, $subscriber);

        bd($mc); // tracy debug: MailChimp object
    }
});

 

749645081_Capturedecran2018-11-19a19_43_05.png.b7c0af313aa1c1a6cb407cc2779f83f1.png

  • Like 2

Share this post


Link to post
Share on other sites

Thanks a lot @flydev

Ok, so, I have to use wire() becasue the hook is ouside the class. and I need also call $pages = $event->object.  But aren't the data store in the argument? why I have to call pages object? 

Unfortunately the hook does not work. I think now the problem is how access to the correct date to send to Mailchimp.

The Dumps I see empty, but in the Logs page now I have the Login-Facebook and I can read: Created new user: name-surname   then Updated user 'name-surname' fields: role:login-register, pad_firstname, pad_firstname, pad_lastname   and then Used Facebook to login for user: name-surname  There isn't the email, could this be the problem that does not generate the user in Mailchimp? But, on the user page the email has been added successfully, for this type of hook only need this, correct? I can't find anywhere bd($mc). I try also to use $email = $page->email;  and  $email = $this->wire('user')->email but  but not work, also try  $email = wire('input')->post->email I know the input post it's use only in the form but I just to try. 
Any Idea? 

 

 

Share this post


Link to post
Share on other sites

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.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...