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:

 

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.

 

 

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

        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