Woocommerce custom fields won't update when I leave them empty and still displays empty fields

旧街凉风 提交于 2019-12-06 08:24:26

Your save function should be like

function woo_add_custom_general_fields_save( $post_id ){

// Customer text ISBN Field
$woocommerce_text_field = $_POST['_ISBN_field'];
if( !empty( $woocommerce_text_field ) )
    update_post_meta( $post_id, '_ISBN_field', esc_attr( $woocommerce_text_field ) );
else
    update_post_meta( $post_id, '_ISBN_field', '' );
}

If !empty( $woocommerce_text_field ) returns true only if $_POST['_ISBN_field'] has some value so the post meta is not updated if $_POST['_ISBN_field'] is empty

what does:

var_dump( get_post_meta( $post->ID, '_ISBN_field' , true) );

return?

i guess the problem ist that the field still contains some value even it's empty.. check that var_dump and than adjust your if statement

and i guess the statement should be like:

if ( get_post_meta( $post->ID, '_ISBN_field', true ) != '' ) {

Try this:

<?php
// Display Custom Field Value
$ISBN_field = get_post_meta($post->ID, '_ISBN_field', true);
if( !empty( $ISBN_field ) ){
  echo '<b>ISBN10: </b>'.$ISBN_field;
} ?>

Regards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!