Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@E-VANCE
Forked from hirejordansmith/functions.php
Last active April 30, 2020 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save E-VANCE/3e44cab131bc2cd1aeff4ce3f7044314 to your computer and use it in GitHub Desktop.
Save E-VANCE/3e44cab131bc2cd1aeff4ce3f7044314 to your computer and use it in GitHub Desktop.
Add support for ACF custom fields to WooCommerce Attributes
<?php
// 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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment