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;
}

Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!

Recent Posts

  1. Automating Code Linting with GitHub Actions for WordPress Plugins
  2. Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer
  3. The Ultimate Guide to Indexing in Database Design
  4. Understanding 'update_meta_cache' in WordPress - When to Use It, When Not to, and Its Impact on Database Queries
  5. A Guide to Configuring JavaScript and SCSS Paths in WordPress Plugins with @wordpress/scripts

Your Questions / Comments

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