Get Woocommerce to Manage Stock as a default

Is there a hook that would allow setting the Inventory > Manage Stock checkbox in woocommerce globally?

All my products are single item quantity, so it seems pretty counter-D.R.Y to always have to remember to check it, especially for other employees.

Related posts

Leave a Reply

3 comments

  1. Although this is quite late, since you asked about a hook: although there doesn’t appear to be a hook, action or function specifically for turning on the Manage Stock option, you can still hook into the post save function and auto-enable it that way, since it is just a post_meta value:

    add_action('save_post', 'myWoo_savePost', 10, 2);
    
    function myWoo_savePost($postID, $post) {
        if (isset($post->post_type) && $post->post_type == 'product') {
    
            update_post_meta($post->ID, '_manage_stock', 'yes');
        }
    }
    

    Note that stock levels will always default to 0, so you may also want to add the line:

    update_post_meta($post->ID, '_stock', '1');
    

    …which will update your stock quantity to 1. Do be aware that this will happen every time a product is saved, though.

    I’m unsure as to whether this has knock-on effects elsewhere in WooCommerce for larger stock quantities, but as you’re dealing with single-quantity items, I’d guess you’re probably okay.

    Update (with $update):

    As of WordPress 3.7, a third parameter was added to save_post so you can easily tell if it’s a new post being created or an existing post being updated. As such, you can fire the function above only when creating a new post (which is arguably the desired effect):

    add_action('save_post_product', 'myWoo_savePost', 10, 3);
    
    function myWoo_savePost($postID, $post, $update) {
        if (!$update) {
            //  $update is false if we're creating a new post
            update_post_meta($post->ID, '_manage_stock', 'yes');
            update_post_meta($post->ID, '_stock', '1');
        }
    }
    

    (Thanks to Dylan for the reminder about post-type specific saves)

  2. A little late, but here’s how for anyone else needing to do this… I found most of this code somewhere else, but I don’t have the link anymore to where I found it. Tweaked it a little and this is what I ended up with (add to functions.php):

    add_action( 'admin_enqueue_scripts', 'wc_default_variation_stock_quantity' );
    function wc_default_variation_stock_quantity() {
      global $pagenow, $woocommerce;
    
      $default_stock_quantity = 1;
      $screen = get_current_screen();
    
      if ( ( $pagenow == 'post-new.php' || $pagenow == 'post.php' || $pagenow == 'edit.php' ) && $screen->post_type == 'product' ) {
    
        ?>
    <!-- uncomment this if jquery if it hasn't been included
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    -->
        <script type="text/javascript">
        jQuery(document).ready(function(){
            if ( !jQuery( '#_manage_stock' ).attr('checked') ) {
              jQuery( '#_manage_stock' ).attr('checked', 'checked');
            }
            if ( '' === jQuery( '#_stock' ).val() ) {
              jQuery( '#_stock' ).val(<?php echo $default_stock_quantity; ?>);
            }
        });
        </script>
        <?php
      }
    }
    
  3. Woocommerce now has it’s own hook for saving a product. For a simple product it’s woocommerce_process_product_meta_simple.

    Before updating the meta, we should first check that the _manage_stock is empty i.e. the checkbox has not been checked, so that it only triggers on products which have not already been set.

    Then toggle the manage stock and set the default stock number.

    function manage_stock_default( $post_id ) {
        if (empty($_POST['_manage_stock'])) {
          update_post_meta($post_id, '_manage_stock', 'yes');
          update_post_meta($post_id, '_stock', '1');
        }
    }
    add_action( 'woocommerce_process_product_meta_simple', 'manage_stock_default');