2

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.

Mike A
  • 430
  • 1
  • 5
  • 14

2 Answers2

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.

Mehbub Rashid
  • 553
  • 6
  • 6
-1

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.

Cristian Stan
  • 44
  • 1
  • 8
  • He wanted to add custom field in each custom taxonomy terms (add term, edit term etc). Your code adds field in the edit taxonomy screen. So, your answer is wrong. However, there is a correct way of doing what you did - using `woocommerce_after_edit_attribute_fields` hook – Mehbub Rashid May 11 '22 at 06:17