A shortcode to display the billing company name of a user on a WooCommerce WordPress site

Why

  • You want to show the name of the user’s company on a page.

Usage

[hx_billing_company default_text='No company specified']

Background

WooCommerce adds a custom field called billing_company, which is filled out by the user with the name of their company. There is no built-in shortcode to display this information on a page. Fortunately, the field is implemented as user metadata and easily made accessible with a shortcode. You can install this shortcode manually or add it as a code snippet with plugins like Code Snippets.

Shortcode

#
# hx_render_billing_company_shortcode
#
#   This function renders the [hx_billing_company] shortcode, which
#   displays the billing_company field of the current user. This
#   field is created by WooCommerce. Therefore to show the 
#   company, the site must have WooCommerce installed and the
#   user must be logged in.
#
# attributes:
#
#   default_text:
#       Shown when the company name cannot be loaded.
#
# login_text:
#       Shown when the user is not logged in, preventing
#       the company name from being loaded. If left blank,
#       default_text is shown as a fallback. Set this attribute
#       only if you want different text when the user is not
#       logged in.
#
function hx_render_billing_company_shortcode( $atts ) {
	
    // Default attributes
    $default_atts = array(
        'default_text' => '', // blank by default
        'login_text'   => '', // blank by default
    );

    // Allow customization via filter
    $default_atts = apply_filters(
        'hx_billing_company_shortcode_defaults',
        $default_atts );

    // Merge user-provided attributes
    $atts = shortcode_atts(
        $default_atts,
        $atts,
        'hx_billing_company' );

    // Fallback: use default_text if login_text is not provided
    if ( empty( $atts['login_text'] ) ) {
        $atts['login_text'] = $atts['default_text'];
    }

    if ( is_user_logged_in() ) {

        $user_id = get_current_user_id();
        #
        # Get the billing_company metadata field created by
        # WooCommerce based on the ID and field name. The
        # true parameter name specifies to return a single value
        # rather than an array of values.
        #
        $billing_company = get_user_meta(
            $user_id,
           'billing_company',
            true );

        if ( $billing_company ) {
            return esc_html( $billing_company );
        } else {
            return esc_html( $atts['default_text'] );
        }
    } else {
        return esc_html( $atts['login_text'] );
    }
}
add_shortcode('hx_billing_company', 'hx_render_billing_company_shortcode');

How to use

To manually install this shortcode, you can add it to functions.php in your theme. However, this is dangerous and we recommend using a plugin that allows you to create and manage custom bits of code without changing your theme files. Two plugins we recommend are Code Snippets and WPCode. Both have free versions that allow you to create custom shortcodes.

Reference

Leave a Reply