Skip to content

Instantly share code, notes, and snippets.

@tankbar
Created December 14, 2017 10:50
Show Gist options
  • Save tankbar/75fa0fbfe82b405e391de22525140b37 to your computer and use it in GitHub Desktop.
Save tankbar/75fa0fbfe82b405e391de22525140b37 to your computer and use it in GitHub Desktop.
WooCommerce - Add GTIN/EAN meta field for product and product variations
/**
* 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', 'textdomain' ),
'placeholder' => '01234567891231',
'desc_tip' => 'true',
'description' => __( 'Enter the Global Trade Item Number (UPC,EAN,ISBN)', 'textdomain' )
)
);
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', '');
}
}
// Add Variation GTIN Meta Field
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_gtin[' . $variation->ID . ']',
'label' => __( 'GTIN', 'textdomain' ),
'placeholder' => '01234567891231',
'desc_tip' => 'true',
'description' => __( 'Enter the Global Trade Item Number (UPC,EAN,ISBN)', 'textdomain' ),
'value' => get_post_meta( $variation->ID, '_gtin', true )
)
);
}
// Save Variation GTIN Meta Field Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $post_id ) {
$gtin_post = $_POST['_gtin'][ $post_id ];
// 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', '');
}
}
@MarkPraschan
Copy link

Thanks! How are there not more people looking for a way to store GTIN numbers for variations?

@Ongarr
Copy link

Ongarr commented Oct 18, 2019

thx for code! how can i add GTIN to woocommerce API?

@LouisEssers
Copy link

Thanx for the code! How can I change GTIN naming on single product page?
20200426 - GTIN naming on product page

@DanielVERC
Copy link

Hello Thanks for the code ;-)...
But i got a problem.
It works on Wordpress WooCommerce Back Office.
I can add a EAN/GTIN code.
But i can't see it on the front, could you have an idea ?

Many thanks...

Capture d’écran 2021-04-16 à 13 12 07
Capture d’écran 2021-04-16 à 13 07 14

@jorgept
Copy link

jorgept commented Oct 14, 2022

How can i show the GTIN/EAN value in front?

The most indicated place on the product page is in the information tab, where the attributes appear.
I have this code

add_filter( 'woocommerce_display_product_attributes', 'custom_product_additional_information', 10, 2 );
function custom_product_additional_information( $product_attributes, $product ) {
    //row
    $product_attributes[ 'attribute_' . 'custom' ] = array(
        'label' => __('GTIN'),
        'value' => $product->get_meta('yourfield'),
    );
    return $product_attributes;
}

But when the value is empty, the label always appears.
How can I change it so that when the value is empty, it doesn't appear?

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment