Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Last active February 26, 2018 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChromeOrange/c8efc3c6f70949f33c5d9ffa75c42221 to your computer and use it in GitHub Desktop.
Save ChromeOrange/c8efc3c6f70949f33c5d9ffa75c42221 to your computer and use it in GitHub Desktop.
Add billing and or shipping title to the address area. This code assumes that you have already included the necessary field on the checkout and have stored it in the order, you can use the WooCommerce Checkout Field Editor plugin for that. The title field should be called billing_title or shipping_title
// Add a field to the customer's address, eg title
add_filter( 'woocommerce_order_formatted_billing_address', 'pdf_invoice_woocommerce_order_formatted_billing_address', 10, 2 );
function pdf_invoice_woocommerce_order_formatted_billing_address( $address, $order ) {
// Add the post meta from the order
$billing_title = get_post_meta( $order->get_id(), 'billing_title', TRUE );
// Add $billing title to the beginning of the address
$address = array( "billing_title" => $billing_title ) + $address;
// Return the address
return $address;
}
add_filter('woocommerce_formatted_address_replacements', 'pdf_invoice_add_woocommerce_formatted_address_replacements', 10, 2);
function pdf_invoice_add_woocommerce_formatted_address_replacements($replace, $args) {
$replace['{title}'] = $args['title'];
$replace['{name}'] = $args['title']." ".$args['first_name']." ".$args['last_name']; //show title along with name
return $replace;
}
add_filter( 'woocommerce_order_formatted_billing_address', 'pdf_invoice_update_formatted_billing_address', 10, 2);
function pdf_invoice_update_formatted_billing_address( $address, $order ){
$address_fields = array(
'title',
'first_name',
'last_name',
'company',
'address_1',
'address_2',
'city',
'state',
'postcode',
'country'
);
if( is_array($address_fields) ) {
foreach( $address_fields as $address_field ){
$field = "get_billing_" . $address_field;
if( method_exists( $order, $field ) ) {
$address[$address_field] = $order->$field();
} else {
$address[$address_field] = $order->{'billing_'.$address_field};
}
}
}
return $address;
}
add_filter( 'woocommerce_order_formatted_shipping_address', 'pdf_invoice_update_formatted_shipping_address', 10, 2);
function pdf_invoice_update_formatted_shipping_address( $address, $order ){
$address_fields = array(
'title',
'first_name',
'last_name',
'company',
'address_1',
'address_2',
'city',
'state',
'postcode',
'country'
);
if( is_array($address_fields) ) {
foreach( $address_fields as $address_field ){
$field = "get_shipping_" . $address_field;
if( method_exists( $order, $field ) ) {
$address[$address_field] = $order->$field();
} else {
$address[$address_field] = $order->{'shipping_'.$address_field};
}
}
}
return $address;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment