'How do you add custom field to woocommerce product attribute

I want to include a simple in the edit attribute form. (/wp-admin/edit.php?post_type=product&page=product_attributes&edit=55)

Is this possible? Everything i've found on SO is concerned with adding fields to products or taxonomies, if I am not wrong, product attributes are not the same as taxonomies.

I want add a custom form to my "Brand" product attribute.

Here is what I tried (with and without the pa_):

add_action('pa_brand_edit_form_fields','msp_pa_brand_form_fields');
add_action('pa_brand_add_form_fields','msp_pa_brand_form_fields');

function msp_pa_brand_form_fields () {
?>
    <tr class="form-field">
            <th valign="top" scope="row">
                <label for="display"><?php _e('Display Type', ''); ?></label>
            </th>
            <td>
                <select name="display_type">
                    <option value="select">Select</option>
                    <option value="variation_image">Variation Image w/ label</option>
                </select>
            </td>
        </tr>
        <?php 
    }

I just need help getting some html to show up on this edit screen. My overall plan is to add this select tag to every attribute, then add a piece of code into variable.php check how a attribute is to be displayed.

Any help is greatly appreciated.



Solution 1:[1]

With the following hooks you can add a field before or after, depending on your wishes

function action_woocommerce_before_edit_attribute_fields(  ) {
    ?>
    <tr class="form-field">
        <th valign="top" scope="row">
            <label for="display"><?php _e('Display Type', ''); ?></label>
        </th>
        <td>
            <select name="display_type">
                <option value="select">Select</option>
                <option value="variation_image">Variation Image w/ label</option>
            </select>
        </td>
    </tr>
    <?php
}
add_action( 'woocommerce_before_edit_attribute_fields', 'action_woocommerce_before_edit_attribute_fields', 10, 0 ); 
 
function action_woocommerce_after_edit_attribute_fields(  ) {
    ?>
    <tr class="form-field">
        <th valign="top" scope="row">
            <label for="display"><?php _e('Display Type', ''); ?></label>
        </th>
        <td>
            <select name="display_type">
                <option value="select">Select</option>
                <option value="variation_image">Variation Image w/ label</option>
            </select>
        </td>
    </tr>
    <?php
}
add_action( 'woocommerce_after_edit_attribute_fields', 'action_woocommerce_after_edit_attribute_fields', 10, 0 );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1