Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce I am looking for billing address and order-received email hooks

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.

like image 735
norixxx Avatar asked Nov 15 '25 14:11

norixxx


1 Answers

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 This section after post author comment

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.


Please Note:
  • 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.
like image 125
Raunak Gupta Avatar answered Nov 17 '25 09:11

Raunak Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!