• 437 Views
  • 6 comments
  • 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.