• I added a custom field to a single product page for woocommerce in order to show ISBN number for the books I sell. I found a nice guide and managed to add everything as I want. However when I empty the custom field for ISBN it won’t go empty on the site.

    I have the following code in the 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 the short-description.php I made it so that it shows on the product page. However it still displays the name ISBN10: if it’s 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 the two problems are I can’t edit the product to contain an empty custom field. And if the field is empty (only possible when field hasn’t been previously contained data) it still displays the field name.

    Thanks in advance.

    https://wordpress.org/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @lemoi, you can update last part of code for Display Custom Field Value become:

    <?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;
        }
    ?>
    Thread Starter LeMoi

    (@lemoi)

    Thanks @terrytsang that seems to work nicely! However now I still have the problem left that whenever I fill in a field value in the product edit admin area, update it, remove the value, update once again. It remains there. So it doesn’t actually delete the value from whereever it gets it from. Really strange. But probably has something to do with:

    function woo_add_custom_general_fields_save( $post_id ){}

    No idea what it could be though 🙁

    Thank you @terrytsang!!

    Anyway, I have the same problem as @lemoi and I have no idea how to fix it, I can not edit the custom field and always shows the same data whatever you do.

    There is another problem .. when the custom field is empty, this field does not appear on the product page …. great! But this hidden field causes a line break between custom fields and that is not nice … I hope you can help us solve these problems!

    Regards

    Thread Starter LeMoi

    (@lemoi)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Woocommerce custom fields won't update when I leave them empty and still display’ is closed to new replies.