Skip to content

Instantly share code, notes, and snippets.

@corsonr
Last active August 9, 2023 17:49
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save corsonr/3d0425deaa80c601d454 to your computer and use it in GitHub Desktop.
Save corsonr/3d0425deaa80c601d454 to your computer and use it in GitHub Desktop.
WooCommerce: custom variations settings
<?php
// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
/**
* Create new fields for variations
*
*/
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field[' . $variation->ID . ']',
'label' => __( 'My Text Field', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_text_field', true )
)
);
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_number_field[' . $variation->ID . ']',
'label' => __( 'My Number Field', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter the custom number here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_number_field', true ),
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea[' . $variation->ID . ']',
'label' => __( 'My Textarea', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_textarea', true ),
)
);
// Select
woocommerce_wp_select(
array(
'id' => '_select[' . $variation->ID . ']',
'label' => __( 'My Select Field', 'woocommerce' ),
'description' => __( 'Choose a value.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_select', true ),
'options' => array(
'one' => __( 'Option 1', 'woocommerce' ),
'two' => __( 'Option 2', 'woocommerce' ),
'three' => __( 'Option 3', 'woocommerce' )
)
)
);
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_checkbox[' . $variation->ID . ']',
'label' => __('My Checkbox Field', 'woocommerce' ),
'description' => __( 'Check me!', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_checkbox', true ),
)
);
// Hidden field
woocommerce_wp_hidden_input(
array(
'id' => '_hidden_field[' . $variation->ID . ']',
'value' => 'hidden_value'
)
);
}
/**
* Save new fields for variations
*
*/
function save_variation_settings_fields( $post_id ) {
// Text Field
$text_field = $_POST['_text_field'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
}
// Number Field
$number_field = $_POST['_number_field'][ $post_id ];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_number_field', esc_attr( $number_field ) );
}
// Textarea
$textarea = $_POST['_textarea'][ $post_id ];
if( ! empty( $textarea ) ) {
update_post_meta( $post_id, '_textarea', esc_attr( $textarea ) );
}
// Select
$select = $_POST['_select'][ $post_id ];
if( ! empty( $select ) ) {
update_post_meta( $post_id, '_select', esc_attr( $select ) );
}
// Checkbox
$checkbox = isset( $_POST['_checkbox'][ $post_id ] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_checkbox', $checkbox );
// Hidden field
$hidden = $_POST['_hidden_field'][ $post_id ];
if( ! empty( $hidden ) ) {
update_post_meta( $post_id, '_hidden_field', esc_attr( $hidden ) );
}
}
@pjv
Copy link

pjv commented Nov 23, 2015

As far as I can tell, that save function has a mistaken assumption that the $post_id parameter is the key for the array that makes up the $_POST['_custom_field_name'] for the custom fields. In my testing, that's not the case - the $post_id parameter is literally the WP post ID for the product variation (which makes sense in the update_post_meta call you have in here).

e.g., where you have $text_field = $_POST['_text_field'][ $post_id ]; I think that could only possibly return a value in the rare coincidence that the $post_id happens to also be the same number as the array key - but it's not going to be the value you really want.

My working code for saving the custom variation fields (works unchanged for WC versions 2.2, 2.3, 2.4) looks like this:

function save_variation_settings_fields( $post_id ) {
  foreach ($_POST['variable_post_id'] as $k => $v) {
    if ( intval($v) == intval($post_id) ) {
      update_post_meta( $post_id, '_custom_field_name1', esc_attr( $_POST['_custom_field_name1'][$k] ) );
      update_post_meta( $post_id, '_custom_field_name2', esc_attr( $_POST['_custom_field_name2'][$k] ) );
      // etc.
    }
  }
}

@studioleland
Copy link

Hello, for some reason the following loading for select fields doesn't pre-populate with existing field value. In my case below the empty value is always preselected when I edit a product variation.

woocommerce_wp_select( 
    array( 
        'id'          => '_product_color_select[' . $variation->ID . ']', 
        'label'       => __( 'Color Swatches', 'woocommerce' ), 
        'desc_tip'    => 'true',
        'description' => __( 'Base color for visitor filtering.', 'woocommerce' ),
        'value'       => get_post_meta( $variation->ID, '_product_color_select', true ),
        'options' => array(
            ''   => __( 'No Color', 'woocommerce' ),
            'black'   => __( 'Black', 'woocommerce' ),
            'blue'   => __( 'Blue', 'woocommerce' ),
            'brown'   => __( 'Brown', 'woocommerce' ),
            'grey'   => __( 'Grey', 'woocommerce' ),
            'green'   => __( 'Green', 'woocommerce' ),
            'orange'   => __( 'Orange', 'woocommerce' ),
            'Purple' => __( 'Purple', 'woocommerce' ),
            'red'   => __( 'Red', 'woocommerce' ),
            'tan' => __( 'Tan', 'woocommerce' ),
            'white'   => __( 'White', 'woocommerce' ),
            'yellow' => __( 'Yellow', 'woocommerce' )
            )
        )
    );

Any suggestions? I see the saved values just fine on the frontend. When I edit a product variation it just selects the first selection option from the dropdown array not the value that was previously saved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment