1

Context:

I'm making a WooCommerce/Dokan marketplace for people to sell second hand products. As this are second hand products each product should have an inventory of just 1. All products are simple products, no variations.

Problem

I want to set by default a stock of 1 to all products. I don't want the users dealing with setting the stock quantity by themselves.

What I've tried

  • I've done a lot of research and haven't found a solution, I came across this online tutorial:
    "Set a Default Stock Quantity for WooCommerce Products" that is what I'm looking for but, the code doesn't work anymore and produces the infamous white screen of death.
  • I found some code to add to my child theme functions.php that sets the quantity value to 1 in the product page, it works but it isn't a complete solution because if someone buys a product the stock won't be reduced to zero and the product won't be hidden in the front-end.

What I would like to have

The ideal solution would be code to add to my child theme functions.php that sets stock quantity to 1 by default in all new published products. Of course I'm open to other solutions and plugins but the main goal is that this quantity is set automatically to all new products.

Notes

  • I'm not an experienced developer, I'm self thought, a designer transitioning, that's why I'm looking for help.
  • I'm not lazy, I've done a lot of research in the past few days and tried many things but haven't found a solution.

Any help you can provide is appreciated.

1

For "New" simple products on admin, the following embedded jQuery script will:

  • trigger and enable "Stock management" at product level,
  • set the "Stock quantity" to "1",
  • set the "Low stock threshold" to "0".

The code:

add_action( 'admin_footer', 'new_product_stock_default_setting' );
function new_product_stock_default_setting() {
    global $pagenow, $post_type;

    // Targeting new product page (admin)
    if( $pagenow === 'post-new.php' && $post_type === 'product' ) :
    ?>
    <script>
    jQuery(function($){
        var a = '#inventory_product_data input#';

        // Set Manage Stock and trigger event to show other related fields
        $(a+'_manage_stock').prop('checked', true).change();
        $(a+'_stock').val('1'); // Set Stock quantity to "1"
        $(a+'_low_stock_amount').val('0'); // Set Low stock threshold to "0"
    });
    </script>
    <?php
    endif;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

enter image description here

| improve this answer | |
  • Thanks @LoicTheAztec for taking the time. I tried using that code in my child theme functions.php but unfortunately it didn't work for me. After creating a new product the inventory isn't managed and set to 1, check this screenshot: snipboard.io/6WvKut.jpg Any idea on what could be happening? could it be related to not filling the SKU field? I'm using WooCommerce 3.7 by the way. – Carlos Rodríguez Sep 7 '19 at 13:29
  • @CarlosRodríguez I have tested that on WooCommerce 3.7 and it works perfectly when you click on "add new" in WooCommerce Product section (Admin). It either works in all woocommerce versions since WooCommerce 3 at least. So there is something else that is making trouble in your installation. – LoicTheAztec Sep 7 '19 at 15:37
0

I believe in WooC the product stock data is saved as post meta with the key of _stock. If this is the case, then you can set the stock level just like any other post meta by hooking a custom function to save_post action.

In you custom function you need to check that you're dealing with a new post and the right post type. This will overwrite any user submitted stock level data.

function save_product_meta( $post_id, $post, $update ) {
  // only new products
  if ( ! $update && 'product' === $post->post_type ) {
    update_post_meta( $post_id, '_stock', 1 ); // stock count
    update_post_meta( $post_id, '_stock_status', 'instock' ); // stock status
  }
}
add_action( 'save_post', 'save_product_meta', 10, 3 );

Please test and tweak the example above to suit your needs in a development environment before using it in a production site.

EDIT

You could also use the CPT specific save action,

function save_product_meta( $post_id, $post, $update ) {
  // only new products
  if ( ! $update ) {
    update_post_meta( $post_id, '_stock', 1 ); // stock count
    update_post_meta( $post_id, '_stock_status', 'instock' ); // stock status
  }
}
add_action( 'save_post_product', 'save_product_meta', 10, 3 );
| improve this answer | |
  • Thanks for taking the time @antti-koskinen but it didn't work. – Carlos Rodríguez Sep 7 '19 at 13:32
  • @CarlosRodríguez I'm sorry, there was a silly mistake in my code example. It was missing $post->post_type from the if statement. If you try the code again, it should work now. – Antti Koskinen Sep 7 '19 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.