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.

 

 

30 thoughts on “Woocommerce: How to add a simple cost price field to products”

  1. 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?

    Reply
  2. 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 !

    Reply
    • 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.

      Reply
  3. 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.

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

      Reply
  4. 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.

    Reply
  5. 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

    Reply
    • 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.

      Reply
  6. 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.

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

    Reply
  8. 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.

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

      Reply
  9. 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 ) );
    }

    }

    Reply

Leave a Comment