I'm looking to add a Phone number field to the Woocommerce settings page. I can get the field to add to the settings list, but it gets appended to the bottom of the entire tab. Ideally I'd like to add the field in the Store Address sections on the general tab.
Here's what I'm using now (trimmed down):
add_filter('woocommerce_general_settings', 'woocommerce_add_phone');
function woocommerce_add_phone($settings) {
$settings[] = array(
'title' => 'Phone Number',
'type' => 'text',
);
$sections[] = array( 'type' => 'sectionend', 'id' => 'store_address' );
return $settings;
}
I made an attempt at using array_splice but couldn't get anywhere with it.
Any advice would be greatly appreciated.
This can be done in a simple way targeting the woocommerce_store_postcode field ID to insert the Store phone custom field in this general settings at the right location:
add_filter('woocommerce_general_settings', 'general_settings_shop_phone');
function general_settings_shop_phone($settings) {
$key = 0;
foreach( $settings as $values ){
$new_settings[$key] = $values;
$key++;
// Inserting array just after the post code in "Store Address" section
if($values['id'] == 'woocommerce_store_postcode'){
$new_settings[$key] = array(
'title' => __('Phone Number'),
'desc' => __('Optional phone number of your business office'),
'id' => 'woocommerce_store_phone', // <= The field ID (important)
'default' => '',
'type' => 'text',
'desc_tip' => true, // or false
);
$key++;
}
}
return $new_settings;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and work… You will get something like: