从 WooCommerce 中的自定义文本字段保存空值

Save an empty value from a custom Text field in WooCommerce

我正在开发一个 WordPress 电子商务网站,WooCommerce 是我选择的购物平台。

我通过将以下代码插入 functions.php 文件,在 WooCommerce 产品仪表板中创建了一个自定义字段:

function product_custom_fields_add(){
echo '<div class="product_custom_field">';
    // Minimum Required Custom Letters
    woocommerce_wp_text_input(
        array(
            'id'        => '_minimum_engrave_text_option',
            'name'      => '_minimum_engrave_text_option',
            'desc'      =>  __('set custom minimum Lettering text field', 'woocommerce'),
            'label'     => __('Minimum Letters', 'woocommerce'),
            'desc_tip'  => 'true'
        )
    );
    echo '</div>';
}
add_action('woocommerce_product_options_advanced', 'product_custom_fields_add');

为了保存这些值,我在 functions.php 文件中输入了以下代码:

// Save Minimum Required Custom Letters
function woocommerce_product_custom_fields_save1($post_id){
    if ( ! empty( $_POST['_minimum_engrave_text_option'] ) )
        update_post_meta($post_id, '_minimum_engrave_text_option', esc_attr( $_POST['_minimum_engrave_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );

当在自定义字段中输入值时,以上代码有效。我的问题是我无法成功保存空白值。每当我保存产品时,自定义字段要么自动用“1”填充字段,要么保存之前输入的数字。

我尝试应用来自 的答案,但无法引用使其生效。

有谁知道我哪里出错了?

尝试将 empty() 替换为 isset() 作为挂钩函数的 if 语句中的条件:

// Save Minimum Required Custom Letters
function woocommerce_product_custom_fields_save1($post_id){
    if ( isset( $_POST['_minimum_engrave_text_option'] ) )
        update_post_meta($post_id, '_minimum_engrave_text_option', esc_attr( $_POST['_minimum_engrave_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );

现在可以使用了