Add GTIN for Simple WooCommerce Product

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(){
   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){
   if(isset($_POST['_gtin']) && !empty($_POST['_gtin'])) {
      $gtin = sanitize_text_field($_POST['_gtin']);
      update_post_meta($post_id,'_gtin', $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.

WooCommerce Product Meta GTIN Input FieldWooCommerce Product Meta GTIN 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. Also note that, any product meta data like this one can be added within the structured data (JSON format) generated by WooCommerce.

Today In History

  •  
  •  

Comment

23 Comments

  1. ChanningPublished: 2 years ago

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

  2. IftekharPublished: 2 years ago

    You are welcome 🙂

  3. MarkPublished: 2 years 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

  4. IftekharPublished: 2 years 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.

  5. Anas MirzaPublished: 2 years ago

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

  6. IftekharPublished: 2 years 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.

  7. BenPublished: 2 years 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?

  8. IftekharPublished: 2 years 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.

  9. DavidPublished: 2 years 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?

  10. IftekharPublished: 2 years 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 🙂

  11. PeterPublished: 2 years 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

  12. IftekharPublished: 2 years 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.

  13. Luke CavanaghPublished: 2 years ago

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

  14. DanPublished: 1 year ago

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

  15. echoPublished: 4 months ago

    I got this to work but when I go to Google and test the URL thru their Structured Data tool I still get an error for GTIN. What am I missing?

  16. IftekharPublished: 4 months ago

    I personally don’t see any reason why this snippet should cause any error unless any third party plugin already using the “_gtin” meta key! Adding custom meta data has nothing to do with structured data as WooCommerce won’t recognize this meta key to begin with. However, please see the note section of this post for a link of another tutorial to help you out with the structured data related issues. Thank you for stopping by.

  17. Peter Netz LassenPublished: 4 months ago

    Hi
    Thanks for this – Really helpful.
    What is the diff between your code on github? and this?
    I have been using your snippet for a while and some of my vendors ask why I don’t use EAN numbers… ?? I couldn’t figure out how to add this to my product feed?

    Or is it due to the field is called _gtin and not _ean?

    Should I use the snippet here on the github version? what would happen if I change from github to this? Can I retain all my _gtin values or how?

    Thx

    Peter

  18. IftekharPublished: 4 months ago

    The GitHub version of this snippet is not mine. However, I glimpsed at it and it seems like the gentleman took my snippet and polished it a bit which I think is the right way to go. Then again, I did not test his snippet so I can’t say it for sure.

    Now, you can definitely use EAN number on the existing GTIN field without making any change to your code. Your data will still be saved with “_gtin” meta key on database. As far as generating the product feed is concern, I already wrote a post on this topic and provided the link at the bottom part of my post. That snippet works flawlessly as well. If you need more technical assistance, feel free to drop me a line. Thanks for stopping by.

  19. StefPublished: 4 days ago

    Does this get picked up by Google at all though? None of these scripts ever do. Curious to know.

  20. Iftekhar BhuiyanPublished: 4 days ago

    Google Merchant Center requires product feed, especially if you want to run an advertising campaign. You can check this page out for more information.

  21. StefPublished: 4 days ago

    Sorry Iftekhar. I didn’t make sense with that question. I meant does this get picked up by Google inside Google Search Console? I never had a problem with this until recently and I’m trying to find why all of sudden I have warnings & errors inside GSC. I never had before.

    So my question is if we get something like this planted inside our themes and then it shows on the front end will GSC pick it up?

    Hope that’s a little clearer.

  22. Iftekhar BhuiyanPublished: 4 days ago

    This snippet to generate product feed has nothing to do with GSC whatsoever. Google generally looks for product’s Structured Data. Only Google supported valid structured data will get crawled and displayed by GSC without any errors.

    WooCommerce adds certain structured data for each product in JSON format but that may or may not get validated by GSC because Google has it’s own requirements for validation.

    I already discussed this issue earlier which you may find useful to resolve those errors. Check out Google’s tool to test out the structured data.

    I used custom snippet to add structured product data for items that I reviewed on this site. GSC successfully crawled them without any errors. Hope that helps.

  23. StefPublished: 2 days ago

    I’m very aware of all those tools and how to do this and that.

    I guess what I was trying to get at is was this article more or less about adding a GTIN for shopping feed requirements or for covering issues within WooCommerce?

    I thought your article was the fix for the errors in GSC. Guess I read it wrong.

Leave a Reply to David

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.