Woocommerce custom fields won't update when I

2019-05-28 22:46发布

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.

3条回答
ゆ 、 Hurt°
2楼-- · 2019-05-28 23:17

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

查看更多
Summer. ? 凉城
3楼-- · 2019-05-28 23:18

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 ) != '' ) {
查看更多
疯言疯语
4楼-- · 2019-05-28 23:23

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

查看更多
登录 后发表回答