PHP WordPress WooCommerce
WooCommerce - Adding Custom Product Meta Fields
568 days agoThe code below shows you how to add custom meta fields to your products. This can be added to a mu-plugins, custom plugin or functions.php file.
<?php
/**
* Display the custom label field
* @since 1.0.0
*/
add_action(
"woocommerce_product_options_general_product_data",
"hoog_add_custom_product_fields"
);
function hoog_add_custom_product_fields()
{
// Add Custom Input
$args = [
"id" => "ean",
"label" => __("EAN", "hd"),
"class" => "hd-ean-label",
"desc_tip" => true,
"description" => __("This will add EAN to the product", "hd"),
"data_type" => "text",
];
woocommerce_wp_text_input($args);
// Add Custom Input
$args = [
"id" => "gtin,
"label" => __("GTIN", "hd"),
"class" => "hd-gtin-label",
"desc_tip" => true,
"description" => __("This will add GTIN to the product", "hd"),
"data_type" => "text",
];
woocommerce_wp_text_input($args);
}
/**
* Save the custom field
* @since 1.0.0
*/
add_action(
"woocommerce_process_product_meta",
"hoog_custom_general_fields_save",
10
);
function hoog_custom_general_fields_save($post_id)
{
$ean = $_POST["ean"];
update_post_meta($post_id, "ean", esc_attr($ean));
$gtin = $_POST["gtin"];
update_post_meta($post_id, "gtin", esc_attr($gtin));
}
