Otero
Otero

Reputation: 29

custom field values not getting to mail

iam using woocommerce plugin... actually this is a php doubt I have.. Iam using some custom fields, so i shuld get values of those custom fields into my email..

When Iam using one custom field 'My Field' , iam getting value for that to my email, BUT iam not understanding how to get values of all custom fields to my email..

Below is the code that works for single custom field: (from here: https://gist.github.com/3905785 )

/**
 * Add the field to the checkout
 **/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
    woocommerce_form_field( 'my_field_name', array( 
        'type'          => 'checkbox', 
        'class'         => array('my-field-class form-row-wide'), 
        'label'         => __('Fill in this field'), 
        'placeholder'   => __('Enter a number'),
        ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';
}

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
}

/**
 * Add the field to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');

function my_custom_checkout_field_order_meta_keys( $keys ) {
    $keys[] = 'My Field';
    return $keys;
}


I tried below code for 2 custom fields: (below code not getting any custom field values to my mail.. ) pls tell me what am i doing wrong in below code:

/**
 * Add the field to the checkout
 **/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
    woocommerce_form_field( 'my_field_name', array( 
        'type'          => 'checkbox', 
        'class'         => array('my-field-class form-row-wide'), 
        'label'         => __('Fill in this field'), 
        'placeholder'   => __('Enter a number'),
        ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';



  echo '<div id="my_custom_checkout_field"><h3>'.__('Keywords').'</h3>';

    woocommerce_form_field( 'keywords', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Fill in this field'),
        'placeholder'       => __('Enter something'),
        ), $checkout->get_value( 'keywords' ));

    echo '</div>';
}

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));

        if ($_POST['keywords']) update_post_meta( $order_id, 'Keywords', esc_attr($_POST['keywords']));
}

/**
 * Add the field to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');

function my_custom_checkout_field_order_meta_keys( $keys ) {
    $keys[] = 'My Field,Keywords';
    return $keys;
}

Upvotes: 1

Views: 1455

Answers (1)

user1694691
user1694691

Reputation:

First of all the line:

$keys[] = 'My Field,Keywords';

should be changed to:

$keys[] = 'My Field';
$keys[] = 'Keywords';

The code you had was creating one array entry with the string 'My Field,Keywords' as opposed to 2 array entries with the strings 'My Field' and 'Keywords'.

This is definitely one of the causes of the problem.

It may not be critical but you should change the id of the second div tag - having multiple elements with the same id is not good.

Make these changes and try again. If there are further problems then we can tackle them in turn.

I hope this helps.

Upvotes: 1

Related Questions