保存WooCommerce中的自定义文本字段的空值
问题描述:
我正在使用WordPress电子商务网站,并且WooCommerce是所选的购物平台。保存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'或保存以前输入的号码。
我试过应用从similar question的答案,但不能引用得到它的工作。
有没有人知道我在哪里错了吗?
答
尝试通过isset()
更换empty()
作为一个条件,你的挂钩函数的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');
现在这个意志工作
,从“空”到“isset”简单的改变,似乎已经做到了。谢谢@LoicTheAztec :-) – Craig