• Resolved ukbaz

    (@ukbaz)


    Hi
    I’m using this for event tickets where attendees get a meal.
    Max ticket purchase per customer is 10 tickets, but as there is an included at the event it’s useful to know Guest names and whether they want ‘standard’ meal option or vegetarian. So using custom fields I have created 10 options for ‘Guest Name’ and ‘Menu Guest’ (see attached code below). These options are displaying in the checkout page, and in the report section so I presume they will be available in the CSV report download as well?

    My question is about formatting the checkout display. If I enter 3 names & menu options This is displayed at checkout:
    Joe BloggsStandard MenuJohn DoeVegetarian MenuJohn AnyoneStandard Menu
    How do I get it to format as:
    Joe Bloggs-Standard Menu, John Doe-Vegetarian Menu, John Anyone-Standard Menu, etc
    Or any other way that displays spacing and separation character?

    Thanks
    Baz

    <?php
    /*
    Plugin Name: My Tickets: Custom Fields
    Plugin URI: https://www.joedolson.com/my-tickets/
    Description: Custom Fields in My Tickets
    Version: 1.1.0
    Author: Joseph Dolson
    Author URI: https://www.joedolson.com/
    */
    /*  Copyright 2014-2018  Joseph C Dolson  (email : [email protected])
    
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.
    
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
    
        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    */
    
    add_filter( 'mt_custom_fields', 'create_custom_fields', 10, 1 );
    /**
     * Add custom fields to each add to cart form. 
     *
     * @param array $array All custom fields.
     *
     * @return array New array of custom fields.
     */
    function create_custom_fields( $array ) {
    	$array['guest_name_1'] = array( 
    		'title'             => 'Guest Name 1 ', 
    		'sanitize_callback' => 'custom_sanitize_callback', 
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'text',
    		'context'           => 'global', 
    		'required'          => 'true',
    	);
    
    	$array['menu_guest_1'] = array(
    		'title'             => 'Menu Guest 1',
    		'sanitize_callback' => 'sanitize_callback',
    		'display_callback'  => 'display_callback',
    		'input_type'        => 'select',
    		'input_values'      => array( 
    			'Standard Menu', 
    			'Vegetarian Menu',  
    		),
    		'context'           => 'global', // Can be an event ID to restrict to that event.
    		'required'          => 'true',
    	);
    	$array['guest_name_2'] = array( 
    		'title'             => 'Guest Name 2', 
    		'sanitize_callback' => 'custom_sanitize_callback', 
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'text',
    		'context'           => 'global', 		
    	);
    
    	$array['menu_guest_2'] = array(
    		'title'             => 'Menu Guest 2',
    		'sanitize_callback' => 'sanitize_callback',
    		'display_callback'  => 'display_callback',
    		'input_type'        => 'select',
    		'input_values'      => array(
    			'', 
    			'Standard Menu', 
    			'Vegetarian Menu',  
    		),
    		'context'           => 'global', // Can be an event ID to restrict to that event.		
    	);
    	$array['guest_name_3'] = array( 
    		'title'             => 'Guest Name 3', 
    		'sanitize_callback' => 'custom_sanitize_callback', 
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'text',
    		'context'           => 'global', 
    	);
    
    	$array['menu_guest_3'] = array(
    		'title'             => 'Menu Guest 3',
    		'sanitize_callback' => 'sanitize_callback',
    		'display_callback'  => 'display_callback',
    		'input_type'        => 'select',
    		'input_values'      => array(
    			'', 
    			'Standard Menu', 
    			'Vegetarian Menu',  
    		),
    		'context'           => 'global', // Can be an event ID to restrict to that event.
    	);
    	$array['guest_name_4'] = array( 
    		'title'             => 'Guest Name 4', 
    		'sanitize_callback' => 'custom_sanitize_callback', 
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'text',
    		'context'           => 'global', 
    	);
    
    	$array['menu_guest_4'] = array(
    		'title'             => 'Menu Guest 4',
    		'sanitize_callback' => 'sanitize_callback',
    		'display_callback'  => 'display_callback',
    		'input_type'        => 'select',
    		'input_values'      => array(
    			'', 
    			'Standard Menu', 
    			'Vegetarian Menu',  
    		),
    		'context'           => 'global', // Can be an event ID to restrict to that event.
    	);
    
    	$array['guest_name_5'] = array( 
    		'title'             => 'Guest Name 5', 
    		'sanitize_callback' => 'custom_sanitize_callback', 
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'text',
    		'context'           => 'global', 
    	);
    
    	$array['menu_guest_5'] = array(
    		'title'             => 'Menu Guest 5',
    		'sanitize_callback' => 'sanitize_callback',
    		'display_callback'  => 'display_callback',
    		'input_type'        => 'select',
    		'input_values'      => array(
    			'', 
    			'Standard Menu', 
    			'Vegetarian Menu',  
    		),
    		'context'           => 'global', // Can be an event ID to restrict to that event.
    	);
    	$array['guest_name_6'] = array( 
    		'title'             => 'Guest Name 6', 
    		'sanitize_callback' => 'custom_sanitize_callback', 
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'text',
    		'context'           => 'global', 
    	);
    
    	$array['menu_guest_6'] = array(
    		'title'             => 'Menu Guest 6',
    		'sanitize_callback' => 'sanitize_callback',
    		'display_callback'  => 'display_callback',
    		'input_type'        => 'select',
    		'input_values'      => array(
    			'', 
    			'Standard Menu', 
    			'Vegetarian Menu',  
    		),
    		'context'           => 'global', // Can be an event ID to restrict to that event.
    	);
    	$array['guest_name_7'] = array( 
    		'title'             => 'Guest Name 7', 
    		'sanitize_callback' => 'custom_sanitize_callback', 
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'text',
    		'context'           => 'global', 
    	);
    
    	$array['menu_guest_7'] = array(
    		'title'             => 'Menu Guest 7',
    		'sanitize_callback' => 'sanitize_callback',
    		'display_callback'  => 'display_callback',
    		'input_type'        => 'select',
    		'input_values'      => array(
    			'', 
    			'Standard Menu', 
    			'Vegetarian Menu',  
    		),
    		'context'           => 'global', // Can be an event ID to restrict to that event.
    	);
    	$array['guest_name_8'] = array( 
    		'title'             => 'Guest Name 8', 
    		'sanitize_callback' => 'custom_sanitize_callback', 
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'text',
    		'context'           => 'global', 
    	);
    
    	$array['menu_guest_8'] = array(
    		'title'             => 'Menu Guest 8',
    		'sanitize_callback' => 'sanitize_callback',
    		'display_callback'  => 'display_callback',
    		'input_type'        => 'select',
    		'input_values'      => array(
    			'', 
    			'Standard Menu', 
    			'Vegetarian Menu',  
    		),
    		'context'           => 'global', // Can be an event ID to restrict to that event.
    	);
    	$array['guest_name_9'] = array( 
    		'title'             => 'Guest Name 9', 
    		'sanitize_callback' => 'custom_sanitize_callback', 
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'text',
    		'context'           => 'global', 
    	);
    
    	$array['menu_guest_9'] = array(
    		'title'             => 'Menu Guest 9',
    		'sanitize_callback' => 'sanitize_callback',
    		'display_callback'  => 'display_callback',
    		'input_type'        => 'select',
    		'input_values'      => array(
    			'', 
    			'Standard Menu', 
    			'Vegetarian Menu',  
    		),
    		'context'           => 'global', // Can be an event ID to restrict to that event.
    	);
    	$array['guest_name_10'] = array( 
    		'title'             => 'Guest Name 10', 
    		'sanitize_callback' => 'custom_sanitize_callback', 
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'text',
    		'context'           => 'global', 
    	);
    
    	$array['menu_guest_10'] = array(
    		'title'             => 'Menu Guest 10',
    		'sanitize_callback' => 'sanitize_callback',
    		'display_callback'  => 'display_callback',
    		'input_type'        => 'select',
    		'input_values'      => array(
    			'', 
    			'Standard Menu', 
    			'Vegetarian Menu',  
    		),
    		'context'           => 'global', // Can be an event ID to restrict to that event.
    	);
    	
    	return $array;
    
    }
    
    /**
     * This display callback is used to format the saved data. 
     *
     * @param mixed  $data Value of saved data.
     * @param string $context either 'payment' or 'cart'.
     * 
     * @return data passed.
     */
    function custom_display_callback( $data, $context='payment' ) {
    	return ( $data ) ? urldecode( $data ) : '';
    }
    
    /**
     * This sanitize callback is used to sanitize the data before it's saved to the DB 
     *
     * @param mixed $data Data supplied by user.
     *
     * @return sanitized value
     */
    function custom_sanitize_callback( $data ) {
    	return ( $data ) ? esc_html( $data ) : '';
    }
    
    add_filter( 'mt_apply_custom_field_rules', 'my_custom_field_rules', 10, 3 );
    /**
     * Use a custom rule set to determine when a field should be displayed.
     *
     * @param bool  $return Should this field display.
     * @param array $field Field characteristics.
     * @param int   $event_id Event being displayed.
     *
     * @return boolean
     */
    function my_custom_field_rules( $return, $field, $event_id ) {
    	if ( 'custom' == $field['context'] ) {
    		// Display this field based on your custom rules.
    		// Example: restrict by post type.
    		if ( is_page( $event_id ) ) {
    			return true;
    		}
    	}
    
    	return $return;
    }
    
    • This topic was modified 6 years, 1 month ago by ukbaz.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Joe Dolson

    (@joedolson)

    My first instinct was to suggest using the display_callback function to add styles to fields. That would work, to a point, as long as you added the same styles to every field. What would be better is if the display_callback received the field array as an argument, so that you could choose HTML to wrap the values based on which field was displayed.

    I’m adding that parameter to the next release; I’ll get that out fairly soon.

    Thread Starter ukbaz

    (@ukbaz)

    Hi Joe
    Many thanks for the very quick response! I’ll keep an eye out for that update then. If you have time to give a quick ‘pointer’ to how it works, that would be great!

    Thanks again
    Baz

    Plugin Author Joe Dolson

    (@joedolson)

    I’ll add more detail to the GitHub example for custom fields, so that will be documented there.

    Plugin Author Joe Dolson

    (@joedolson)

    Release currently in progress; GitHub example already updated.

    Thread Starter ukbaz

    (@ukbaz)

    Hi Joe – thanks for that tried and tested and working fine!

    Quick question – I currently have the following code for Guest name (test box) and Menu Choice(select):

    function create_custom_fields( $array ) {
    	$array['guest_name_1'] = array( 
    		'title'             => 'Guest Name 1 ', 
    		'sanitize_callback' => 'custom_sanitize_callback', 
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'text',
    		'context'           => 'global', 
    		'required'          => 'true',
    	);
    
    	$array['menu_guest_1'] = array(
    		'title'             => 'Menu Guest 1',
    		'sanitize_callback' => 'custom_sanitize_callback',
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'select',
    		'input_values'      => array( 
    			'Standard Menu', 
    			'Vegetarian Menu',  
    		),
    		'context'           => 'global', 
    		'required'          => 'true',
    	);
    	$array['guest_name_2'] = array( 
    		'title'             => 'Guest Name 2', 
    		'sanitize_callback' => 'custom_sanitize_callback', 
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'text',
    		'context'           => 'global', 		
    	);
    
    	$array['menu_guest_2'] = array(
    		'title'             => 'Menu Guest 2',
    		'sanitize_callback' => 'custom_sanitize_callback',
    		'display_callback'  => 'custom_display_callback',
    		'input_type'        => 'select',
    		'input_values'      => array(
    			'', 
    			'Standard Menu', 
    			'Vegetarian Menu',  
    		),
    		'context'           => 'global', 		
    	);
    

    How can I change the above code to replace the select 3 options (blank, standard & vegetarian) with a single ‘vegetarian’ checkbox that would just display when checked?

    Thanks

    Baz

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom Fields styles’ is closed to new replies.