DEV Community

owais alam
owais alam

Posted on

Add Custom Stock Numbers to WooCommerce

It often happens that stock is often distributed between stockrooms and production floors. The problem is that tracking stock that is distributed across two locations.

Surprisingly, there is no built in solution for tracking distributed stock. I decided to create a custom solution that would involve custom fields. There will be two custom ‘factory stock’ field and the ‘total stock’.

The total stock field will contain the sum of the factory stock and the WooCommerce built in stock control. This is an easy way of tracking the total stock numbers even when they are actually distributed across two locations.

At the moment, this solution works for Simple Products but it could be expended to cover Variable Product as well. To make things even better, I have added two columns for these stock levels to the product list columns.

Add the following code to the functions.php 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);

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">';

'id' => '_factory_inventory',
'label' => __( 'Factory Inventory', 'woocommerce' ),
'description' => __( 'Please Enter Your Factory Count.', 'woocommerce' ),
'type' => 'number',
// Number Field
'id' => '_total_inventory',
'label' => __( 'Total Inventory', 'woocommerce' ),
'placeholder' => "",
'description' => __( 'Stock Must Match.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0',
echo '</div>';

function woo_add_custom_general_fields_save( $post_id ){

$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 );
Enter fullscreen mode Exit fullscreen mode

In this article, I discussed how you could track stock levels that are distributed across two locations. I also added two columns in the Product List columns so that you could easily view the two stock levels.

Top comments (1)