I am trying to create some additional functionality on my Woocommerce backend where I add a custom field to the variations section of backend of Woocommerce. However I cannot for the life of me figure out how to get the current variation id so I can get the post meta.
here is what I have been working with:
<?php
// Get variations
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $post->ID
);
$variations = get_posts( $args );
foreach ( $variations as $variation ) {
$variation_id = absint( $variation->ID );$variable_id = $this['variation_id'];
$variation_post_status = esc_attr( $variation->post_status );
$variation_data = get_post_meta( $variation_id );
$variation_data['variation_post_id'] = $variation_id;
echo get_post_meta( $variation_data['variation_post_id'], '_my_custom_field', true) . ' - TEST';
}
?>
When I check the backend it looks like it is pulling all the post meta into each variation like this:
However if I use the actual variation id like the below then it works for that variation:
echo get_post_meta( 134, '_my_custom_field', true) . ' - Test Variation #134';
@Derek,
Where in your template are you trying to get the custom field data to display?
If it is within the product loop, you can do this:
You in your foreach loop you should be able to access it via:
This would look like:
You may also be able to use the get_the_ID() function