I worked on a project that required integration with SalesForce via the Zapier extension. In order to make this work smoothly, one of the requirements was to use a field that would always have the same value. Zapier can pull in the billing and shipping fields so we decided to use company billing to handle this. Of course, if the customer actually had a billing company, we would then need to find a way to include it.  Fortunately, there are both WooCommerce extensions as well as actions and filters available to implement adding a custom field.  WooThemes has good documentation on how to add custom billing and shipping fields.  Following the documentation for adding the hooks and filters, we added a new field “custom_billing_company_name” where the customer could place the billing company name. This field is then stored as “Billing Company Name” in the post meta. While this worked, we still needed to show the custom field in the addresses whenever this full address was presented to the customer – either online or in an email.

It turns out that WooCommerce  has filters for this purpose:

  • woocommerce_order_formatted_billing_address
  • woocommerce_order_formatted_shipping_address

 
Below you can see an example of modifying the address.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
/*use our custom field in the formatted addresses that show for the customer*/
add_filter('woocommerce_order_formatted_billing_address', 'perfectimage_order_formatted_billing_address', 10, 2);
function perfectimage_order_formatted_billing_address($address, $wc_order) {
    $address = array(
        'first_name'    => $wc_order->billing_first_name,
        'last_name'     => $wc_order->billing_last_name,
        'company'       => get_post_meta( $wc_order->id, 'Billing Company Name', true ),
        'address_1'     => $wc_order->billing_address_1,
        'address_2'     => $wc_order->billing_address_2,
        'city'          => $wc_order->billing_city,
        'state'         => $wc_order->billing_state,
        'postcode'      => $wc_order->billing_postcode,
        'country'       => $wc_order->billing_country
    );
    return $address;
}