Created
December 14, 2017 10:50
-
-
Save tankbar/75fa0fbfe82b405e391de22525140b37 to your computer and use it in GitHub Desktop.
WooCommerce - Add GTIN/EAN meta field for product and product variations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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', ''); | |
} | |
} |
thx for code! how can i add GTIN to woocommerce API?
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
Thanks! How are there not more people looking for a way to store GTIN numbers for variations?