'How to add a custom field to all Woocommerce attribute terms using add_action

On the Woocommerce Product Attributes page, I have the following working for one Woocommerce attribute (called myattribute). The following adds the custom field to the add / edit page of the terms for myattribute:

add_action( 'myattribute_add_form_fields', array( $this, 'add_new_field' ), 10, 2 );

add_action( 'created_myattribute', array( $this, 'save_new_field' ), 10, 2 );

add_action( 'myattribute_edit_form_fields', array( $this, 'update_new_field' ), 10, 2 );

add_action( 'edited_myattribute', array( $this, 'updated_new_field' ), 10, 2 );

Is there a way to do the same but for all attributes, without needing to name them in the add_action?

The reason is that I have numerous attributes and would like to add custom fields to all the terms related to those attributes.

Thanks in advance.



Solution 1:[1]

There is a clear difference between adding fields in edit taxonomy screen and in edit taxonomy term screen. Looking into your codes, I think you want to achieve the second one.

This is how I have done it -

if ( is_admin(  ) && isset( $_GET['taxonomy'], $_GET['post_type'] ) && $_GET['post_type'] === 'product' ) {
    $taxonomy_name = sanitize_text_field( $_GET['taxonomy'] );


    add_action( $taxonomy_name.'_edit_form_fields', 'etoiles_add_priority_field', 10, 2 );


    function etoiles_add_priority_field($term, $taxonomy) {
        ?> 
        <tr class="form-field">
            <th>
                <label for="term_priority"><?php echo esc_html__( 'Priority', 'etoiles' ); ?></label>
            </th>
            <td>
                <input name="term_priority" id="term_priority" type="number" value="" />
                <p class="description"><?php echo esc_html__( 'The more the priority, the first it will come in product filter items.', 'etoiles' ); ?></p>
            </td>
        </tr>
        <?php
    }
}

Note: This is just adds the field. You will have to save the field values by using the hooks you wrote in your question. Hope everyone gets the idea.

Solution 2:[2]

This might not be the best solution but it works for me:

if (isset($_GET['taxonomy']) && !empty($_GET['taxonomy'])) {
    $taxonomy_name = $_GET['taxonomy'];

    if (strpos($taxonomy_name, 'pa_') !== false) {
        add_action( $taxonomy_name.'_add_form_fields', array ( $this, 'add_category_image' ), 10, 2 );
        add_action( 'created_'.$taxonomy_name, array ( $this, 'save_category_image' ), 10, 2 );
        add_action( $taxonomy_name.'_edit_form_fields', array ( $this, 'update_category_image' ), 10, 2 );
        add_action( 'edited_'.$taxonomy_name, array ( $this, 'updated_category_image' ), 10, 2 );
    }
}

I was looking for the same solution and so far this quick code helped me achieve the custom field on any product attribute edit/add views.

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
Solution 2 Cristian Stan