Using GTIN Numbers on Products in WooCommerce

Posted on by Dan Pock
Category: Other | Tags: WooCommerce, WordPress
Reading Time: 2 minutes

By default WooCommerce adds in a SKU custom field to products, but that may not cover your needs, depending on the products you offer. If you’re selling books or magazines ISBN numbers are going to be necessary. Various GTIN (Global Trade Identification Number) options are also important, both for selling and for tracking your products.

How to add GTIN number fields to WooCommerce Products

Or: I need to be able to add GTIN numbers onto the products listed on my store, but WooCommerce only adds an option for SKU?

Fortunately, there is code that can be added to your theme’s function.php that will allow you to use different GTIN options, be they UPC, EAN or ISBN numbers, for the products you’re selling. This is also handy if you happen to be listing your products on Amazon, via a feed.

Note:
WordPress Code Warning: This article features code changes, or snippets, that you can make in your active themes function.php file. If you are unfamiliar with this task, or want to brush up, we have an article on Managing code snippets in WooCommerce. The technique provided can be used for both WordPress and WooCommerce sites.
<?php
/**
 * Add Global Trade Identification Numbers (GTINs) to WooCommerce products.
 *
 * @link /kb/using-gtin-numbers-woocommerce/
 */

/**
 * Render the Global Trade Identification Number (GTIN) meta field.
 */
function woocommerce_render_gtin_field() {
   $input   = array(
      'id'          => '_gtin',
      'label'       => sprintf(
         '<abbr title="%1$s">%2$s</abbr>',
         _x( 'Global Trade Identification Number', 'field label', 'my-theme' ),
         _x( 'GTIN', 'abbreviated field label', 'my-theme' )
      ),
      'value'       => get_post_meta( get_the_ID(), '_gtin', true ),
      'desc_tip'    => true,
      'description' => __( 'Enter the Global Trade Identification Number (UPC, EAN, ISBN, etc.)', 'my-theme' ),
   );
?>

   <div id="gtin_attr" class="options_group">
      <?php woocommerce_wp_text_input( $input ); ?>
   </div>

<?php
}

add_action( 'woocommerce_product_options_inventory_product_data', 'woocommerce_render_gtin_field' );

/**
 * Save the product's GTIN number, if provided.
 *
 * @param int $product_id The ID of the product being saved.
 */
function woocommerce_save_gtin_field( $product_id ) {
   if (
      ! isset( $_POST['_gtin'], $_POST['woocommerce_meta_nonce'] )
      || ( defined( 'DOING_AJAX' ) && DOING_AJAX )
      || ! current_user_can( 'edit_products' )
      || ! wp_verify_nonce( $_POST['woocommerce_meta_nonce'], 'woocommerce_save_data' )
   ) {
      return;
   }

   $gtin = sanitize_text_field( $_POST['_gtin'] );

   update_post_meta( $product_id, '_gtin', $gtin );
}

add_action( 'woocommerce_process_product_meta','woocommerce_save_gtin_field' );

It is recommended that you try this in a staging environment, which allows you the ability to make sure you have the functionality you’re looking for, without impacting your live site until you’re ready.

Alternative Option: Use a plugin to add GTINs

The option above is great if you're familiar with customizing your site with code. You can easily drop that code into your themes functions.php file or even as a mu-plugin and get rolling. For those less familiar with using code though an alternative exists. There is a plugin on WordPress.org that can add the field in for you called WooCommerce Add GTIN: UPC, EAN, and ISBN. Just install this plugin like any other one and you'll be good to go.

Give us a call at 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgeable Solutions or Experienced Hosting advisors to learn how you can take advantage of these techniques today!

Avatar for Dan Pock

About the Author: Dan Pock

Dan Pock does technical support at Liquid Web with a background in System Administration, Public Relations, and Customer Service. His favorite things include his cats, Oscar Boots, and Dash Nougat; experimenting with PHP; and making up recipes (or at least attempting to). You can find his coding hijinks on GitHub, where he shares most of his projects and open source work.

Latest Articles

In-place CentOS 7 upgrades

Read Article

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article