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.

woo-commerce-custom-inventory-multiple-locations

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.

add-custom-value-to-woocomerce-product-list

 

Here is the code from the functions file:

<?php

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 {
background-color:red;
color: #fff;
}
</style>’;
}
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);
}
}
//http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
//http://stackoverflow.com/questions/15908411/get-woocommerce-to-manage-stock-as-a-default
// 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
woocommerce_wp_text_input(
array(
‘id’ => ‘_factory_inventory’,
‘label’ => __( ‘Factory Inventory’, ‘woocommerce’ ),
‘placeholder’ => ”,
‘description’ => __( ‘Enter the factory inventory count here.’, ‘woocommerce’ ),
‘type’ => ‘number’,

)
);
// Number Field
woocommerce_wp_text_input(
array(
‘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 ) );
}

//http://tareq.wedevs.com/2011/07/add-your-custom-columns-to-wordpress-admin-panel-tables/
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];
break;

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

default:
}
}

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

//quick edit custom fields
//http://wordpress.stackexchange.com/questions/3316/how-to-show-a-custom-meta-box-on-the-quick-edit-screen