In this post I’ll show you how to add a very simple cost price field to your products in Woocommerce. The aim of this is to show admin users the cost price of a product, so they don’t sell it at a loss.
The code is adapted from a post here by Gerhard Potgieter
We add a field to the interface in the general tab pricing section like so:
add_action( 'woocommerce_product_options_pricing', 'wc_cost_product_field' ); function wc_cost_product_field() { woocommerce_wp_text_input( array( 'id' => 'cost_price', 'class' => 'wc_input_price short', 'label' => __( 'Cost Price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) ); }
We then need to be able to save the new cost price field like so:
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' ); }
Just add these to snippets of code to your theme’s functions.php file and you will have a simple cost price field in your Woocommerce products.
thanks, code works great , but it only displays the cost field for simple products, not as a field within variations … is this just for simple products?
hi Romie, yes this is only for simple products. Maybe in the future I’ll write on adding to product variations. But in the meantime Woocommerce developer Remi has a good tutorial on this: http://www.remicorson.com/woocommerce-custom-fields-for-variations/
Great code !
Any way to add it to “Quick Edit” ?
and to “screen options” in order to see it on the products list ?
Last question : Can I show it only to administrators and not to shop managers ?
Thanks !
hi Dani,
thanks for your question, yes indeed all this is possible. I don’t have time to do it myself at the moment unfortunately. But let me know how you get on.
It worked first time. Exactly what was needed, great job : )
good stuff ! glad it helped you.
Would you be kind enough to point exactly where to plug this code into, within the functions.php file please? I’m using WP ALL IMPORT and though I have the cost price enabled through BOOSTER, it is not a visible option in WP ALL IMPORT.
I’m hoping that this will solve that issue.
hi JKK,
It should be fine if you just drop it in at the end of the functions file. I’ve never used WP ALL IMPORT, but it’s possible there’s something interfering I’d try putting later priorities on the add_action calls in the code above this kind of thing (note the 99999) : add_action( ‘save_post’, ‘wc_cost_save_product’ ,99999);
If that doesn’t help, I don’t know what it could be without actually investigating it (which unfortunately won’t be possible due to current high workload of client work).
Have you figured out how to import the cost through an import of a CSV file?
Thanks,
Jeff
Thanks for this. I had to remove the is_numeric check to accept 5,00 entries instead of 5.00. Using this for a Dutch website, where the decimal character is usual a comma, instead of a period.
Good stuff Jelle, I’m glad you found the code useful.
I ran into the same but changed the line to:
`if ( is_numeric( $_POST[‘cost_price’] ) OR number_format ( $_POST[‘cost_price’] , 2 , “,” , “.” ) )`
This way it accepts both the format with the . as decimal separator (5.00) AND the format with the , as decimal separator (5,00).
Good stuff, Steven
hi your code works great, can it be used to show the cost in the new order details page in woocommerce
so that we see the cost when a new order comes in.
any help as iam not that good with code
hi Wayne,
That’s great that it’s proved useful for you 🙂 Unfortunately the improvement you suggest, is not something I have time to look at currently due to client workload.
Hi,
Great post and very easy to implement
Keep up the good work, it’s appreciated.
thanks Festus, glad you found it useful.
Great Code. Been looking for something like but not exactly. I am developing a multi-vendor WordPress woocommerce food marketplace. The client would like to add Takeout pack fee per restaurant.
thanks, glad you found it useful 🙂
hi possible if we can show the cost price in single product page and shop page?
May this field be populated automatically with a kind of fee like 5% from product price and then read only?
hi Adrian
You could certainly do something like that with it if needed.
Great snippet! Is there a way of displaying the custom price on front end?
I copy this code and paste on function.php file. I see a cost price field on my dashboard but it is not showing my website. how show this field my website on single product post. please help me. thanks
You don’t want the customer to see your cost of goods!
Jeff
Hi,
I have entered the code into my word press theme.
However i can now no longer get rid of the code and it wont delete and resave back to orgional code.
Can you please advise how i delete this without issue.
thank you.
This is not working when the product has variations
good point Sebastian the website I did this for doesn’t have variations, but I can revisit it at some stage to amend it ( probably won’t be any time soon unforunately).
FOR THIS TO WORK FOR VARIATIONS USE THIS CODE:
add_action( ‘woocommerce_product_after_variable_attributes’, ‘variation_settings_fields’, 10, 3 );
// Save Variation Settings
add_action( ‘woocommerce_save_product_variation’, ‘save_variation_settings_fields’, 10, 2 );
function variation_settings_fields( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input(
array(
‘id’ => ‘cost_price[‘ . $variation->ID . ‘]’,
‘label’ => __( ‘Cost Price’, ‘woocommerce’ ),
‘desc_tip’ => ‘true’,
‘description’ => __( ‘Vendos koston’, ‘woocommerce’ ),
‘value’ => get_post_meta( $variation->ID, ‘cost_price’, true ),
‘custom_attributes’ => array(
‘step’ => ‘any’,
‘min’ => ‘0’
)
)
);
}
function save_variation_settings_fields( $post_id ) {
// Number Field
$number_field = $_POST[‘cost_price’][ $post_id ];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, ‘cost_price’, esc_attr( $number_field ) );
}
}
How to display cost price on product page in admin panel?