Woocommerce add custom field to Products


I want to add a cost element to each product also a notes text area for purchasing notes etc

  • in your own child themes functions.php
add_action( 'woocommerce_product_options_pricing', 'wc_cost_product_field' );
function wc_cost_product_field() {
 woocommerce_wp_text_input( array( 'id' => 'purchasing_note', 'class' => 'wc_input_price short', 'label' => __( 'Cost Price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) );
}


add_action( 'save_post', 'wc_cost_save_product' );
function wc_cost_save_product( $product_id ) {

 // stop the quick edit interferring as this will stop it saving properly, when a user uses quick edit feature
 if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce'))
 return;

 // If this is a auto save do nothing, we only save when update button is clicked
 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
 return;
 if ( isset( $_POST['cost_price'] ) ) {
 if ( is_numeric( $_POST['cost_price'] ) )
 update_post_meta( $product_id, 'cost_price', $_POST['cost_price'] );
 } else delete_post_meta( $product_id, 'cost_price' );
}

xx

Quickedit should I go that way later:

Expand the WordPress Quick Edit Menu