Add Customer Name Title Prefix to Woocommerce Checkout Page
384
post-template-default,single,single-post,postid-384,single-format-standard,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,qode-content-sidebar-responsive,qode-child-theme-ver-1.0.0,qode-theme-ver-13.0,qode-theme-bridge,wpb-js-composer js-comp-ver-5.4.4,vc_responsive

Add Customer Name Title Prefix to Woocommerce

I had a client ask for a name prefix for their woocommerce  store, with the added caveat that they needed it to work in the admin orders page as they take orders over the telephone. Also included in this script, which can be removed, is a translation of wordings, for example changing the word shipping to delivery, which is more a UK based phrase and also forcing the characters in the  checkout for to be uppercase, this is very handy for printing invoices, making the invoice presentable.

We Also add a new mobile field on checkout and in the admin orders page.

We also add an order time for the orders screen in this plugin, this can be helpful if a customer phones and tells you what time they placed an order with you etc.

You can omit the sections of the script you do not want, you can either put in functions.php or just add as a plugin

To use as a plugin you can download with the link at the bottom of the post or copy the code to a notepad and save as a php file, then zip it and upload to WordPress

<?php
/**

Plugin URI: https://learningtoblog.co.uk
Plugin Name: Change Billing and Shipping wording and uppercase address fields also add title prefix to woocommerce
Description: Changes the wording of woocommerce billing and shipping to invoice and delivery, forces uppercase letters in checkout and limits order notes to 50 chars, and adds title/prefix
Author: Tudor gibson
Author URI: https://learningtoblog.co.uk
Version: 0.1
License: GPL2
*/
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foun

// This section translates wording, you can edit adapt for your own needs or delete if not needed

add_filter(‘gettext’, ‘translate_reply’);
add_filter(‘ngettext’, ‘translate_reply’);

function translate_reply($translated) {
$translated = str_ireplace(‘Shipping’, ‘Delivery’, $translated);
$translated = str_ireplace(‘via’, ”, $translated);
$translated = str_ireplace(‘Billing’, ‘Invoice’, $translated);
$translated = str_ireplace(‘Order Number’, ‘Invoice Number’, $translated);
$translated = str_ireplace(‘Order Date’, ‘Invoice Date’, $translated);
return $translated;
}

// This section forces uppercase characters in the woocmmerce checkout when saved in the database, edit to suite your own setup or delete

// add the desired keys to this array in the following format, eg.
$uppercasefields = array(‘billing_first_name’,’billing_last_name’,’billing_company’,’billing_country’,’billing_address_1′,’billing_address_2′,’billing_city’,’billing_state’,’billing_postcode’,’shipping_first_name’,’shipping_last_name’,’shipping_company’,’shipping_country’,’shipping_address_1′,’shipping_address_2′,’shipping_city’,’shipping_state’,’shipping_postcode’);

foreach ($uppercasefields as $fieldkey) {
add_filter(‘woocommerce_process_checkout_field_’.$fieldkey,’custom_field_to_uppercase’);
}
function custom_field_to_uppercase($value) {return strtoupper($value);}

// This add the time of order to the orders page

add_filter( ‘post_date_column_time’, ‘custom_post_date_column_time’, 10, 2 );
function custom_post_date_column_time( $h_time, $post ) {
return get_the_time( __( ‘d/m/Y g:i:s A’, ‘woocommerce’ ), $post );
}

// This hides some of those pesky update notifications of plugins and wordpress, helpful for keeping the admin screen cleaner

// hide update notifications
function remove_core_updates(){
global $wp_version;return(object) array(‘last_checked’=> time(),’version_checked’=> $wp_version,);
}
add_filter(‘pre_site_transient_update_core’,’remove_core_updates’); //hide updates for WordPress itself
add_filter(‘pre_site_transient_update_plugins’,’remove_core_updates’); //hide updates for all plugins
add_filter(‘pre_site_transient_update_themes’,’remove_core_updates’); //hide updates for all themes

// This changes the order comments to delivery instructions or shipping instructions, and limits to 50 characters, stopping customers leaving an essay on where to deliver to. Also relabels the mobile field that we are about to creat to Mobile

add_filter( ‘woocommerce_checkout_fields’, ‘filter_checkout_fields’ );

function filter_checkout_fields( $fields ) {
$fields[‘order’][‘order_comments’][‘maxlength’] = 50;
$fields[‘order’][‘order_comments’][‘placeholder’] = ‘Extra delivery instructions 50 characters max’;
$fields[‘order’][‘order_comments’][‘type’] = ‘text’;
$fields[‘billing’][‘billing_mobile’][‘label’] = ‘Mobile’;
return $fields;
}

//global array to reposition the elements to display as you want (e.g. kept ‘title’ before ‘first_name’ )
$wdm_address_fields = array(
‘title’, //new field
‘first_name’,
‘last_name’,
‘company’,
‘address_1’,
‘address_2’,
‘city’,
‘state’,
‘postcode’,
‘country’,
‘mobile’);

//global array only for extra fields
$wdm_ext_fields = array(‘title’,’mobile’);

add_filter( ‘woocommerce_default_address_fields’ , ‘wdm_override_default_address_fields’ );

function wdm_override_default_address_fields( $address_fields ){

$temp_fields = array();

$address_fields[‘title’] = array(
‘label’ => __(‘Title’, ‘woocommerce’),
‘required’ => false,
‘class’ => array(‘form-row-custom’),
‘type’ => ‘select’,
‘options’ => array (” => __(‘Select option’, ‘woocommerce’), ‘MR’ => __(‘Mr’, ‘woocommerce’), ‘MRS’ => __(‘Mrs’, ‘woocommerce’), ‘MISS’ => __(‘Miss’, ‘woocommerce’), ‘MS’ => __(‘Ms’, ‘woocommerce’), ‘DR’ => __(‘Dr’, ‘woocommerce’))
);

$address_fields[‘mobile’] = array(
‘label’ => __(‘Mobile’, ‘woocommerce’),
‘required’ => false,
‘placeholder’ => _x(‘Mobile’, ‘placeholder’, ‘woocommerce’),
‘class’ => array(‘form-row-first’),
‘type’ => ‘text’,
‘clear’ => true,
‘value’=>get_post_meta( $theorder->id, ‘billing_phone’, true ),
‘show’ => true
);

global $wdm_address_fields;

foreach($wdm_address_fields as $fky){
$temp_fields[$fky] = $address_fields[$fky];
}

$address_fields = $temp_fields;

return $address_fields;
}

add_filter(‘woocommerce_formatted_address_replacements’, ‘wdm_formatted_address_replacements’, 99, 2);

function wdm_formatted_address_replacements( $address, $args ){

$address[‘{name}’] = $args[‘title’].” “.$args[‘first_name’].” “.$args[‘last_name’]; //show title along with name
$address[‘{mobile}’] = $args[‘mobile’];

return $address;

}

add_filter( ‘woocommerce_order_formatted_billing_address’, ‘wdm_update_formatted_billing_address’, 99, 2);

function wdm_update_formatted_billing_address( $address, $obj ){

global $wdm_address_fields;

if(is_array($wdm_address_fields)){

foreach($wdm_address_fields as $waf){
$address[$waf] = $obj->{‘billing_’.$waf};
}
}

return $address;
}

add_filter( ‘woocommerce_order_formatted_shipping_address’, ‘wdm_update_formatted_shipping_address’, 99, 2);

function wdm_update_formatted_shipping_address( $address, $obj ){

global $wdm_address_fields;

if(is_array($wdm_address_fields)){

foreach($wdm_address_fields as $waf){
$address[$waf] = $obj->{‘shipping_’.$waf};
}
}

return $address;
}

add_filter(‘woocommerce_my_account_my_address_formatted_address’, ‘wdm_my_account_address_formatted_address’, 99, 3);

function wdm_my_account_address_formatted_address( $address, $customer_id, $name ){

global $wdm_address_fields;

if(is_array($wdm_address_fields)){

foreach($wdm_address_fields as $waf){
$address[$waf] = get_user_meta( $customer_id, $name.’_’.$waf, true );
}
}

return $address;
}

add_filter(‘woocommerce_admin_billing_fields’, ‘wdm_add_extra_customer_field’);
add_filter(‘woocommerce_admin_shipping_fields’, ‘wdm_add_extra_customer_field’);

function wdm_add_extra_customer_field( $fields ){

//take back up of email and phone fields as they will be lost after repositioning
$email = $fields[’email’];
$phone = $fields[‘phone’];
$mobile = array(
‘label’ => __( ‘Mobile’, ‘woocommerce’ ),
‘show’ => true,
);

$fields = wdm_override_default_address_fields( $fields );

//reassign email and phone fields
$fields[’email’] = $email;
$fields[‘phone’] = $phone;
$fields[‘mobile’] = $mobile;

global $wdm_ext_fields;

if(is_array($wdm_ext_fields)){

foreach($wdm_ext_fields as $wef){
$fields[$wef][‘show’] = false; //hide the way they are display by default as we have now merged them within the address field
}
}

return $fields;
}

add_action( ‘woocommerce_admin_order_data_after_billing_address’, ‘my_mobile_custom_checkout_field_display_admin_order_meta’, 10, 1 );

function my_mobile_custom_checkout_field_display_admin_order_meta($order){
$mobile = get_post_meta( $order->id, ‘_billing_mobile’, true );
if(!($mobile == null || $mobile == ”)){
echo ‘<p><strong>’.__(‘Mobile’).’:</strong> ‘ . get_post_meta( $order->id, ‘_billing_mobile’, true ) . ‘</p>’;
}
}
/**
* Add the field to order emails
**/
add_filter(‘woocommerce_email_order_meta_keys’, ‘my_woocommerce_email_order_meta_keys’);

function my_woocommerce_email_order_meta_keys( $keys ) {
$keys[‘Mobile’] = ‘_billing_mobile’;
return $keys;
}

//* Add CSS directly into the admin head, you can edit this to suite your screen size
add_action(‘admin_head’, ‘rv_custom_wp_admin_style_head’);
function rv_custom_wp_admin_style_head() {
echo ‘<style type=”text/css”>
.form-field._billing_title_field {
top: -55px;
position: absolute;
}
.form-field._shipping_title_field {
top: -55px;
position: absolute;
}
p.form-field._shipping_email_field {
display: none;
}
p.form-field._shipping_phone_field {
display: none;
}
</style>’;
}

 

We also had to edit the order-customer-details.php so it showed new details for invoicing etc and saved to child theme

This is the section modified adding billing_mobile under billing_phone

<?php if ( $order->billing_phone ) : ?>
<tr>
<th><?php _e( ‘Telephone:’, ‘woocommerce’ ); ?></th>
<td><?php echo esc_html( $order->billing_phone ); ?></td>
</tr>
<?php endif; ?>
<?php if ( $order->billing_mobile ) : ?>
<tr>
<th><?php _e( ‘Mobile:’, ‘woocommerce’ ); ?></th>
<td><?php echo esc_html( $order->billing_mobile ); ?></td>
</tr>
<?php endif; ?>

You can download the plugin here

You can contact me here if you run into problems. Every woocommerce store and theme are different so you have to adapt to your needs.

No Comments

Sorry, the comment form is closed at this time.

Read previous post:
SEO Faqs Answered

Here is my view on some of the most frequently asked questions regarding SEO for your website and business. Do...

Close