Sarathlal N

Add phone number to the output of the addresses on the "My Account" page - WooCommerce

Add phone number to the output of the addresses on the "My Account" page - WooCommerce

We need to add 3 filters to modify the output of the addresses on the “My Account” page/shortcode for WooCommerce.

First, we will need to use woocommerce_my_account_my_address_formatted_address to populate any new values we want to add.

add_filter( 'woocommerce_my_account_my_address_formatted_address', 'er34d_formatted_address',10, 3 );

function er34d_formatted_address( $args, $customer_id, $name ){
	// the phone is saved as billing_phone and shipping_phone
	$args['phone'] = get_user_meta( $customer_id, $name . '_phone', true );
	return $args;
} 

Next use woocommerce_localisation_address_formats to modify the formatting of the address. We have do this as per country locale. But now I ignore that condition.

// modify the address formats
add_filter( 'woocommerce_localisation_address_formats', '3e45_localisation_address_formats');

function 3e45_localisation_address_formats( $formats ){
	foreach ( $formats as $key => &$format ) {
		// put a break and then the phone after each format.
		$format .= "\n{phone}";
	}
	return $formats;
}

Last, we need to update woocommerce_formatted_address_replacements to have WooCommerce replace your replacement string with the actual data.

// add the replacement value
add_filter( 'woocommerce_formatted_address_replacements', '3er45_formatted_address_replacements', 10, 2 );

function 3er45_formatted_address_replacements( $replacements, $args ){
	// we want to replace {phone} in the format with the data we populated
	$replacements['{phone}'] = $args['phone'];
	return $replacements;
}

Recent Posts

  1. Automating Release Generation with GitHub Actions
  2. WP CLI Commands to Bulk Delete Entries in WordPress Database
  3. Split a Single CSV File into Multiple Files Using the Split Command - Bash
  4. Migrating code repo from BitBucket to GitHub
  5. Streamlining Development - Our Journey with Git, Bitbucket, and Jira

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.