Add custom field to Woocommerce add new attribute / edit page

Yes, you can add custom fields to WooCommerce (product) attributes, which are WordPress custom taxonomies — e.g. an attribute named Color creates a taxonomy with the slug pa_color.

But because the data are stored in the woocommerce_attribute_taxonomies table which does not offer a field for custom data, you’ll need to save your data somewhere else, e.g. the WordPress options table (wp_options) like what I did in the examples below:

Add “My Field” to the attribute form:

function my_edit_wc_attribute_my_field() {
    $id = isset( $_GET['edit'] ) ? absint( $_GET['edit'] ) : 0;
    $value = $id ? get_option( "wc_attribute_my_field-$id" ) : '';
    ?>
        <tr class="form-field">
            <th scope="row" valign="top">
                <label for="my-field">My Field</label>
            </th>
            <td>
                <input name="my_field" id="my-field" value="<?php echo esc_attr( $value ); ?>" />
            </td>
        </tr>
    <?php
}
add_action( 'woocommerce_after_add_attribute_fields', 'my_edit_wc_attribute_my_field' );
add_action( 'woocommerce_after_edit_attribute_fields', 'my_edit_wc_attribute_my_field' );

Set/save the “My Field” value, e.g. in the options table:

function my_save_wc_attribute_my_field( $id ) {
    if ( is_admin() && isset( $_POST['my_field'] ) ) {
        $option = "wc_attribute_my_field-$id";
        update_option( $option, sanitize_text_field( $_POST['my_field'] ) );
    }
}
add_action( 'woocommerce_attribute_added', 'my_save_wc_attribute_my_field' );
add_action( 'woocommerce_attribute_updated', 'my_save_wc_attribute_my_field' );

Delete the custom option when the attribute is deleted.

add_action( 'woocommerce_attribute_deleted', function ( $id ) {
    delete_option( "wc_attribute_my_field-$id" );
} );

Then for example, on a taxonomy/term archive page, you can get the My Field value like so:

$term = get_queried_object();
$attr_id = wc_attribute_taxonomy_id_by_name( $term->taxonomy );
$my_field = get_option( "wc_attribute_my_field-$attr_id" );