0
votes

I have been working with Wordpress and Woocommerce to add a custom meta field to my admin products page. I have everything working fine except that when I save the product, it does not show the info I entered in my meta box.

I checked the database and the data does indeed save, I saw the data I entered in the value and the meta field name was in the meta key. So that leads me to believe everything is working except showing the value on the admin product page once the product has been saved.

I think that the problem lies within one of these two functions but not certain, it could be in the save function though where it calls the update_post_meta.

here is where I think my problem is:

function variable_fields( $loop, $variation_data ) {
?>  
    <tr>
        <td>
            <div>
                    <label><?php _e( 'My Custom Field', 'woocommerce' ); ?></label>
                    <input type="text" size="5" name="my_custom_field[<?php echo $loop; ?>]" value="<?php echo $variation_data['_my_custom_field'][0]; ?>"/>
            </div>
        </td>
    </tr>
<?php
}

function variable_fields_js() {
?>
<tr>
        <td>
            <div>
                    <label><?php _e( 'My Custom Field', 'woocommerce' ); ?></label>
                    <input type="text" size="5" name="my_custom_field[' + loop + ']" />
            </div>
        </td>
    </tr>
<?php
}

This is the function that saves the data, just incase the problem could be in here?

function variable_fields_process( $post_id ) {
    if (isset( $_POST['variable_sku'] ) ) :
        $variable_sku = $_POST['variable_sku'];
        $variable_post_id = $_POST['variable_post_id'];
        $variable_custom_field = $_POST['my_custom_field'];
        for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
            $variation_id = (int) $variable_post_id[$i];
            if ( isset( $variable_custom_field[$i] ) ) {
                update_post_meta( $variation_id, '_my_custom_field', stripslashes( $variable_custom_field[$i] ) );
            }
        endfor;
    endif;
}

Looking through the code nothing is sticking out to me. Everything is working except when I save the product, the data entered doesnt show (but it is in the database) so Im thinking it could be a conflict with when it is saved, showing the saved values in the fields.

Thanks for your help


Update: Specifically I think it is this line that is jamming me up:
value="<?php echo $variation_data['_my_custom_field'][0]; ?>"
2

2 Answers

0
votes

I would recommend trying the Advanced Custom Fields (ACF) plugin, assuming this is for your site and not code to go into a published plugin.

I also started trying to code custom fields into a site, then realised after several pages of code that goes way beyond the business needs of "just adding a field", that the ACF plugin does the same job in ten clicks and two minutes. Sometimes you just have to be pragmatic, and accept that occasionally the best way of winning the debugging game is not to play the coding game in the first place.

Apologies if that is not the answer you or SO is looking for.

0
votes

I know this is a little late but i managed to solve this and wanted to share it with you.

Basically WooCommerce have dropped $variation_data. You will now need to add $variation to your function and get the id. With the ID you can them get the post_meta / custom field. This can be done like so:

function variable_fields( $loop, $variation_data, $variation ) {
    $get_variation_id = $variation->ID;
    $data_to_get = get_post_meta($get_variation_id, '_my_custom_field', true );

This line:

value="<?php echo $variation_data['_my_custom_field'][0]; ?>"

Then becomes:

value="<?php echo $data_to_get; ?>"

That's it! i hope this helps you and anyone else that comes across this page.