最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Set a specific default stock quantity on all WooCommerce new products

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Sep 7, 2019 at 11:44 LoicTheAztec 3,39117 silver badges24 bronze badges asked Sep 6, 2019 at 13:47 Carlos RodríguezCarlos Rodríguez 1261 silver badge7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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 );
发布评论

评论列表(0)

  1. 暂无评论