user1433479
user1433479

Reputation: 135

Wordpress custom fields hook not working

I'm trying to add more fields to a product on Wordpress (Woocommerce). I have added the html fields like so:

<tr>
    <td><input class="backend_price_accommodatie" name="g/w/l_prijs" type="text" placeholder="g/w/l prijs" width="10"></td>
    <td><input class="backend_price_accommodatie" name="t/t/i_prijs" type="text" placeholder="Telefoon/tv/internet prijs" width="10"></td>
    <td><input class="backend_price_accommodatie" name="heffingen_prijs" type="text" placeholder="Heffingen prijs" width="10"></td>
    <td><input class="backend_price_accommodatie" name="verzekering_prijs" type="text" placeholder="Woonverzekering prijs" width="10"></td>
</tr>

I'm using a hook to save this information to the database but I can't get it to work.

add_action( 'save_post', 'wc_prices_save_product' );
function wc_prices_save_product( $pID ) {
global $globals;
// If this is a auto save do nothing, we only save when update button is clicked
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; }
    add_post_meta( $pID, 'g/w/l_prijs', $_POST['g/w/l_prijs'], true ) || update_post_meta( $pID, 'g/w/l_prijs', $_POST['g/w/l_prijs'] ); 
    add_post_meta( $pID, 't/t/i_prijs', $_POST['t/t/i_prijs'], true ) || update_post_meta( $pID, 't/t/i_prijs', $_POST['t/t/i_prijs'] ); 
    add_post_meta( $pID, 'heffingen_prijs', $_POST['heffingen_prijs'], true ) || update_post_meta( $pID, 'heffingen_prijs', $_POST['heffingen_prijs'] ); 
    add_post_meta( $pID, 'verzekering_prijs', $_POST['verzekering_prijs'], true ) || update_post_meta( $pID, 'verzekering_prijs', $_POST['verzekering_prijs'] );
}
?>

Am I missing something? It seems pretty straightforward but it doesn't seem to save anything. Or it's not showing the saved information in the fields for some reason.

Upvotes: 0

Views: 147

Answers (1)

Domain
Domain

Reputation: 11808

While saving the post, there is no need of add_post_meta(), as update_post_meta() will create meta field if not present or update the existing meta field, if it is present.

For display in dashboard, you need to extract these metafields values and display them.

For example,

In meta box, callback function, where you have added, extra fields

<tr>
    <td><input class="backend_price_accommodatie" name="g/w/l_prijs" type="text" placeholder="g/w/l prijs" width="10"></td>
    <td><input class="backend_price_accommodatie" name="t/t/i_prijs" type="text" placeholder="Telefoon/tv/internet prijs" width="10"></td>
    <td><input class="backend_price_accommodatie" name="heffingen_prijs" type="text" placeholder="Heffingen prijs" width="10"></td>
    <td><input class="backend_price_accommodatie" name="verzekering_prijs" type="text" placeholder="Woonverzekering prijs" width="10"></td>
</tr>

Use the below code,

global $post;

<tr>
    <td><input class="backend_price_accommodatie" name="g/w/l_prijs" type="text" placeholder="g/w/l prijs" width="10" value="<?php echo get_post_meta( $post->ID, 'g/w/l_prijs', true); ?>"></td>

...

</tr>

Upvotes: 1

Related Questions