• Resolved lancer85

    (@lancer85)


    Hello there,

    I can’t figure out how to set up a trigger to send an email if a user meta has been changed. I have tried to set up a trigger and it doesn’t seem to work as the documentation states.

    Here is the code snippet

    class TherapistSelected extends \BracketSpace\Notification\Abstracts\Trigger {
    
      public function __construct() {
    
          // Add slug and the title.
          parent::__construct(
              'therapistselected',
              __( 'Therapist Selected Message', 'therapistselected' )
          );
    
          // Hook to the action.
          $this->add_action( 'add_therapist', 10, 2 );
    
      }
    
      public function merge_tags() {}
    
    }
    
    register_trigger( new TherapistSelected() );

    Additionally, I added meta tags for PMPro fields and every time a user registration email is triggered, the fields don’t show up. Not sure what I am doing wrong here. Below is the code snippet

    add_action( 'notification/trigger/merge_tags', function( $trigger ) {
    
    	if ( $trigger->get_slug() !== 'user/registered' ) {
    		return;
    	}
    
    	$trigger->add_merge_tag( new BracketSpace\Notification\Defaults\MergeTag\User\UserNicename() );
    	$trigger->add_merge_tag( new BracketSpace\Notification\Defaults\MergeTag\User\UserDisplayName() );
    	$trigger->add_merge_tag( new BracketSpace\Notification\Defaults\MergeTag\User\UserFirstName() );
    	$trigger->add_merge_tag( new BracketSpace\Notification\Defaults\MergeTag\User\UserLastName() );
    	$trigger->add_merge_tag( new BracketSpace\Notification\Defaults\MergeTag\User\UserBio() );
    
    		$trigger->add_merge_tag( new BracketSpace\Notification\Defaults\MergeTag\StringTag( array(
    
    			
    			'slug'        => 'user_address1',
    			'name'        => __( 'User Street Address', 'textdomain' ),
    			'resolver'    => function( $trigger ) {
    					return get_user_meta( $trigger->user_object->ID, 'pmpro_baddress1', true );
    			},
    	) ) );
    }
Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Kuba Mikita

    (@kubitomakita)

    Hi,

    the custom trigger seems to be missing the action method. Plus there might be an issue with executing the add_therapist action.

    Adding the Merge Tags looks fine, but what do you mean by:

    the fields don’t show up

    You cannot see this merge tag in the sidebar while editing the notification or it doesn’t render any value?

    Thread Starter lancer85

    (@lancer85)

    That is correct. The merge tags show up in the side bar, but the value isn’t rendered on the email notification.

    Could you provide an example of how to trigger an email on a meta field change?

    My example is when a user chooses a therapist, I need to trigger an email to that therapist. I have two meta fields that change, one that changes from false to true “has_selected_therapist” and the other is “selected_therapist_id”

    I am trying to create this notification for a therapist to be alerted when a user has selected them

    Plugin Author Kuba Mikita

    (@kubitomakita)

    If the value doesn’t render, probably the field is saved after the Notification is processed. I suggest enabling Background Processing which may help.

    The “meta field” change trigger is not so easy and unfortunately, I don’t have any example of that trigger.

    The procedure would be:
    – hook to user profile updated action (this may depend on what are you using to add that field)
    – check the field value(s) and if they are satisfied trigger your custom action
    – build the trigger around that custom action

    Thread Starter lancer85

    (@lancer85)

    Ok, I will give that a try. If I continue to have an issue, I’ll let you know what issue I am facing and see if you can further assist. Thanks for your help thus far.

    Thread Starter lancer85

    (@lancer85)

    Hey Kuba,

    I was able to successfully get the trigger to happen on “update_user_meta” but the issue is that it fires each time the page reloads. I tried “profile_update” but according to WP docs this only fires when the user profile has been updated. Since this is user meta, the profile isn’t being updated, just the meta field.

    So after further investigation, I need to use an action hook that detects the change for the meta that is being changed from ‘add_therapist’ function.

    This is that function

    add_action( 'wp_ajax_nopriv_add_therapist', 'add_therapist' );
    add_action( 'wp_ajax_add_therapist', 'add_therapist' );
    
    function add_therapist(){
      $user_id = $_REQUEST['user'];
      $therapist_id = $_REQUEST['therapist'];
      update_user_meta($user_id, 'has_selected_therapist', 1);
      update_user_meta($user_id, 'selected_therapist_id', $therapist_id);
      update_user_meta($user_id, 'selected_therapist_status', 'Pending');
    
      echo 'Therapist has been selected';
      die();
    
    }

    So essentially, I need to get the trigger to fire when update_user_meta(‘has_selected_therapist’) has been changed (to the 1). Is there anything I hook to that would work with the trigger??

    Additionally, I am turned on background processing, and I am still not seeing the User First Name and Last Name rendering.

    Here is that code:

    add_action( 'notification/elements', function() {
    
    	class TherapistSelected extends \BracketSpace\Notification\Abstracts\Trigger {
    
    		public function __construct() {
    	
    				// Add slug and the title.
    				parent::__construct(
    						'therapistselected',
    						__( 'Therapist Selected Message', 'therapistselected' )
    				);
    	
    				// Hook to the action.
    				$this->add_action( 'update_user_meta', 'add_therapist' );
    	
    		}
    	
    		public function merge_tags() {
    			
    		$this->add_merge_tag( new BracketSpace\Notification\Defaults\MergeTag\User\UserFirstName() );
    		$this->add_merge_tag( new BracketSpace\Notification\Defaults\MergeTag\User\UserLastName() );
    		$this->add_merge_tag( new BracketSpace\Notification\Defaults\MergeTag\User\UserEmail() );
    		
    	}
    
    	
    	}
    
    	notification_register_trigger( new TherapistSelected() );
    } );

    Let me know if you see anything missing from this, or if there is a way to configure this better.

    Thanks!

    Thread Starter lancer85

    (@lancer85)

    Hey Kuba,

    I think I might have figured it out. For this one instance… still having issues with the merge tags, however, I was able to successfully trigger the user meta change by using the action hook “personal_options_update”. Which once the add_therapist AJAX call is complete the email is sent. I was able to go in and change the therapist and another email was sent. So, I am excited that worked! Just need to understand why I can’t get the merge tags not to render on the email side.

    Plugin Author Kuba Mikita

    (@kubitomakita)

    Awesome that you figured it out!

    For the merge tags – doesn’t the Background Processing help?

    Thread Starter lancer85

    (@lancer85)

    No, It’s now turned on but nothing renders to the email.

    cpvb86

    (@cpvb86)

    @lancer85 Are you willing to share the final code?
    I’m looking for this trigger but can’t figure out how to build it up/add it to my installation.

    Thread Starter lancer85

    (@lancer85)

    @cpvb86 Yes I can, however the trigger on “personal_options_update” didn’t quite trigger as I expected. There were some finicky processes that didn’t pan out as I expected. I will upload that code later today.

    Thanks!

    • This reply was modified 4 years ago by lancer85.
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Create Trigger Change Of User Meta Status And More’ is closed to new replies.