skip to Main Content
WooCommerce: Add Billing Codes To Checkout & User Profile

Request

We got a request to add a custom field to user profiles allowing multiple codes separated by commas and then add a select containing those codes to the checkout page. These codes were going to be used to add a cc: to pdf invoice emails being sent out separately.

Plugins Required

Code

<?php
/**
// Add Billing Codes user profile field. Used as a comma deliniated list
// See custom_override_checkout_fields()
**/
add_filter( 'user_contactmethods', 'modify_contact_methods' );
function modify_contact_methods( $profile_fields ) {

    // Add new fields
    $profile_fields['billing_code']  =  'Billing Codes';

    return $profile_fields;
}

/**
// Add a billing code to the checkout screen. Customzied to show different values based on the
// value of the User Field "Billing Code"|'billing_code' from modify_contact_methods()
**/
add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {

    $billing_code  =  get_user_meta ( get_current_user_id() );
    $billing_code  =  $billing_code['billing_code']['0'];
    if ( empty ( $billing_code ) ) {
        $num_billing_codes  =  0 ;
    }
    else {
        $billing_code  =  explode ( ',', $billing_code );
        $num_billing_codes  =  count ( $billing_code );
    }

    $fields['billing']['billing_code']  =  array(
            'label'          => __('Bill To', 'woocommerce'),
            'placeholder'    => _x('', 'placeholder', 'woocommerce'),
            'required'       => true,
            'clear'          => false,
            'type'           => 'select',
            'class'          => array('form-row-wide'),
    );

    $i  =  0;
    if ( $num_freshmed_billing_codes  ==  0 ) {
        $fields['billing']['billing_code']['type']  =  'text';
    }
    elseif ( $num_billing_codes == 1 ) {
        $fields['billing']['billing_code']['options']  =  array(
            $billing_code['0']  => __( $freshmed_billing_code['0'], 'woocommerce' ),  
        );
    }
    elseif ( $num_freshmed_billing_codes  >  1 ) {
        $fields['billing']['billing_code']['options'][""]  =  __("Please Select Bill To" , 'woocommerce');
        foreach ( $freshmed_billing_code as $code ) {
            $fields['billing']['billing_code']['options']["$code"]  =  __($code, 'woocommerce');
            $i++;
        }
    }

    return $fields;
}
?>

Display

User Profile
Image: Display of billing code field on the user profile page within WordPress dashboard.

Checkout Page

Display

Image: Display of select input on checkout page

Element Inspector

Image: Layout of select input html within browser's inspector tool

Disclaimer

Maje Media LLC cannot be held responsible for the functionality of this code. Please make sure you test it on a development site before adding the code to your production website. There is no support available for this (and other) code snippet(s) posted on this website. If you’d like Maje Media to do custom development to help with your custom implementation please send a contact request.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top