• Resolved codac

    (@codac)


    Hi guys,
    I have written a function (child-theme; functions.php) for the plugin Event-Manager in WordPress. This function reads values out of custom input fields and saves those in the databse.

    I would like to write a custom Placeholder in order to place it in the email notification.

    The function is:

    function em_save_custom_event_fields() {
        global $EM_Booking;
        if(!empty($_REQUEST['user_motorcycle'])) {
            $EM_Booking->booking_meta['registration']['user_motorcycle'] = wp_kses($_Request['user_motorcycle'], array());
        }
    }
    add_filter('em_booking_add, em_save_custom_event_fields');

    For the email notification I would like to add a new placeholder called #_USERMOTORCYCLE in the template that actually replaces the placeholder with the value that was provided by the user and stored at:

    $EM_Booking->booking_meta['registration']['user_motorcycle']

    I have searched the web and found some samples but as a novice I am not able to apply them to my requirements.

    Create a Custom Placeholder for Event Formatting

    and

    Show specific database value in notification email

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • timrv

    (@timrv)

    Hello,

    It seems like you’re not using the correct filters? Your code uses:
    add_filter('em_booking_add, em_save_custom_event_fields');
    while our documentation is:
    add_filter('em_event_output_placeholder','my_em_styles_placeholders',1,3);

    I would suggest re-checking our documentation here: https://wp-events-plugin.com/tutorials/create-a-custom-placeholder-for-event-formatting/

    The best way to start is you could copy the function on our tutorial and changing them slowy until you get what you need.

    Thread Starter codac

    (@codac)

    To be honest it’s quite hard to follow the tutorial.
    Currently the placeholder gets replaced (at least one step further), but no result is being found.

    The data is stored in em_bookings -> booking_meta and has the following content:

    a:2:{s:7:"consent";b:1;s:12:"registration";a:10:{s:15:"user_motorcycle";s:3:"bmw";}}

    I need the value “bmw” of the field(name) “user_motorcycle”.

    In the functions.php I fill a new $EM_Bookings object “custom_fields”:

    function em_custom_fields_load($EM_Booking){
        global $wpdb;
        $sql = $wpdb->prepare("SELECT booking_meta FROM ".mct_em_bookings." WHERE booking_id=%s", $EM_Booking->booking_id);
        $EM_Booking->custom_fields = $wpdb->get_col($sql, 0);
    }
    add_action('em_event','em_custom_fields_load',1,1);

    In another function I try to get this value and replace the placeholder with it:

    function em_custom_form_fields_email_placeholders($replace, $EM_Booking, $result) {
    	if($result == '#_USERMOTORCYCLE'){
    		$replace = 'none';
            if(count($EM_Booking->custom_fields) > 0){
                $user_motorcycle = (is_array(get_option('user_motorcycle'))) ? get_option('user_motorcycle'):array();
                $motorcycles = array();
                foreach($EM_Booking->custom_fields as $id){
                    if(!empty($user_motorcycle[$id])){
                        $motorcycles[] = $user_motorcycle[$id];
                    }
                }
                $replace = implode(', ', $motorcycles);
            }
    	}
    	return $replace;
    }
    add_filter('em_event_output_placeholder','em_custom_form_fields_email_placeholders',1,3);

    The result in the email is: “none”.

    timrv

    (@timrv)

    Hello,

    Currently We’re very limited in terms of custom coding. https://eventsmanagerpro.com/support-policy however, We’ll try to help you as much as We could.

    If you’re placeholder #_USERMOTORCYCLE is always displaying none. Then that means the code is not proceeding with in the if/else argument you have.

    Best to troubleshoot is to use var_dump or print_r to get the data that is being displayed. Alternatively, You could try something like:

    if($result == '#_USERMOTORCYCLE'){
    	$replace = 'none';
    	if(count($EM_Booking->custom_fields) > 0){
    		$replace = 'inside-of-count';
    		$user_motorcycle = (is_array(get_option('user_motorcycle'))) ? get_option('user_motorcycle'):array();
    		$motorcycles = array();
    		foreach($EM_Booking->custom_fields as $id){
    			$replace = 'inside-of-foreach';
    			if(!empty($user_motorcycle[$id])){
    				$replace = 'inside-of-not-empty';
    				$motorcycles[] = $user_motorcycle[$id];
    			}
    		}
    		$replace = implode(', ', $motorcycles);
    	}
    }

    You’ll notice that I put multiple replace. This way you’ll see where it run/displayed last then you could start troubleshooting for there. ie: if it still shows as “none” then the part if(count($EM_Booking->custom_fields) > 0){ is probably not being triggered means the count is either 0 or less.

    Thread Starter codac

    (@codac)

    Ok, I’ve changed thos code now and figured out that the problem is to get the current booking_id.

    function em_custom_form_fields_email_placeholders($replace, $EM_Booking, $result) {
    	global $wpdb;
    	$sql = $wpdb->prepare("SELECT booking_meta FROM ".mct_em_bookings." WHERE booking_id=%s", $EM_Booking->booking_id);
    	$EM_Booking->custom_fields = $wpdb->get_col($sql, 0);
    
    	if($result == '#_USERMOTORCYCLE'){
    		$replace = 'none'.$sql;.$EM_Booking->booking_id;
            if(count($EM_Booking->custom_fields) > 0){
                $user_motorcycle = (is_array(get_option('user_motorcycle'))) ? get_option('user_motorcycle'):array();
                $motorcycles = array();
                foreach($EM_Booking->custom_fields as $id){
                    if(!empty($user_motorcycle[$id])){
                        $motorcycles[] = $user_motorcycle[$id];
                    }
                }
                $replace = implode(', ', $motorcycles);
            }
    	}
    	return $replace;
    }
    add_filter('em_event_output_placeholder','em_custom_form_fields_email_placeholders',1,3);

    the $replace = 'none'.$sql;.$EM_Booking->booking_id; returns: noneSELECT booking_meta FROM mct_em_bookings WHERE booking_id=0;

    I think when I can figure out how to get the correct booking_id, this topic is solved.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Create custom Placeholder for use in Email notification’ is closed to new replies.