WooCommerce: add a custom field to product variations
Each variation of a variable product (a size, a colour) has its own price, stock and SKU. Sometimes it also needs its own text: a “Best value” badge on the large size, a delivery note for one colour. WooCommerce has hooks to add such a field to the variation panel in the admin, save it, and show it on the product page when the customer selects the variation.
Problem: A variable product needs an extra text per variation that changes on the product page when the customer picks a different variation.
Solution: Output the field with woocommerce_variation_options_pricing, save it with woocommerce_save_product_variation, add it to the variation data with woocommerce_available_variation, and show it in JavaScript on the found_variation event.
The field in the admin
Every variation is a row in one form, so field names get the index of the variation ($loop):
<?php
add_action( 'woocommerce_variation_options_pricing', 'ha_variation_badge_field', 10, 3 );
function ha_variation_badge_field( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => "ha_variation_badge_{$loop}",
'name' => "ha_variation_badge[{$loop}]",
'value' => get_post_meta( $variation->ID, '_ha_variation_badge', true ),
'label' => __( 'Badge text', 'textdomain' ),
'description' => __( 'For example "Best value" or "New".', 'textdomain' ),
'desc_tip' => true,
'wrapper_class' => 'form-row form-row-full',
) );
}
Saving
<?php
add_action( 'woocommerce_save_product_variation', 'ha_save_variation_badge', 10, 2 );
function ha_save_variation_badge( $variation_id, $loop ) {
if ( ! isset( $_POST['ha_variation_badge'][ $loop ] ) ) {
return;
}
$variation = wc_get_product( $variation_id );
$variation->update_meta_data( '_ha_variation_badge', sanitize_text_field( wp_unslash( $_POST['ha_variation_badge'][ $loop ] ) ) );
$variation->save();
}
The variations are saved by AJAX when you click “Save changes” in the variations tab, so check the result there, not only after “Update” of the product.
Passing it to the product page
The data of all variations is printed into the page as JSON; this filter adds the badge to every variation:
<?php
add_filter( 'woocommerce_available_variation', 'ha_add_badge_to_variation_data', 10, 3 );
function ha_add_badge_to_variation_data( $data, $product, $variation ) {
$data['ha_badge'] = $variation->get_meta( '_ha_variation_badge' );
return $data;
}
// An empty container under the price
add_action( 'woocommerce_single_product_summary', function () {
echo '<div class="ha-variation-badge" style="display:none"></div>';
}, 11 );
Showing it
WooCommerce triggers found_variation on the form when a complete combination is selected, and reset_data when the selection is cleared:
jQuery( function ( $ ) {
var $badge = $( '.ha-variation-badge' );
$( '.variations_form' )
.on( 'found_variation', function ( event, variation ) {
if ( variation.ha_badge ) {
$badge.text( variation.ha_badge ).show();
} else {
$badge.hide();
}
} )
.on( 'reset_data', function () {
$badge.hide();
} );
} );
NOTE: Use $loop in the field name as shown. Without the index, all variations send a field with the same name, PHP keeps only the last one, and every variation gets the same value. For products with more than 30 variations WooCommerce loads the variation data by AJAX instead of printing it into the page; found_variation still delivers the same data, including the badge.