Custom product field
<?php
add_action('woocommerce_product_options_inventory_product_data', 'woocom_simple_product_ean_field', 10, 1);
function woocom_simple_product_ean_field()
{
global $woocommerce, $post;
$product = new WC_Product(get_the_ID());
echo '<div id="ean_attr" class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_ean',
'label' => __('EAN', 'zomerspecialist'),
'placeholder' => '01234567891231',
'desc_tip' => 'true',
'description' => __('Voer hier de EAN code in', 'zomerspecialist')
)
);
echo '</div>';
}
add_action('woocommerce_process_product_meta', 'woocom_simple_product_ean_save');
function woocom_simple_product_ean_save($post_id)
{
$ean_post = $_POST['_ean'];
if (isset($ean_post)) {
update_post_meta($post_id, '_ean', esc_attr($ean_post));
}
$ean_data = get_post_meta($post_id, '_ean', true);
if (empty($ean_data)) {
delete_post_meta($post_id, '_ean', '');
}
}
add_action('woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3);
function variation_settings_fields($loop, $variation_data, $variation)
{
woocommerce_wp_text_input(
array(
'id' => '_ean[' . $variation->ID . ']',
'label' => __('ean', 'textdomain'),
'placeholder' => '01234567891231',
'desc_tip' => 'true',
'description' => __('Voer hier de EAN code in', 'zomerspecialist'),
'value' => get_post_meta($variation->ID, '_ean', true)
)
);
}
add_action('woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2);
function save_variation_settings_fields($post_id)
{
$ean_post = $_POST['_ean'][ $post_id ];
if (isset($ean_post)) {
update_post_meta($post_id, '_ean', esc_attr($ean_post));
}
$ean_data = get_post_meta($post_id, '_ean', true);
if (empty($ean_data)) {
delete_post_meta($post_id, '_ean', '');
}
}