Add Custom field in woocommerce Product admin
/**
* Displays the demostock input field in the WooCommerce product data meta box
*/
function cfwc_create_demostock_field() {
$args = array(
‘id’ => ‘demostock’,
‘label’ => __( ‘Demo Stock’, ‘cfwc’ ),
‘class’ => ‘cfwc-demostock-field’,
‘desc_tip’ => true,
‘description’ => __( ‘Demo Stock’, ‘ctwc’ ),
);
woocommerce_wp_text_input( $args );
}
add_action( ‘woocommerce_product_options_general_product_data’, ‘cfwc_create_demostock_field’ );
/**
* Saves the demostock field data to product meta data
*/
function cfwc_save_demostock_field( $post_id ) {
$product = wc_get_product( $post_id );
$title = isset( $_POST[‘demostock’] ) ? $_POST[‘demostock’] : ”;
$product->update_meta_data( ‘demostock’, sanitize_text_field( $title ) );
$product->save();
}
add_action( ‘woocommerce_process_product_meta’, ‘cfwc_save_demostock_field’ );