WordPress.org

Forums

WooCommerce - excelling eCommerce
How to display custom checkout fields? (6 posts)

  1. absoluteweb
    Member
    Posted 2 years ago #

    Hi,

    I followed this tutorial to add a date of birth field but I don't know how to display this field in the customer record (users) and in the order notification email.

    Thank's
    Nicolas.

    http://wordpress.org/extend/plugins/woocommerce/

  2. royho
    Member
    Posted 2 years ago #

  3. absoluteweb
    Member
    Posted 2 years ago #

    Hi,

    for the notification email it's possible to use this snippet but I can't chose the title of this custom field.

    My custom field :

    // Hook in
    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    
    // Our hooked in function - $fields is passed via the filter!
    function custom_override_checkout_fields( $fields ) {
         $fields['billing']['naissance'] = array(
            'label'     => __('Date de naissance', 'woocommerce'),
        'placeholder'   => _x('JJ/MM/AAAA', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide'),
        'clear'     => true
         );
    
         return $fields;
    }

    If I use this snippet

    add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
    
    function my_custom_checkout_field_order_meta_keys( $keys ) {
    $keys[] = '_naissance';
    return $keys;
    }

    Into the emails notification I obtain

    _naissance: 31/01/1970

    Another solution, I override emails template and chose in which, how and where add my custom field

    Just for admin notifications, in admin-new-order.php I add

    <?php if ($order->order_custom_fields["_naissance"][0]) : ?>
    	<p><strong>Date de naissance :</strong> <?php echo $order->order_custom_fields["_naissance"][0]; ?></p>
    <?php endif; ?>

    Custom fields are linked to the order but I dont no how link them to the customer profile...

  4. lehoaian
    Member
    Posted 2 years ago #

    Hi,

    I have added some code to function.php to test the validate function.
    Like bellow :

    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
    
    function my_custom_checkout_field_process() {
    	global $woocommerce;
    	$woocommerce->add_error( 'Some test ' . __('is a required field.', 'woocommerce') );
    
    }

    But when I press submit button on Cart, nothing appear ...
    Is i did a wrong !

    Please help

  5. absoluteweb
    Member
    Posted 2 years ago #

    Hi,

    Strange, here is the code I use to date of birth

    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
    
    function my_custom_checkout_field_process() {
        global $woocommerce;
        if ($_POST['naissance']) {
            $date = explode("/", $_POST['naissance']);
    		if(!checkdate($date[1], $date[0], $date[2]))
    			$woocommerce->add_error( __('<strong>Date de naissance</strong> invalide.') );
    		else {
    			$date18YearsAdded = strtotime(date("Y-m-d", strtotime($date[2].'-'.$date[1].'-'.$date[0])) . " +18 year");
    			if(date("Y-m-d", $date18YearsAdded)>date("Y-m-d"))
    				$woocommerce->add_error( __('<strong>Date de naissance :</strong> vente interdite aux mineurs') );
    		}
    	}
    }
  6. clickmac
    Member
    Posted 2 years ago #

    @absoluteweb have you figured out how to change the "_naissance"

    in

    _naissance: 31/01/1970

    ? if so, please do share.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.