Home » WordPress » How to Add a custom fields to WooCommerce General Product Data tab

How to Add a custom fields to WooCommerce General Product Data tab

In this tutorial we are going to add custom check box to WooCommerce General product tab.

to add custom checkbox in WooCommerce General tab we will use bellow hook.

 woocommerce_product_options_general_product_data

Open your function.php and add bellow code:


function wc_add_wpdumi_custom_fields()
{
global $post;

$input_checkbox = get_post_meta($post->ID, 'wpdumi_checkbox', true);
if (empty($input_checkbox)) $input_checkbox = '';

woocommerce_wp_checkbox(array(
'id' => 'wpdumi_checkbox',
'label' => __('Wpdumi checkbox', 'woocommerce'),
'description' => __('Helpful tooltip text', 'woocommerce'),
'value' => $input_checkbox,
));
}
add_action('woocommerce_product_options_general_product_data', 'wc_add_wpdumi_custom_fields');

after creating your custom field, then we can save product meta using bellow code.


function wc_save_wpdumi_custom_custom_field($post_id)
{
$_custom_text_option = isset($_POST['wpdumi_checkbox']) ? 'yes' : '';
update_post_meta($post_id, 'wpdumi_checkbox', $_custom_text_option);
}
add_action('woocommerce_process_product_meta', 'wc_save_wpdumi_custom_custom_field');
Here we are:

References

Duminda Wijerathna

An all round web designer building websites Over 18 years. Interested in achieving a suitable placement in the field of IT in a growth oriented organization which offers diverse job responsibilities in order to utilize and improve my skills, Knowledge and experience.

Leave a Comment

Your email address will not be published. Required fields are marked *