从WooCommerce中的自定义文本字段中保存一个空值 [英] Save an empty value from a custom Text field in WooCommerce

查看:60
本文介绍了从WooCommerce中的自定义文本字段中保存一个空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在WordPress电子商务网站上工作,WooCommerce被选为购物平台。

I am working on a WordPress eCommerce website, with WooCommerce being the chosen Shopping Platform.

我通过在WooCommerce产品信息中心内插入将以下代码放入 functions.php 文件:

I have created a Custom Field, within the WooCommerce Product Dashboard by inserting the following code into the functions.php file:

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 文件:

In order to save the values, I have entered the following code into the functions.php file:

// 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或保存先前输入的数字。

The above code works when a value is entered into the Custom Field. My problem is that I am unable to successfully save a Blank Value. Whenever I save a Product, the Custom Field either auto populates the Field with '1' or saves a previously entered number.

我尝试从类似问题,但不能报价使其工作。

I tried applying the answer, from a similar question, but could not quote get it to work.

有人知道我在哪里错吗?

Does anyone know where I am going wrong here?

推荐答案

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

Try to replace empty() by isset() as a condition in the if statement of your hooked function:

// 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' );

现在可以使用

这篇关于从WooCommerce中的自定义文本字段中保存一个空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆