3

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:

enter image description here

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';


UPDATE:
Here is everything in its entirety in case this helps.

// Display Fields
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields' );
//JS to add fields for new variations
add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
// Save Fields
add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );

function variable_fields( $loop, $variation_data, $variation ) {
?>  
    <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]; ?>"/>

      <?php
      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] ) );
            }
            echo get_post_meta( $variation_id, '_my_custom_field', true) . ' - Test';
        endfor;
    endif;

        //works echo get_post_meta( 134, '_my_custom_field', true) . ' - Test 134';



     ?>
            </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
}

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;
}
10
  • what is the context ? at which position you need to get the variation_id ? to add your field, are you using woocommerce_product_after_variable_attributes hook, or what ? – Shazzad Jan 15 '14 at 0:49
  • Thanks @Shazzad, I updated the question to show my full code, yes I was using that hook. Thanks for your help – Derek Jan 15 '14 at 14:24
  • The third argument $variation is a post object which has the id, ex $variation->ID. Other wise the $variation_data array also do have the id $variation_data['variation_post_id']. And for updating the values into meta, you can use woocommerce_save_product_variation hook, which comes with two argument $$variation_id, $i (index key/number) – Shazzad Jan 15 '14 at 21:57
  • When I try changing the get_post_meta to $variation->ID then I dont get anything to print on the admin product edit page. Would you mind posting an example (im more of a visual learner) where I can see and test it? – Derek Jan 16 '14 at 2:17
  • Ok i will, however i am a little bit confused what you are trying to achieve ? can you please explain so that i can try to explain ? – Shazzad Jan 16 '14 at 5:35