Add GTIN for Simple WooCommerce Product

  • 16 comments
  • 7456 Views
  • Print

Recently I discussed about Google Merchant Center and how we can take advantage of simple snippet that can allow us to generate product feed automatically from WooCommerce as we update or add new product.

On my personal experience though I noticed Google Merchant Center sometimes does not approve (for advertisement) certain products, specially if it is from a third party Company or Brand. In most cases Google would ask for GTIN (Global Trade Item Number) for product verification. This could be the EAN, UPC, or ISBN of your product. Ofcourse it depends on the type of product you are selling.

I already discussed on how to add product brand for WooCommerce products. So, let’s talk about adding custom GTIN meta field for simple WooCommerce Product. Here is the snippet.

/** 
 * Adding Custom GTIN Meta Field
 * Save meta data to DB
 */
// add GTIN input field
add_action('woocommerce_product_options_inventory_product_data','woocom_simple_product_gtin_field', 10, 1 );
function woocom_simple_product_gtin_field(){
   global $woocommerce, $post;
   $product = new WC_Product(get_the_ID());
   echo '<div id="gtin_attr" class="options_group">';
   //add GTIN field for simple product
   woocommerce_wp_text_input( 
      array(	
         'id' => '_gtin',
         'label' => 'GTIN',
         'desc_tip' => 'true',
         'description' => 'Enter the Global Trade Item Number (UPC,EAN,ISBN)')
   );
   echo '</div>';
}
// save simple product GTIN
add_action('woocommerce_process_product_meta','woocom_simple_product_gtin_save');
function woocom_simple_product_gtin_save($post_id){
   $gtin_post = $_POST['_gtin'];
   // save the gtin
   if(isset($gtin_post)){
      update_post_meta($post_id,'_gtin', esc_attr($gtin_post));
   }
   // remove if GTIN meta is empty
   $gtin_data = get_post_meta($post_id,'_gtin', true);
   if (empty($gtin_data)){
      delete_post_meta($post_id,'_gtin', '');
   }
}

Like always, simply copy this snippet on your current theme’s functions.php page and update it. The snippet above should generate the GTIN input field on Simple Product’s inventory tab of product meta box. This is how our GTIN input field should look like.
GTIN - WooCommerce Meta Input Field

Adding GTIN on your Product Feed

Now, in order to provide the GTIN of your products within your product feed, you should simply retrieve the meta data for your product that has _gtin key. This is how generally you can retrieve your product meta data(depending on meta key).

echo get_post_meta($post->ID,'_gtin', true);

For your product feed though, you must provide the following lines in between the <item></item> tag which is located within the foreach loop. Read More from this link.

$gtin = get_post_meta($post->ID,'_gtin', true);
$gmc .= '<g:gtin>'.$gtin.'</g:gtin>';

Feel free to use the comment section if you have any question regarding this snippet. Thanks.

Note: I would highly recommend you to try out this snippet on your computer first before you incorporate them on a live site. This is a fully functional snippet that is currently being used on a live site.

  •  
  •  
  •  

Comment

16 Comments

  1. ChanningPublished: 1 year ago

    Very helpful! Thanks so much for sharing! This worked on our site.

    • IftekharPublished: 1 year ago

      You are welcome 🙂

  2. davePublished: 1 year ago

    Thank you so much! There’s not even a plugin made for this. It worked perfectly 🙂

    • IftekharPublished: 1 year ago

      Dave, you are welcome. I am happy to know that you found this snippet useful. Cheers.

  3. MarkPublished: 1 year ago

    Hello Iftekhar – I use the woocommerce-google-product-feed plugin. Can you tell me how to modify the plugin to include the GTIN info? I’ve added the snippet and it works great. Just need to know how to get that info into the plugin.

    Thanks

    • IftekharPublished: 1 year ago

      Hello Mark, I am not quite sure which plugin you were talking about. However, I already wrote another post explaining how to generate product feed for Google Merchant center. On that post, I also pointed out the possible usage of GTIN within the feed. You can check out that post and try to incorporate this snippet along with that one. I think that may resolve your issue. Please note that these snippets (combined) are currently being used on a live eCommerce site without any issue. Hope that helps. Thank you.

  4. Anas MirzaPublished: 1 year ago

    Added the snippet. But can’t see the GTIN field in Product manager

    • IftekharPublished: 1 year ago

      Hi, I am not quite familiar with this “Product Manager” thing that you are talking about. Can you explain a bit more? Just in case if you are talking about any third party plugin, you would have to incorporate the GTIN value as you normally would for any post meta. Hope that helps. Thanks.

  5. BenPublished: 1 year ago

    I copied the code for GTIN as well as products feed to google and when I clicked update site crashed. Do you know how I can get back to it now?

    • IftekharPublished: 1 year ago

      Hello Ben, sorry to hear that. I couldn’t find any reason why your site would break up unless you are adding this snippet within a wrong file (other than functions.php file) or a wrong place (outside of "<?php ?>" tag ). However the easiest method to get your site back up online is to remove this snippet from the file (where you pasted it). Simply download the file using any FTP client, remove the code from it using “Notepad” and upload it back where your file was. Hope that helps. Thank you.

  6. DavidPublished: 1 year ago

    Wow this is great! How can you add one for a variable product. Woo commerce has personal SKU’s but does not have UPC yet it is now compulsory for google feeds. Do you have any plugins or ways to do this?

    • IftekharPublished: 1 year ago

      Hello David. So, I generate product feed (for Google Merchant Center) using my own code which does not submit the variations of a “Variable Product”. For both the “Simple” and “Variable” product, I use only one GTIN number. So, let’s say I have a product with different color variations (red, blue & green), if I use the “red” item to be my main product image, I would use red colored item’s GTIN. Google never complained about it.

      Also, since I have thousands (almost) of products, I find it more reasonable to treat a variable item as a single product than going by their variations and generate a huge XML file. This may not be a great idea specially if the site is hosted on a shared hosting account where almost all the product has variations. Other factors like, how often we add/delete/update products, how often do we generate the XML file etc. needs to be take in consideration as well because it would definitely have some impact on the site’s overall performance. Then again that’s just me. Thank you for stopping by 🙂

  7. PeterPublished: 1 year ago

    Great job, also the code you write for adding a brand field to woocommerce products. However the brand field shows up in the dropdown menu, product_brand, for plugins which you can use to generated a feed. The GTIN field does not shows up in the dropdown menu. Is it possible that this GTIN field appears as well?

    Best Regards,
    Peter

    • IftekharPublished: 1 year ago

      Hello Peter, I am really sorry that I couldn’t respond to your comment right away. Unfortunately your question was not quite clear to me either. Why would you wan’t the GTIN field to appear as a dropdown menu? What exactly you are trying to achieve out of that? Please feel free to elaborate the question below. I would be happy to assist you if I can. Thank you.

  8. Luke CavanaghPublished: 10 months ago

    Cleaned up code snippet. https://gist.github.com/lukecav/6efbd9bca8668f19c0c96d8b00507a53

    • DanPublished: 7 months ago

      I have pasted the code to my child theme function.php, however, nothing is showing up on the front end. Any suggestion?

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.