Managing the cost of goods in your WooCommerce store is crucial for understanding your profit margins and overall business performance. While there are several plugins available to help track the cost of goods, you can achieve this functionality without relying on third-party solutions. In this guide, we’ll show you how to manage WooCommerce cost of goods without a plugin by adding custom code to your theme’s functions.php file.
This approach gives you more control over how your cost data is handled and ensures your store remains lightweight and efficient. If you’re new to modifying your theme files, consider using our free WordPress child theme generator to create a child theme. This will protect your customisations when updating your theme.
Step 1: Adding a Custom Field for Cost of Goods
The first step is to add a custom field to the WooCommerce product editor, allowing you to input the cost of goods directly for each product. Add the following code snippet to your theme’s functions.php file:
/**
* Snippet: How to Manage WooCommerce Cost of Goods Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1451
* Tested with WooCommerce 10.7.0
* "Add a custom cost of goods field in the product editor"
*/
function wcsuccess_add_cost_of_goods_field() {
woocommerce_wp_text_input(
array(
'id' => '_cost_of_goods',
'label' => __('Cost of Goods', 'woocommerce'),
'desc_tip' => true,
'description' => __('Enter the cost of goods for this product.', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => '0.01',
'min' => '0'
)
)
);
}
add_action('woocommerce_product_options_pricing', 'wcsuccess_add_cost_of_goods_field');
This code adds a new input field labeled “Cost of Goods” in the product pricing section of the WooCommerce product editor, allowing you to input the cost directly.
Step 2: Saving the Cost of Goods Data
Next, you’ll need to ensure that the cost of goods entered in the custom field is saved when the product is updated. Add the following code to handle this:
/**
* Snippet: How to Manage WooCommerce Cost of Goods Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1451
* Tested with WooCommerce 10.7.0
* "Save the cost of goods data entered in the product editor"
*/
function wcsuccess_save_cost_of_goods_field($post_id) {
$product = wc_get_product($post_id);
$cost_of_goods = isset($_POST['_cost_of_goods']) ? sanitize_text_field($_POST['_cost_of_goods']) : '';
$product->update_meta_data('_cost_of_goods', $cost_of_goods);
$product->save();
}
add_action('woocommerce_process_product_meta', 'wcsuccess_save_cost_of_goods_field');
This function retrieves the value from the custom field and saves it to the product’s meta data whenever the product is updated.
Step 3: Calculating Profit in WooCommerce Orders
To manage your profit margins, you’ll want to calculate the profit for each order based on the cost of goods. The following code snippet calculates and displays the profit for each product in the order details:
/**
* Snippet: How to Manage WooCommerce Cost of Goods Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1451
* Tested with WooCommerce 10.7.0
* "Calculate and display profit based on cost of goods for each order"
*/
function wcsuccess_calculate_order_profit($order_id) {
$order = wc_get_order($order_id);
$total_profit = 0;
foreach ($order->get_items() as $item_id => $item) {
$product = $item->get_product();
$cost_of_goods = $product->get_meta('_cost_of_goods');
$quantity = $item->get_quantity();
$line_total = $item->get_total();
if ($cost_of_goods) {
$profit = ($line_total - ($cost_of_goods * $quantity));
$total_profit += $profit;
wc_add_order_item_meta($item_id, '_item_profit', wc_price($profit));
}
}
wc_update_order_meta($order_id, '_order_profit', wc_price($total_profit));
}
add_action('woocommerce_checkout_update_order_meta', 'wcsuccess_calculate_order_profit');
This function loops through each product in the order, calculates the profit, and stores it as meta data in the order details. The total profit for the entire order is also calculated and saved.
Step 4: Displaying Profit in the WooCommerce Admin Panel
To make your life easier, you can also display the profit calculation in the WooCommerce admin order list. Add the following code to display the profit column in the order list:
/**
* Snippet: How to Manage WooCommerce Cost of Goods Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1451
* Tested with WooCommerce 10.7.0
* "Add profit column to WooCommerce admin orders list"
*/
function wcsuccess_add_profit_column_to_orders($columns) {
$columns['order_profit'] = __('Profit', 'woocommerce');
return $columns;
}
add_filter('manage_edit-shop_order_columns', 'wcsuccess_add_profit_column_to_orders', 20);
function wcsuccess_show_profit_column_data($column) {
global $post;
if ('order_profit' === $column) {
$order_profit = get_post_meta($post->ID, '_order_profit', true);
echo $order_profit ? $order_profit : '-';
}
}
add_action('manage_shop_order_posts_custom_column', 'wcsuccess_show_profit_column_data', 2);
This code adds a new column labeled “Profit” to the WooCommerce admin orders list and populates it with the calculated profit for each order.
Conclusion
By following these steps, you can manage WooCommerce cost of goods without a plugin, giving you full control over how you track and calculate your store’s profit margins. This method is lightweight and integrates seamlessly with WooCommerce, providing you with crucial business insights without the need for additional plugins.
Remember to use our free WordPress child theme generator to create a child theme before editing your functions.php file. This ensures that your customisations are safe during theme updates.
For further customisations, you might find our other tools helpful, such as the wp-config file generator, .htaccess file generator, robots.txt generator, and the bad bot blocker generator.
By managing the WooCommerce cost of goods without a plugin, you keep your site lean while gaining valuable insights into your business’s profitability. Happy coding!
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

