WooCommerce

Here’s a quick little snippet that you can make use of if you want to set a default stock quantity for any newly added WooCommerce product Variations. This can be useful when bulk-adding variations and you don’t want an initial quantity of ‘0’.

add_action( 'admin_enqueue_scripts', 'wc_default_variation_stock_quantity' );

function wc_default_variation_stock_quantity() {
  global $pagenow, $woocommerce;

  $default_stock_quantity = 0;
  $screen = get_current_screen();

  if ( ( $pagenow == 'post-new.php' || $pagenow == 'post.php' || $pagenow == 'edit.php' ) && $screen->post_type == 'product' ) {

    ob_start();
    ?>
    $('.woocommerce_variations').bind('woocommerce_variations_added',function() {
      $('.woocommerce_variations input').each(function(index,el) {
        el = $(el);
        if(el.attr('name') && el.attr('name').substr(0,14) == 'variable_stock' && el.val() == '') {
          el.val(<?php echo $default_stock_quantity; ?>);
        }
      });
    });
    <?php
    $javascript = ob_get_clean();
    $woocommerce->add_inline_js( $javascript );
  }
}

Or, if you need a default stock quantity for simple products:

add_action( 'admin_enqueue_scripts', 'wc_default_variation_stock_quantity' );

function wc_default_variation_stock_quantity() {
  global $pagenow, $woocommerce;

  $default_stock_quantity = 0;
  $screen = get_current_screen();

  if ( ( $pagenow == 'post-new.php' || $pagenow == 'post.php' || $pagenow == 'edit.php' ) && $screen->post_type == 'product' ) {

    ob_start();
    ?>
    if ( '' === $( '#_stock' ).val() ) {
      $( '#_stock' ).val(<?php echo $default_stock_quantity; ?>);
    }
    <?php
    $javascript = ob_get_clean();
    $woocommerce->add_inline_js( $javascript );
  }
}

Published by Justin Stern

Justin is one of our co-founders, and is our resident overengineer. He likes to write developer tutorials and make black magic happen in our plugins. He thinks that writing code is a lot easier than writing words.

14 Comments

  1. ok cool so how do I actually implement this? I tried going into editor but then im sort of aloof. I don’t want to keep trying to copy/paste all day into the guessed php file. I’ll paste it right under the beggining on the main portion if that is where it can work and not break functionality, but I’m just trying to automate my process with this awesome looking snippet. Thanks for any feedback in advance.

    ajw

    • You’ll want to add this to your theme’s functions.php and it should work just fine 🙂

  2. I tried adding this into my child theme’s functions.php (and I tried the parent theme’s functions.php as well) in the section marked as “You can add custom functions below” and I changed the $default_stock_quantity to “1” but when I go into Add Products and I checked the Manage Stock box, the Stock Qty still shows blank. I am using ‘Simple Product’ and not Variable… does that matter?

    The site is for a consignment store where everything in the store is unique. My hope is to default in a check in the Manage Stock box and the value of 1 in the Stock Qty field for everything we enter. I’m relatively new at php so I’m open to any suggestions. Here’s my functions.php page:

    • Hey Johnny, thanks for the question, that’s pretty interesting. I’ve added an equivalent section for simple products to the article, give it a try and see if it works out for you

  3. Hey is it possible to make a simple product have a default quality and even default cost?

    Building a stock online ordering system for trade/dealer see what is available and reserve stock woocommerce is great for what I need but if I can set so every product to a default quality of 1 and everything is cost of £0 will save putting the info.

    • Hey Shezzamoo, don’t know whether you still need this, but I’ve added a new section to the article covering the simple product default quantity. You could do this for cost as well very simply by duplicating the javascript part, ie add something like:
      if ( '' === $( '#_regular_price' ).val() ) {
      $( '#_regular_price' ).val(1);
      }

      Hope this helps!

      • I tried to modify your code to make a default variation price, but I had no success.

        I have 7 variations in all my products, and their prices are the same ($3,50). Every product in my site will be like that. Can you help me to write a code that fills all the variation’s prices to $3,50? That would save me a lot of time, because I need to make > 2000 products and I need to change 17000 prices.

        Thank you :-). Sorry for my English, its not my native language.

  4. Very nice, thank u! It’s possible to check automatically the stock management?

  5. Hey Justin,

    I’m looking to set a default quantity based on product type, for example toothpaste could be sold as a pack of 6 products for sale where toothbrushes can be bought in packs of 12. I’ve tried implementing both above examples but nothing seems to change. Any other suggestions?

    • Hi Andrew, this snippet sets a “starting” stock level for a new product, so it won’t influence how products are sold. Since your toothpaste is sold in packs of 6, why not just put that in your product title / description and then a quantity of “1” = 6 toothpastes, 2 = 12 toothpastes, etc. You could have separate products for packs versus single / individual products. If you want to offer a discount for packs, you could also look into this extension: http://www.woocommerce.com/products/product-bundles/

  6. Hi there,

    I have been using this snippet succesfully for some time now, but now it suddently doesn’t work anymore. I am running WP 4.1 and WC 2.2.10. Any ideas/solutions on why it is not working anymore?

    Regards

  7. Hi, how do I make my Stock Quantity field compulsory/required.

    And also a checkbox for showing StockQuantity on detail page or not.

  8. Would it be possible to modify this to reset the stock quantity on products that are currently in the store? I need to weekly reset the quantities of all products back to 0.

Hmm, looks like this article is quite old! Its content may be outdated, so comments are now closed.