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.
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.
Comments