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

0

Leave a Reply

Your email address will not be published. Required fields are marked *