2
votes

I have to make two customization on my woocommerce site.

I need to know two main hooks. Someone out there please help me out!

  1. Show custom field values to billing address on order-received page.(I added custom fields on checkout page.)

  2. Need to include these values to customers' order-received email, too.

Thanks for stopping by.

1

1 Answers

1
votes

To show custom field in order-received page you have to use woocommerce_thankyou hook.

Here is the code:

// define the woocommerce_thankyou callback 
function action_woocommerce_thankyou($order_id)
{
    $my_custom_field = get_post_meta($order_id, '_billing_my_field', TRUE);
}

// add the action 
add_action('woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1);


UPDARED

To add custom billing fields in WooCommerce email you have to use woocommerce_email_customer_details hook; this will be displayed just before the customer details.

Here is the code:

add_filter('woocommerce_email_customer_details', 'custom_woocommerce_email_order_meta_fields', 10, 3);

function custom_woocommerce_email_order_meta_fields($order, $sent_to_admin, $plain_text)
{
    $_billing_my_field = get_post_meta($order->id, '_billing_my_field', true);
    if ($plain_text)
    {
        echo 'My field is ' . $_billing_my_field;
    }
    else
    {
        echo '<p>My field is  ' . $_billing_my_field . '</p>';
    }
}

All code goes in function.php file of your active child theme (or theme) or also in any plugin file.


  • I have assuming that you have added a custom billing fields as $fields['billing']['billing_my_field'] using woocommerce_checkout_fields hook.
  • All the codes are tested and fully functional.