2

I have successfully setup some custom fields on checkout and it all works fine.

I have a checkbox as one of the fields, my_gift_wrap_checkbox, and I managed to write code to display text in the orders page ('yes please!' or 'no thank you') instead of a simple blank or 1 if it is checked or not.

I can't figure out how to achieve the same in the email code. My code is:

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

function my_woocommerce_email_order_meta_keys( $keys ) {
    $keys['Gift wrap?'] = 'my_gift_wrap_checkbox';
    $keys['Gift wrap instructions'] = 'my_gift_wrap_field';
    return $keys;
}

Right now:

  • If it's checked, I get Gift wrap?: 1
    • I want Gift wrap?: Yes please!
  • If it's not checked, it shows Gift wrap?:
    • I want Gift wrap?: No thank you.

1 Answer 1

1

Try this instead:

add_action( "woocommerce_email_after_order_table", "my_woocommerce_email_after_order_table", 10, 1);

function my_woocommerce_email_after_order_table( $order ) {
    $my_gift_wrap_checkbox = get_post_meta( $order->id, "my_gift_wrap_checkbox", true );
    $gift_wrap = $my_gift_wrap_checkbox ? 'Yes please!' : 'No thank you.';

    echo '<p><strong>Gift wrap?: </strong>' . $gift_wrap . '</p>';

    if ( $my_gift_wrap_checkbox ) {
        echo '<p><strong>Gift wrap instructions: </strong>' . get_post_meta( $order->id, "my_gift_wrap_field", true ) . '</p>';
    }

}
1
  • Thanks that worked perfectly!Do you happen to know how to get the same thing to appear on the order confirmation page the customer sees after purchase. That was my next challenge and I have not found anydocumentation
    – user9557
    Commented Aug 26, 2016 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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