I added a custom one page product box for woocommerce to show the ISBN number for books that I sell. I found a good guide and managed to add whatever I want. However, when I empty the custom field for ISBN, it will not be empty on the site.
I have the following code in functions.php
// Display Fields add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' ); // Save Fields add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' ); function woo_add_custom_general_fields() { global $woocommerce, $post; echo '<div class="options_group">'; // Custom fields will be created here... // Text Field woocommerce_wp_text_input( array( 'id' => '_ISBN_field', 'label' => __( 'ISBN', 'woocommerce' ), 'placeholder' => '', 'desc_tip' => 'true', 'description' => __( 'ISBN.', 'woocommerce' ) ) ); 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 ) ); }
Then in short-description.php I made it appear on the product page. However, it still displays the name ISBN10: if it is an empty field.
<?php // Display Custom Field Value if (!((get_post_meta($post->ID, '_ISBN_field', true))==")) { //Not empty echo '<b>ISBN10: </b>',get_post_meta( $post->ID, '_ISBN_field' , true); } ?>
So, two problems: I cannot edit the product to contain an empty custom field. And if the field is empty (possible only when the field previously did not contain data), it still displays the field name.
Thanks in advance.
source share