简体   繁体   English

WooCommerce 产品变化:自动启用管理库存和设置库存数量

[英]WooCommerce product variations: Auto enable Manage Stock and set stock quantity

On Admin Woocommerce product pages, for variable products, on "Variations" settings, for all product variations, I would like to have Manage Stock option enabled by default with the Stock Quantity option set to 1 .在管理员 Woocommerce 产品页面上,对于可变产品,在“变体”设置上,对于所有产品变体,我希望默认启用Manage Stock选项,并将Stock Quantity选项设置为1

Using Get Woocommerce to Manage Stock as a default answer code, I am able to do it for the default inventory tab (for the parent variable product) .使用Get Woocommerce 管理库存作为默认答案代码,我可以为默认库存选项卡(对于父变量产品)执行此操作。

I even tried Woocommerce: Creating variations - Manage stock checked by default answer code, but it didn't worked form me.我什至尝试过Woocommerce:创建变体 - 管理库存检查默认答案代码,但它对我不起作用。

Any help will be appreciated.任何帮助将不胜感激。

For info, woocommerce_product_variation_get_manage_stock hook doesn't exist anymore.有关信息, woocommerce_product_variation_get_manage_stock钩子不再存在。

Based on jQuery code, the following will enable Manage Stock and will set Stock Quantity to 1 , for all product variations:基于 jQuery 代码,以下将启用Manage Stock并将Stock Quantity设置为1 ,适用于所有产品变体:

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

    if ( 'product' === $post_type ) :
    ?><script type='text/javascript'>
    jQuery( function($) {
        $('#variable_product_options').on( 'change', function(){
            $('input.variable_manage_stock').each( function(){
                if( ! $(this).is(':checked') ){
                    $(this).attr('checked', 'checked').closest('div').find('.wc_input_stock').val(1);
                }
            });
        });
    });
    </script><?php
    endif;
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。

In case if you only want to checked, variation manage stock and do not want to change stock quantity, similarly if you want that it should be edited on variation level in case product level manage stock is no checked如果您只想检查,变体管理库存并且不想更改库存数量,同样如果您希望在没有检查产品级别管理库存的情况下在变体级别进行编辑

Following is the code (Edited version of above posted answer by @LoicTheAztec )以下是代码(@LoicTheAztec 发布的上述答案的编辑版本)

/* Manage Stock Checked By Default On New Product */
if (jQuery("body").hasClass("post-new-php") && jQuery("body").hasClass("post-type-product")) {
 //By default check product level manage stock  
 jQuery("#_manage_stock").prop('checked',true);

 jQuery('#variable_product_options').on( 'change', function(){
    //condition in case product level manage stock is checked 
   if(jQuery("#_manage_stock").prop('checked') == true){
        jQuery('input.variable_manage_stock').each( function(){
            if( ! jQuery(this).is(':checked') ){
                jQuery(this).attr('checked', 'checked');
            }
        });
      }
  });
}
/* END:: Manage Stock Checked By Default On New Product */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM