I created two product categories called “Grade 11” and “Grade 12”. I am trying to use ACF to add a custom field to them. My understanding is I need to create a custom rule location.
I tried to create one, the furthest I’ve gotten is that I can select “Product Category” in the first dropdown, but in the last it’s empty. I don’t see “Grade 11” or “Grade 12”.
I’ve looked at some posts in Stack Overflow and ACF’s docs. I have the following functions:
` function acf_location_rules_types( $choices ) {
$choices[‘Post’][‘product_category’] = ‘Product Category’;
return $choices;
}
add_filter(‘acf/location/rule_types’, ‘acf_location_rules_types’);
function acf_location_rules_values_woocommerce_category( $choices ) {
$args = array(
‘number’ => $number,
‘orderby’ => $orderby,
‘order’ => $order,
‘hide_empty’ => $hide_empty,
‘include’ => $ids
);
$product_categories = get_terms( ‘product_cat’, $args );
if( $product_categories ) {
foreach( $product_categories as $cat ) {
$choices[ $cat->name ] = $cat->name;
}
}
return $choices;
}
add_filter(‘acf/location/rule_values/woocommerce_category’, ‘acf_location_rules_values_woocommerce_category’);
function acf_location_rules_match_woocommerce_category( $match, $rule, $options ) {
$args = array(
‘number’ => $number,
‘orderby’ => $orderby,
‘order’ => $order,
‘hide_empty’ => $hide_empty,
‘include’ => $ids
);
$product_categories = get_terms( ‘product_cat’, $args );
$selected_category = (int) $rule[‘My Product Category Name’];
if($rule[‘operator’] == “==”)
{
foreach( $product_categories as $cat ) {
$match = ( $cat->name == $selected_category );
}
}
return $match;
}
add_filter(‘acf/location/rule_match/woocommerce_category’, ‘acf_location_rules_match_woocommerce_category’, 10, 3);`