3

On Wordpress / WooCommerce, is it possible to add custom fields to a WooCommerce attribute term?

With "attributes" I mean the general attributes and not the attributes under products.

Please, check the image below for more details:

enter image description here

Is it possible to do it with ACF (Advanced Custom Fields) plugin?

Thanks!

closed as off-topic by Krzysiek Dróżdż, Jacob Peattie, Fayaz, Christine Cooper, bosco May 25 '18 at 4:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

7

Yes, it is possible. And there's an easy guide here.

Below is a working code you can add to the theme's main functions.php file:

// Adds a custom rule type.
add_filter( 'acf/location/rule_types', function( $choices ){
    $choices[ __("Other",'acf') ]['wc_prod_attr'] = 'WC Product Attribute';
    return $choices;
} );

// Adds custom rule values.
add_filter( 'acf/location/rule_values/wc_prod_attr', function( $choices ){
    foreach ( wc_get_attribute_taxonomies() as $attr ) {
        $pa_name = wc_attribute_taxonomy_name( $attr->attribute_name );
        $choices[ $pa_name ] = $attr->attribute_label;
    }
    return $choices;
} );

// Matching the custom rule.
add_filter( 'acf/location/rule_match/wc_prod_attr', function( $match, $rule, $options ){
    if ( isset( $options['taxonomy'] ) ) {
        if ( '==' === $rule['operator'] ) {
            $match = $rule['value'] === $options['taxonomy'];
        } elseif ( '!=' === $rule['operator'] ) {
            $match = $rule['value'] !== $options['taxonomy'];
        }
    }
    return $match;
}, 10, 3 );

You'd get something like this on the ACF create/edit Field Group screen:

enter image description here


UPDATED Sep 25 2018 (UTC)

In the function for matching the rule on the term edit page, the $options['ef_taxonomy'] has been changed to $options['taxonomy'] — back then, the array key taxonomy didn't exist (in my case), and it exists now, which I think replaces the ef_taxonomy key. Thanks @JordanCarter for noticing the key issue, and @VadimH for the initial answer's edit. =)

In that function, I also added the if ( isset( $options['taxonomy'] ) ) check to avoid PHP's "undefined" notice. Thanks @JordanCarter for noticing this.

@VadimH, you can use get_field( '{NAME}', 'term_{TERM ID}' ) to retrieve (and display) the field's value, like so:

$term_id = 123;
$value = get_field( 'my_field', 'term_' . $term_id );

See the "Get a value from different objects" section on the get_field()'s official documentation.

PS: The whole code (not just the get_field()) was last tried and tested on ACF 5.7.6 and ACF PRO 5.7.3, with WooCommerce 3.4.5.

  • WC Product Attribute option is not there in the UI (ACF Pro v.5.7.3; WC v.3.4.3) – Vadim H Sep 1 '18 at 21:21
  • 1
    This works for me, except I had to change $options['ef_taxonomy'] to $options['taxonomy'] in the Matching custom rule filter – Jordan Carter Sep 18 '18 at 21:20
  • Thanks @JordanCarter. As I could recall it, that key wasn't available back then. So to everyone reading this, use $options['taxonomy'] and not $options['ef_taxonomy'] – Sally CJ Sep 19 '18 at 2:30
  • 1
    @SallyCJ Actually, I ran into an error message on a single product page, saying that the "taxonomy" key doesn't exist. I wrapped the entire bit in an array_key_exists IF statement – Jordan Carter Sep 19 '18 at 21:20
  • 1
    @SallyCJ oops, sorry, forgot to paste the code into functions.php . And having corrected the $options, it all works now, thank you (I just edited this answer, it's getting peer-reviewed now). Next question: how do you get that custom field's value in the site front-end? – Vadim H Sep 24 '18 at 23:20

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