我想“管理库存”在woocommerce中默认选中,只要创建一个新的变体(一个孩子被添加到.woocommerce_variations.wc-metaboxes. ui-sortable),但我的代码不值得
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 ($) {
// Function to handle changes in the DOM
function handleDOMChanges(mutationsList, observer) {
$('#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);
}
});
});
}
// Create a MutationObserver
const targetNode = document.querySelector('.woocommerce_variations.wc-metaboxes.ui-sortable');
const config = { childList: true, subtree: true };
const observer = new MutationObserver(handleDOMChanges);
// Start observing changes in the DOM
observer.observe(targetNode, config);
});
</script><?php
endif;
}
字符串
我已经尝试了stackoverflow中关于这个特定问题的所有帖子。
1条答案
按热度按时间qv7cva1a1#
这里是一个脚本,我已经测试和工程
字符串