colombo design studio logo

We design great products

Product design is what we do. We take an idea, some functionality, a brand identity and a lot of creativity and we bring it all together to make real products that have real purpose.

September 3, 2015 By adminCDS

Adding custom stock numbers to WooCommerce

We have been helping our friends at Midaya Ceramic out with their new shop in Colombo, and one request we had was to do with managing their inventory between the shop and the factory.

We suggested using the free WooCommerce platform together with WordPress so that they had a free online solution that can be accessed anywhere. This also means that down the line they will have a fully populated online shop platform that they can put live to potential online consumers as and when they’re ready.

However, we cam across a complication which was that they want to be able to manage their stock inventory in two locations: the shop and the factory. We did some searching, but couldn’t find a simple, free solution to this problem, so we decided to use some custom fields instead. The result is a custom ‘factory stock’ field that can be filled in for any product, and a ‘total stock’ field that takes the sum of the ‘factory stock’ and the WooCommerce built in stock control. Wince products from the factory will largely be sold as builk orders, and we’re more interested in seeing the performance of the shop, we can use the built in store functionality to control sales and inventory in the shop itself.


Currently this only works for Simple Products, but we don’t think it would be particularly difficult to expand into variable products.

As a bonus we added the stock levels to the product list columns for a ‘quick look’ feature.



Here is the code from the functions file:


add_action( ‘wp_enqueue_scripts’, ‘theme_enqueue_styles’ );
function theme_enqueue_styles() {
wp_enqueue_style( ‘parent-style’, get_template_directory_uri() . ‘/style.css’ );

add_action(‘admin_head’, ‘my_custom_fonts’);

function my_custom_fonts() {
echo ‘<style>
._total_inventory_field {
color: red;
._total_inventory_field input {
color: #fff;
add_action(‘save_post’, ‘myWoo_savePost’, 10, 2);

function myWoo_savePost($postID, $post) {
if (isset($post->post_type) && $post->post_type == ‘product’) {
$shopstock = get_post_meta($post->ID, ‘_stock’, true);
$factorystock = get_post_meta($post->ID, ‘_factory_inventory’, true);
$totalstock = $shopstock + $factorystock;
update_post_meta($post->ID, ‘_manage_stock’, ‘yes’);
update_post_meta($post->ID, ‘_total_inventory’, $totalstock);
// Display Fields
add_action( ‘woocommerce_product_options_inventory_product_data’, ‘woo_add_custom_general_fields’ );

// Save Fields
add_action( ‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’ );

function woo_add_custom_general_fields() {

global $woocommerce, $post;

echo ‘<div class=”options_group”>’;

// Custom fields will be created here…

// Number Field
// woocommerce_wp_text_input(
// array(
// ‘id’ => ‘_shop_inventory’,
// ‘label’ => __( ‘Shop Inventory’, ‘woocommerce’ ),
// ‘placeholder’ => ”,
// ‘description’ => __( ‘Enter the shop inventory count here.’, ‘woocommerce’ ),
// ‘type’ => ‘number’,
// ‘custom_attributes’ => array(
// ‘step’ => ‘any’,
// ‘min’ => ‘0’
// )
// )
// );
// Number Field
‘id’ => ‘_factory_inventory’,
‘label’ => __( ‘Factory Inventory’, ‘woocommerce’ ),
‘placeholder’ => ”,
‘description’ => __( ‘Enter the factory inventory count here.’, ‘woocommerce’ ),
‘type’ => ‘number’,

// Number Field
‘id’ => ‘_total_inventory’,
‘label’ => __( ‘Total Inventory’, ‘woocommerce’ ),
‘placeholder’ => ”,
‘description’ => __( ‘This should match the Total Stock above after sales.’, ‘woocommerce’ ),
‘type’ => ‘number’,
‘custom_attributes’ => array(
‘step’ => ‘any’,
‘min’ => ‘0’,
echo ‘</div>’;

function woo_add_custom_general_fields_save( $post_id ){
// Shop Inventory Field
//$woocommerce_number_field = $_POST[‘_shop_inventory’];
//if( !empty( $woocommerce_number_field ) )
// update_post_meta( $post_id, ‘_shop_inventory’, esc_attr( $woocommerce_number_field ) );
// Factory Inventory Field
$woocommerce_number_field = $_POST[‘_factory_inventory’];
if( !empty( $woocommerce_number_field ) )
update_post_meta( $post_id, ‘_factory_inventory’, esc_attr( $woocommerce_number_field ) );

function add_custom_stock_values( $column ) {
$column[‘factory_stock’] = ‘Factory Stock’;
$column[‘total_stock’] = ‘Total Stock’;

return $column;
add_filter( ‘manage_product_posts_columns’, ‘add_custom_stock_values’ );

function add_value_to_custom_stock( $column_name, $post_id ) {

$custom_fields = get_post_custom( $post_id );

switch ($column_name) {
case ‘factory_stock’ :
echo $custom_fields[‘_factory_inventory’][0];

case ‘total_stock’ :
echo $custom_fields[‘_total_inventory’][0];


add_action( ‘manage_product_posts_custom_column’, ‘add_value_to_custom_stock’, 10, 2 );

//quick edit custom fields