Display, in administration product page, a custom meta filed for the product cost price.
The following code must be added to the function.php file of the theme (better if is a child-theme). Optionally you can create a custom plugin.
/**
* Add cost price column title to admin product list, after the price column
*/
function al_wc_add_column($columns){
$new_columns = array();
foreach( $columns as $key => $column ) {
$new_columns[$key] = $columns[$key];
if( $key === 'price' ) {
$new_columns['cost_price'] = __( 'Cost price','ego-wc-fs');
}
}
return $new_columns;
}
add_filter( 'manage_edit-product_columns', 'al_wc_add_column', 11 );
/**
* Add cost price column data to admin product list, after the price column
*/
function al_wc_product_column( $column, $postid ) {
if ( $column == 'cost_price' ) {
echo get_post_meta( $postid, 'cost_price', true );
}
}
add_action( 'manage_product_posts_custom_column', 'al_wc_product_column', 10, 2 );
/**
* Add the cost price meta data to the admin product page
*/
function al_wc_product_meta_cost_price() {
woocommerce_wp_text_input( array(
'id' => 'cost_price',
'class' => 'wc_input_price short',
'label' => __( 'Cost price', 'ego-wc-fs' ) . ' (' . get_woocommerce_currency_symbol() . ')'
));
}
add_action( 'woocommerce_product_options_pricing', 'al_wc_product_meta_cost_price' );
/**
* Handle the cost price data saving
*/
function al_wc_save_product( $product_id ) {
if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce'))
return;
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' );
}
add_action( 'save_post', 'al_wc_save_product' );