Skip to content

Instantly share code, notes, and snippets.

@gbyat
Last active July 28, 2020 22:54
Show Gist options
  • Save gbyat/c1bcc9f47ef9e661fc6f to your computer and use it in GitHub Desktop.
Save gbyat/c1bcc9f47ef9e661fc6f to your computer and use it in GitHub Desktop.
WooCommerce Custom Fields for Variations
<?php
//Display Fields
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 3 );
//Save variation fields
add_action( 'woocommerce_save_product_variation', 'save_variable_fields', 10, 2 );
function variable_fields( $loop, $variation_data, $variation ) {
// Text Field
$text_field = get_post_meta( $variation->ID, '_text_field', true );
woocommerce_wp_text_input(
array(
'id' => 'text_field['.$variation->ID.']',
'label' => __( 'My Text Field', 'woocommerce' ),
'placeholder' => __( 'placeholder text', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter custom value here.', 'woocommerce' ),
'value' => $text_field
)
);
// Number Field
$number_field = get_post_meta( $variation->ID, '_number_field', true );
woocommerce_wp_text_input(
array(
'id' => 'number_field['.$variation->ID.']',
'label' => __( 'My Number Field', 'woocommerce' ),
'desc_tip' => 'true',
'type' => 'number',
'description' => __( 'Enter custom number here.', 'woocommerce' ),
'value' => $number_field,
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Textarea
$textarea = get_post_meta( $variation->ID, '_textarea', true );
woocommerce_wp_textarea_input(
array(
'id' => 'textarea['.$variation->ID.']',
'label' => __( 'My Textarea', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter custom value here.', 'woocommerce' ),
'value' => $textarea,
)
);
// Select
$select = get_post_meta( $variation->ID, '_select', true );
woocommerce_wp_select(
array(
'id' => 'select['.$variation->ID.']',
'label' => __( 'Select', 'woocommerce' ),
'description' => __( 'Choose a value.', 'woocommerce' ),
'value' => $select,
'options' => array(
'one' => __( 'Option 1', 'woocommerce' ),
'two' => __( 'Option 2', 'woocommerce' ),
'three' => __( 'Option 3', 'woocommerce' )
)
)
);
// Checkbox
$checkbox = get_post_meta( $variation->ID, '_checkbox', true );
woocommerce_wp_checkbox(
array(
'id' => 'checkbox['.$variation->ID.']',
'label' => __('Check', 'woocommerce' ),
'description' => __( 'Check me!', 'woocommerce' ),
'value' => $checkbox,
)
);
}
/**
* Save custom field values for variations
*
*/
function save_variable_fields( $variation_id ) {
// Text Field
if ( isset( $_POST['text_field'][ $variation_id ] ) ) {
$text_field_value = sanitize_text_field( $_POST['text_field'][ $variation_id ] );
update_post_meta( $variation_id, '_text_field', $text_field_value );
}
// Number Field
if ( isset( $_POST['number_field'][ $variation_id ] ) ) {
$number_field_value = absint( $_POST['number_field'][ $variation_id ] );
update_post_meta( $variation_id, '_number_field', $number_field_value );
}
// Textarea
if ( isset( $_POST['textarea'][ $variation_id ] ) ) {
$textarea_value = sanitize_textarea_field( $_POST['textarea'][ $variation_id ] );
update_post_meta( $variation_id, '_textarea', $textarea_value );
}
// Select
if ( isset( $_POST['select'][ $variation_id ] ) ) {
$select_value = sanitize_key( $_POST['select'][ $variation_id ] );
update_post_meta( $variation_id, '_select', $select_value );
}
// Checkbox
$checkbox_value = isset( $_POST['checkbox'][ $variation_id ] ) ? 'yes' : 'no';
update_post_meta( $variation_id, '_checkbox', $checkbox_value );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment