Add Purchase Price for Cost of Goods to WooCommerce Products

Add this code to your child themes functions file to add a custom field for the products purchase price for cost of goods.


add_action( 'woocommerce_product_options_general_product_data', 'add_purchase_price_field' );function add_purchase_price_field() {
    woocommerce_wp_text_input( array(
        'id'          => '_purchase_price',
        'label'       => __( 'Purchase Price', 'woocommerce' ),
        'description' => __( 'Enter the purchase price of the product.', 'woocommerce' ),
        'desc_tip'    => true,
        'type'        => 'number',
        'custom_attributes' => array(
            'step' => '0.01',
            'min'  => '0'
        ),
    ));
}

add_action( 'woocommerce_admin_process_product_object', 'save_purchase_price_field' );
function save_purchase_price_field( $product ) {
    if ( isset( $_POST['_purchase_price'] ) ) {
        $product->update_meta_data( '_purchase_price', sanitize_text_field( $_POST['_purchase_price'] ) );
    }
}

Here’s what the code produces :

This is handy if you need to work out profit margins and different tax rates for products.

Leave a Comment