簡體   English   中英

WooCommerce 產品變化:自動啟用管理庫存和設置庫存數量

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

在管理員 Woocommerce 產品頁面上,對於可變產品,在“變體”設置上,對於所有產品變體,我希望默認啟用Manage Stock選項,並將Stock Quantity選項設置為1

使用Get Woocommerce 管理庫存作為默認答案代碼,我可以為默認庫存選項卡(對於父變量產品)執行此操作。

我什至嘗試過Woocommerce:創建變體 - 管理庫存檢查默認答案代碼,但它對我不起作用。

任何幫助將不勝感激。

有關信息, woocommerce_product_variation_get_manage_stock鈎子不再存在。

基於 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;
}

代碼進入活動子主題(或活動主題)的functions.php文件。 測試和工作。

如果您只想檢查,變體管理庫存並且不想更改庫存數量,同樣如果您希望在沒有檢查產品級別管理庫存的情況下在變體級別進行編輯

以下是代碼(@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