-1

I'm having trouble setting up custom fields for variations on Woocommerce. I followed this very helpful tutorial to get the ball rolling, and everything worked well enough except that my values for the fields aren't being saved on Woocommerce 2.4.6. Basically, I enter in a value, update the post and then the value vanishes.

Based on the comments from that original tutorial, it would seem that I'm not alone. However all of the resources posted, including a separate post on this same topic here on StackExchange, and a few others I've found online don't actually work for me.

An excerpt of my functions file is below. As you'll see, I'm just trying to set one custom text field as a proof of concept, called "Voltage." Using this function, the field does appear in the back end, but the value assigned to it doesn't stick once the product is published.

Ultimately there will be several additional fields, and I'll need to be able to update these on the front end depending on the selection (like the SKU). But right now, I can't even get the values to save. Can anyone offer any help? :-/

<?php
/* Add Custom Fields to Variation Products */

//Display Fields
add_action( 'woocommerce_variation_options', 'variable_fields', 10, 3 );
//JS to add fields for new variations
add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
//Save variation fields
add_action( 'woocommerce_save_product_variation', 'save_variable_fields', 10, 1 );
add_action( 'woocommerce_process_product_meta_variable-subscription' , 'save_variable_fields' , 10 , 1 ) ;

// -------------------------------------------------------------------------------------
// Create new fields for variations
// ------------------------------------------------------------------------------------
function variable_fields( $loop, $variation_data ) {
?>

  <tr>
    <td>
      <?php
      // Text
      woocommerce_wp_text_input( 
        array( 
          'id'          => '_voltage['.$loop.']', 
          'label'       => __( 'Voltage', 'woocommerce' ), 
          'placeholder' => '', 
          'description' => __( 'Enter the voltage here.', 'woocommerce' ),
          'value' => get_post_meta($variation->ID, '_voltage', true)
        )
      );
      ?>
    </td>
  </tr>

<?php
}

// -------------------------------------------------------------------------------------
// Create new fields for new variations, yes again.
// ------------------------------------------------------------------------------------
function variable_fields_js() {
?>

  <tr>
    <td>
      <?php
      // text
      woocommerce_wp_text_input( 
        array( 
          'id'          => '_voltage[ + loop + ]', 
          'label'       => __( 'Voltage', 'woocommerce' ), 
          'placeholder' => '', 
          'description' => __( 'Enter the voltage here.', 'woocommerce' ),
          'value' => get_post_meta($variation->ID, '_voltage', true)
        )
      );
      ?>
    </td>
  </tr>

<?php
}

// -------------------------------------------------------------------------------------
// Save new fields for variations
// ------------------------------------------------------------------------------------
function save_variable_fields( $post_id ) {
  if (isset( $_POST['variable_sku'] ) ) :

    $variable_sku          = $_POST['variable_sku'];
    $variable_post_id      = $_POST['variable_post_id'];

    // Text
    $_voltage = $_POST['_voltage'];
    for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
      $variation_id = (int) $variable_post_id[$i];
      if ( isset( $_voltage[$i] ) ) {
        update_post_meta( $variation_id, '_voltage', stripslashes( $_voltage[$i] ) );
      }
    endfor;


  endif;
}
1

Fixed it, I think. Looks like I botched this line, and forgot to add in $variation. Now to figure out how to get this to display dynamically on the front end so it changes with each variation...

function variable_fields( $loop, $variation_data, $variation ) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.