Woocommerce: How to add a simple cost price field to products

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:

 

 

We then need to be able to save the new cost price field like so:

 

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.

 

 

14 Comments

  • Romie says:

    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?

  • louie171 says:

    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/

  • Dani says:

    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 !

    • louie171 says:

      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.

  • Nick Hart says:

    It worked first time. Exactly what was needed, great job : )

  • JKK says:

    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.

    • louie171 says:

      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).

  • Jelle says:

    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.

    • louie171 says:

      Good stuff Jelle, I’m glad you found the code useful.

      • Steven says:

        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).

  • wayne hibbs says:

    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

    • louie171 says:

      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.

Leave a Reply