6

Is there a way to add a custom field to the add new attribute page or edit attribute in Woocommerce ?

Preferably with CMB2, but whatever works.

Woocommerce attributes page

As I understand, this specific section of woocommerce is a taxonomy inception - it's a taxonomy that creates taxonomies :)

I looked under woocommerce/includes/class-wc-post-types.php but not sure what filter would do the trick (if any).

I've managed to add fields to the individual terms but I need a field for the attribute itself.

3
  • using 'woocommerce_after_add_to_cart_form' and 'woocommerce_after_edit_attribute_fields' actions, i've managed to output a field on the add / edit pages, don't know how to save / load the value from the database
    – Mihai
    Commented Jul 15, 2020 at 0:33
  • Did my answer help or you needed more details? :)
    – Sally CJ
    Commented Jul 16, 2020 at 6:29
  • 1
    Yes, thanks! I ended up creating a function to generate multiple fields in case i needed, but basically doing what you suggested.
    – Mihai
    Commented Jul 19, 2020 at 22:55

1 Answer 1

12

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" );
6
  • 2
    Great answer Sally CJ, How can i do for an upload file field? thanks in advanced. Commented Feb 14, 2021 at 16:16
  • 1
    The bit you need to change is my_save_wc_attribute_my_field, where you'll have to accept the file upload instead - and probably save it to disk or the media library and put a filename or ID in the options table rather than store all the data there.
    – Rup
    Commented Feb 14, 2021 at 16:33
  • @Sally CJ How can I display field value on product page?
    – Juraj
    Commented Oct 14, 2023 at 12:27
  • @Juraj as long as you know the attribute ID, which is the attribute_id value in the woocommerce_attribute_taxonomies table, you can use get_option() to get the field value no matter where are you displaying it. If you don't already know the attribute ID, but you know the attribute name (which is a taxonomy like pa_color), you can use wc_attribute_taxonomy_id_by_name() to get the attribute ID. Anyway, have a look at this example.
    – Sally CJ
    Commented Jan 26 at 9:30
  • @JuaneteMontoya @Rup back then, I didn't check this, but today I found out that I had to manually add the enctype attribute to the product attribute form, hence I made an example here which adds a field labeled "My Image Field" (like this).
    – Sally CJ
    Commented Jan 26 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.