0

Am having trouble showing custom field data from a select drop down in the order email form in Woocommerce. The data is showing in the admin page order just not email?

    add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {

echo '<div class="clear"></div><br/>';
echo '<div id="my_custom_checkout_field"><h3 style="margin:0 0 0.3em;">Payment Options</h3>';
echo '<p style="margin:0 0 0.8em;clear:left;">Please select your preferred payment method below:</p>';

global $current_user; 
get_currentuserinfo(); 
if ( user_can( $current_user, "wholesale_buyer" ) ){ 
        echo '<div class="pTrade" style="clear:left;">';
            woocommerce_form_field( 'pTrade', array( 
              'type'             => 'select', 
              'class'            => false, 
              'label'            => __('', 'woocommerce'), 
              'required'         => false,
              'options'          => array(
                    '' => __('Select', 'woocommerce' ),
                    'paypal' => __('PayPal invoice', 'woocommerce' ),
                    'bank' => __('Bank Transfer', 'woocommerce' ),
                    'credit' => __('Credit Account', 'woocommerce' ),
                    'cash' => __('Pickup & Pay', 'woocommerce' )
                    )
            ), $checked );
        echo '</div>';
    } else {
        echo '<div class="pRetail" style="clear:left;">';
            woocommerce_form_field( 'pRetail', array( 
              'type'             => 'select', 
              'class'            => false, 
              'label'            => __('', 'woocommerce'), 
              'required'         => false,
              'options'          => array(
                    '' => __('Select', 'woocommerce' ),
                    'paypal' => __('PayPal invoice', 'woocommerce' ),
                    'bank' => __('Bank Transfer', 'woocommerce' ),
                    'cash' => __('Pickup & Pay', 'woocommerce' )
                    )
            ), $checked );
        echo '</div>';
}
echo '<p style="margin:0 0 0.8em;clear:left;color:red;"><strong>Note:</strong> Selecting to pay through "PayPal invoice" adds 5% to your order total. <br/> You will recieve an email from PayPal requesting the amount below + 5%. </p>';
echo '</div>';
echo '<div class="clear"></div>';
}

/** Update the order meta with field value  WORKING ****************/
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['pTrade']) update_post_meta( $order_id, 'Trade payment method', esc_attr($_POST['pTrade']));
        if ($_POST['pRetail']) update_post_meta( $order_id, 'Retail payment method', esc_attr($_POST['pRetail']));
    }

/** Add the field to order email **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
    function my_custom_checkout_field_order_meta_keys($keys) {
        echo '<h3>Payment Option :</h3>';
        $keys['Trade pay by:'] = 'pTrade';
        $keys['Retail pay by:'] = 'pRetail';
        return $keys;
    }

I have read this doc - https://gist.github.com/mikejolley/1604009 and a couple of others but still not showing any joy with it!.

2

If the code you have provided is the only code you are using to achieve what you want then thats incomplete code. Below is the tested version of code which works well. I hope this helps:

/* 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( 'p_trade', array(
        'type'          => 'select',
        'class'         => array('p-trade orm-row-wide'),
        'label'         => __('Trade Details'),
        'placeholder'   => __('Enter a number'),
        'clear'     => true,
        'options' => array( 'trade-1' => 'Trade One', 'trade-2' => 'Trade Two', 'trade-3' => 'Trade Three', 'trade-4' => 'Trade Four' )
        ), $checkout->get_value( 'p_trade' ));

        woocommerce_form_field( 'p_retail', array(
            'type'          => 'select',
            'class'         => array('p-retail orm-row-wide'),
            'label'         => __('Retail Details'),
            'placeholder'   => __('Enter a number'),
            'clear'     => true,
            'options' => array('retail-1' => 'Retail One', 'retail-2' => 'Retail Two', 'retail-3' => 'Retail Three', 'retail-4' => 'Retail Four')
        ), $checkout->get_value( 'p_retail' ));
    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['p_trade']) update_post_meta( $order_id, 'p_trade', esc_attr($_POST['p_trade']));
    if ($_POST['p_retail']) update_post_meta( $order_id, 'p_retail', esc_attr($_POST['p_retail']));
}

/* Add the fields to order email */

add_filter( 'woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys' );
function my_custom_checkout_field_order_meta_keys( $keys ) {
                echo '<h3>Payment Option:</h3>';
        $keys['Trade'] = 'p_trade';
        $keys['Retail'] = 'p_retail';
        return $keys;
}
4
  • Thanks for getting back, ive reedited my commented and added the full code. Apr 25 '16 at 2:31
  • mine is slightly different from yours but should that effect it ? in the filed "options" i have - 'paypal' => __('PayPal invoice', 'woocommerce' )' dose having woocommerce make any difference ? Apr 25 '16 at 2:33
  • No that does not make any difference. In __('PayPal invoice', 'woocommerce' ) ... woocommerce is a text domain used for string translation purpose. Apr 26 '16 at 3:34
  • How do you add the custom field to a particular area within the email? For example, I want to display the custom order meta field within the opening text of customer-completed-order.php
    – Garconis
    Sep 5 '17 at 18:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.