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.

Displaying on Front End

Retrieving meta data is fairly easy in WordPress. You can use the following snippet to retrieve any meta data of a post (within the loop).

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

All you need to do is simply change the “meta_key” with a valid one and place this snippet within your desired location of a template file. Generally that template file is “single.php” for post but for WooCommerce product it is bit more complicated than simply modifying a template file.

However, you can always take advantage of hooks to inject your data and display them as you wish. I used the following snippet to display the GTIN meta data within the product page.

add_action('woocommerce_product_meta_end', function () {
   global $product;
      if ($product->is_type('simple')) {
         $gtin_data = get_post_meta($product->get_id(), '_gtin', true);
         if (!empty($gtin_data)) {
            echo '<p>GTIN: '.$gtin_data.'</p>';
      }
   }
});

That should resolve your issue with displaying GTIN data for simple products in WooCommerce.

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 and incorporate it within the existing snippet.

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. Also note that, any product meta data like this one can be added within the structured data (JSON format) generated by WooCommerce. I also wrote another post explaining how you can add GTIN field for variable products which some of you may find useful.

Today In History

  •  
  •  

Comment

30 Comments

    Louis EssersPublished: 6 months ago

    Added the code snippets to my WP site (GTIN to meta data, show on front end product page), works good. Thanks!

    But: how to include GTIN in the search functionality of my site? Search does not recognize GTIN now.

    zeeshan uddinPublished: 8 months ago

    I have added all the codes your provided and in the product inventory its showing also in store front end, i have added the UPC also but google structure data is not seeing. Any solution plz?

      Iftekhar BhuiyanPublished: 8 months ago

      Zeeshan, adding structured data is fairly simple and easy to implement. I shared a link within the “Note” section of this post that explains how to do that. Following the instruction should get your job done. If that didn’t work, there must be something that went wrong. You can get in touch with me from the contact page for additional support. Thanks.

    AmePublished: 10 months ago

    Hello Iftekhar,

    Thank you very much for the code, I added everything and it works, google structured data sees gtin. Is it ok for google if the gtin is not shown on front -end?
    To show this meta field on front end what should I do?
    I also see link with other code from Luke, should I use this or from github?

    Thanks in advance,
    Amelia

      IftekharPublished: 10 months ago

      Hello Ame, thank you very much for reaching out and I am more than happy to know that this snippet was useful for you.

      Now, let’s be clear on the fundamental differences between “product feed” and “structured data” first. Product feed generally gets generated as an external file in XML format whereas structured data is incorporated within the page itself by WooCommerce in JSON format which is not visible to visitors. Google can crawl both and retrieve information from them.

      Displaying GTIN data from front-end is not a requirement by Google. However, it is required by Google only if you submit product feed to them for whatever the purpose is. On the other hand it is a good practice to display the GTIN data from the front end because some visitor or potential client may find this information to be useful to identify product. I updated my post and provided the snippet to display GTIN data from front end. You can follow that as well.

      Luke’s snippet seemed a bit more polished to me. You may or may not choose to use that snippet as it’s your decision to make and I honestly can’t suggest something that I personally did not try out or wrote. Hope that helps.

      Thank you.

    JérémyPublished: 11 months ago

    Very usefull 🙂 I’ve seen people are talking about GSC before. I’m looking at adding this GTIN in my structured data. I have no idea where Woocommerce stores the json file for the structured data, so I’m not sure how to add this GTIN in this data. Any idea ? Thanks again

      IftekharPublished: 11 months ago

      Jeremy, WC does not create or store any json file (*.json) to hold structured product data. It retrieves all the relevant product meta data from database and then generates the structured data within the following tag.
      <script type="application/ld+json"></script>
      This tag gets injected or hooked while viewing a product from single-product.php page. Now, as you are saving the GTIN as product meta data on your database, it can also be retrieved and injected into the existing structured data generated by WC. I already wrote an article on this issue and shared the link within the “note” section located at the bottom part of this post. You should find the necessary instructions from there. Hope that helps. Thank you for stopping by.

    StefPublished: 1 year 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.

    StefPublished: 1 year ago

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

      Iftekhar BhuiyanPublished: 1 year 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.

      StefPublished: 1 year 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.

      Iftekhar BhuiyanPublished: 1 year 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.

    Peter Netz LassenPublished: 1 year 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

      Iftekhar BhuiyanPublished: 1 year 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.

    Echo JamesonPublished: 1 year 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?

      Iftekhar BhuiyanPublished: 1 year 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.

    Luke CavanaghPublished: 3 years ago

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

      DanielPublished: 3 years ago

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

    PeterPublished: 3 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

      Iftekhar BhuiyanPublished: 3 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.

    DavidPublished: 3 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?

      Iftekhar BhuiyanPublished: 3 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 🙂

    Thakur BhanuPublished: 3 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?

      Iftekhar BhuiyanPublished: 3 years ago

      Hello Thakur, 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.

    Anas MirzaPublished: 3 years ago

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

      Iftekhar BhuiyanPublished: 3 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.

    Mark CapwellPublished: 3 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

      Iftekhar BhuiyanPublished: 3 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.

    ChanningPublished: 3 years ago

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

      Iftekhar BhuiyanPublished: 3 years ago

      You are welcome 🙂

Leave a Reply to zeeshan uddin

Note: Convet HTML, PHP, JavaScripts from HTMLify, 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.