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]; ?>"