• Hi guys!

    I’ve added custom checkout fields using the docs:

    //Add checkout fields
    	add_filter( 'woocommerce_checkout_fields' , 'add_checkout_fields' );
    
    	function add_checkout_fields( $fields ) {
    		$fields['billing']['type_klant'] = array(
    	     	'type'			=> 'radio',
    	        'label'     	=> 'Type klant',
    		    'clear'     	=> true,
    		    'required'  	=> true,
    		    'options'		=> array(
    		    	'option_1' => 'Particulier',
    				'option_2' => 'Zakelijk'
    			)
    	    );
    	    $fields['billing']['factuur_email'] = array(
    	    	'label'     	=> 'Factuur emailadres',
    		    'placeholder'   => 'Factuur emailadres',
    		    'required'  	=> false,
    		    //'class'     	=> array('form-row-wide'),
    		    'clear'     	=> true
        	);
    	    $fields['billing']['kvk_nr'] = array(
    	    	'label'     	=> 'KVK #',
    		    'placeholder'   => 'vul hier uw KVK nummer in',
    		    'required'  	=> false,
    		    //'class'     	=> array('form-row-wide'),
    		    'clear'     	=> true
        	);
    	    $fields['billing']['functie'] = array(
    	    	'label'     	=> 'Functie',
    		    'placeholder'   => 'vul hier uw functie in',
    		    'required'  	=> false,
    		    //'class'     	=> array('form-row-wide'),
    		    'clear'     	=> true
        	);
        	$fields['billing']['aanhef'] = array(
    	     	'type'			=> 'radio',
    	        'label'     	=> 'Aanhef',
    		    'clear'     	=> true,
    		    'required'  	=> true,
    		    'options'		=> array(
    		    	'option_1' => 'Mevr',
    				'option_2' => 'Dhr'
    			)
    	    );
    
    	    return $fields;
    	}

    They display fine. Now I want to add them to the backend and email, I was stuggeling until I checked the wp_postmeta table and found my added fields ARE present but are all empty
    whats up with that?

    What do we do with the new field? Nothing. Because we defined the field in the checkout_fields array, the field is automatically processed and saved to the order post meta (in this case, _shipping_phone). If you want to add validation rules, see the checkout class where there are additional hooks you can use.

    states It is saved automatically? with me it isnn’t any clues?

    Thanks!

    https://www.ads-software.com/plugins/woocommerce/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    Is this your full code? Can you show us the part which saves the values?

    Thread Starter boriskamp1991

    (@boriskamp1991)

    Hi Mike,

    Thanks for helping out!
    This is it, the quoted part in my question states there’s no need to save it because the field is auto-processed and saved?
    Seems like I’m completely missing something here….

    Plugin Contributor Mike Jolley

    (@mikejolley)

    Prefix each key with billing_ – see if that works.

    Thread Starter boriskamp1991

    (@boriskamp1991)

    Hi Mike,

    that worked perfectly!
    Might be worth updating the docs to make that more clear right?

    I would like to add those fields to the woocommerce_email_customer_details_fields filter.
    I tried this but it did not work, any ideas?

    // Add fields to Email #2
    	add_filter('woocommerce_email_customer_details_fields', 'my_custom_order_meta_keys');
    
    	function my_custom_order_meta_keys( $fields, $sent_to_admin, $order ) {
    	    $fields['_billing_type_klant'] = array(
    			'label' => 'Type Klant_',
    			'value' => wptexturize( $order->_billing_type_klant )
    		);
    		$fields['_billing_type_klant'] = array(
    			'label' => 'Type Klant',
    			'value' => wptexturize( $order->billing_type_klant )
    		);
    	    return $keys;
    	}

    when using

    add_filter('woocommerce_email_order_meta_fields', 'custom_email_order_meta_fields', 10, 3 );
    
    	function custom_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    	    $fields['_billing_type_klant'] = array(
                'label' => __( 'Type Klant' ),
                'value' => get_post_meta( $order->id, '_billing_type_klant', true ),
            );
            $fields['_billing_factuur_email'] = array(
                'label' => __( 'Factuur Email' ),
                'value' => get_post_meta( $order->id, '_billing_factuur_email', true ),
            );
            $fields['_billing_kvk_nr'] = array(
                'label' => __( 'KvK Nummer' ),
                'value' => get_post_meta( $order->id, '_billing_kvk_nr', true ),
            );
            $fields['_billing_functie'] = array(
                'label' => __( 'Functie' ),
                'value' => get_post_meta( $order->id, '_billing_functie', true ),
            );
            $fields['_billing_aanhef'] = array(
                'label' => __( 'Aanhef (voornaam)' ),
                'value' => get_post_meta( $order->id, '_billing_aanhef', true ),
            );
    	    return $fields;
    	}

    they get added above the customer details, I need them in the list

    Thanks Mike!

    Plugin Contributor Mike Jolley

    (@mikejolley)

    You’ll likely want to edit tempalte files if you want them in a different location https://docs.woothemes.com/document/template-structure/

    Thread Starter boriskamp1991

    (@boriskamp1991)

    is there no way I can add or remove to the $fields array in email-customer-details.php without editing template files?

    I can edit class-wc-email.php after line 340 but that would not be that smart with updating right?

    Plugin Contributor Mike Jolley

    (@mikejolley)

    That email doesn’t use the same kind of $fields. You’re just echo’ing meta data.

    Thread Starter boriskamp1991

    (@boriskamp1991)

    Im sorry Mike, but I lost you there.
    Please read my last question without my previous ones in mind:

    is there no way I can add or remove to the $fields array in email-customer-details.php without editing template files?

    I can edit class-wc-email.php after line 340 but that would not be that smart with updating right?

    Do you mind to elaborate a little bit on that question?

    Thanks Mike!

    Plugin Contributor Mike Jolley

    (@mikejolley)

    is there no way I can add or remove to the $fields array in email-customer-details.php without editing template files?

    There is not $fields array for emails, so no you cannot edit that.

    Edit the template file via a template override.

    Thread Starter boriskamp1991

    (@boriskamp1991)

    Thanks for clearing up Mike!

    the file I want to add to is class-wc-emails.php, however, this is not in the templates folder, so I cannot safely override this right?
    let me elaborate, this is the current piece of code in class-wc-emails.php:

    public function customer_details( $order, $sent_to_admin = false, $plain_text = false ) {
    		$fields = array();
    
    		if ( $order->customer_note ) {
    			$fields['customer_note'] = array(
    				'label' => __( 'Note', 'woocommerce' ),
    				'value' => wptexturize( $order->customer_note )
    			);
    		}
    
    		if ( $order->billing_email ) {
    			$fields['billing_email'] = array(
    				'label' => __( 'Email', 'woocommerce' ),
    				'value' => wptexturize( $order->billing_email )
    			);
    	    }
    
    	    if ( $order->billing_phone ) {
    			$fields['billing_phone'] = array(
    				'label' => __( 'Tel', 'woocommerce' ),
    				'value' => wptexturize( $order->billing_phone )
    			);
    	    }
    
    		$fields = array_filter( apply_filters( 'woocommerce_email_customer_details_fields', $fields, $sent_to_admin, $order ), array( $this, 'customer_detail_field_is_valid' ) );
    
    		if ( $plain_text ) {
    			wc_get_template( 'emails/plain/email-customer-details.php', array( 'fields' => $fields ) );
    		} else {
    			wc_get_template( 'emails/email-customer-details.php', array( 'fields' => $fields ) );
    		}
    	}

    this is what I would like:

    public function customer_details( $order, $sent_to_admin = false, $plain_text = false ) {
    		$fields = array();
    
    		if ( $order->customer_note ) {
    			$fields['customer_note'] = array(
    				'label' => __( 'Note', 'woocommerce' ),
    				'value' => wptexturize( $order->customer_note )
    			);
    		}
    
    		if ( $order->billing_email ) {
    			$fields['billing_email'] = array(
    				'label' => __( 'Email', 'woocommerce' ),
    				'value' => wptexturize( $order->billing_email )
    			);
    	    }
    
    	    if ( $order->billing_phone ) {
    			$fields['billing_phone'] = array(
    				'label' => __( 'Tel', 'woocommerce' ),
    				'value' => wptexturize( $order->billing_phone )
    			);
    	    }
                //I added this if statement:
    	    if ( $order->billing_type_klant ) {
    			$fields['billing_type_klant'] = array(
    				'label' => __( 'Customer Type', 'woocommerce' ),
    				'value' => wptexturize( $order->billing_type_klant )
    			);
    	    }
    
               //and more new array items
    
    		$fields = array_filter( apply_filters( 'woocommerce_email_customer_details_fields', $fields, $sent_to_admin, $order ), array( $this, 'customer_detail_field_is_valid' ) );
    
    		if ( $plain_text ) {
    			wc_get_template( 'emails/plain/email-customer-details.php', array( 'fields' => $fields ) );
    		} else {
    			wc_get_template( 'emails/email-customer-details.php', array( 'fields' => $fields ) );
    		}
    	}
    Plugin Contributor Mike Jolley

    (@mikejolley)

    Rather than use that, edit emails/email-customer-details.php template.

    @boriskamp1991,

    the trick is passing the number of args your function expects to add_filter(). By default, it considers only 1 argument is being passed and does not consider the rest. In this case, only $fields array is passed, but not the other 2 ($sent_to_admin and $order).

    By adding the args 10 (priority) and 3 (number of args passed to your function) to add_filter(), it works flawlessly. Example:

    function ico_woocommerce_email_customer_details_fields( $fields, $sent_to_admin, $order ) {
    	if ( $order->billing_cellphone ) {
    		$fields['billing_cellphone'] = array (
    			'label' => __( 'Cell Phone', 'woocommerce-extra-checkout-fields-for-brazil' ),
    			'value' => wptexturize( $order->billing_cellphone )
    		);
    	}
    	return $fields;
    }
    

    Thanks, Mike, for the awesome WooCommerce plugin! ??

    • This reply was modified 8 years, 1 month ago by renatofrota. Reason: forgot wptexturize() on field value
    • This reply was modified 8 years, 1 month ago by renatofrota. Reason: some code polishment
Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘added checkout fields not saving to post_meta’ is closed to new replies.