WooCommerce,如果自定义复选框被选中隐藏库存状态

WooComerce, if custom checkbox is checked hide stock status

我在使用 woocomerce 复选框时遇到了一些问题,我使用以下代码将自定义复选框添加到产品页面:

 woocommerce_wp_checkbox( 
array( 
    'id'            => '_checkbox', 
    'wrapper_class' => 'show_if_simple', 
    'label'         => __('My Checkbox Field', 'woocommerce' ), 
    'description'   => __( 'Check me!', 'woocommerce' ) 
    )
);
}

然后用这个保存价值:

    $woocommerce_checkbox = isset( $_POST['_checkbox'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_checkbox', $woocommerce_checkbox );

现在,我尝试编写功能,在检查此复选框时,我的股票状态是Hiden,但我失败了,我可以问大家提供一些支持吗?

如果用于保存复选框选项的代码工作正常并且该产品的保存选项反映在数据库中,那么添加以下代码将有助于完成您的任务

add_filter('woocommerce_stock_html','wdm_remove_stock_html',10,3);

function wdm_remove_stock_html($availability_html, $availability, $product)
{
if ( 'yes' === get_post_meta( $product->id,'_checkbox', true) ) {
    return '';
}else{
 return $availability_html;   
}
}