Home › Forums › Backend Issues (wp-admin) › Custom Field on Certain WooCommerce Product Category Page › Reply To: Custom Field on Certain WooCommerce Product Category Page
Following along with the instructions within the link, I’m able to accomplish all the steps except for “Matching the Rule” to the Category Editor page (Sorry for the sloppy code).
add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types( $choices ) {
$choices['WooCommerce Category']['woocommerce_category'] = 'WooCommerce Category';
return $choices;
}
add_filter('acf/location/rule_values/woocommerce_category', 'acf_location_rules_values_woocommerce_category');
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_match/woocommerce_category', 'acf_location_rules_match_woocommerce_category', 10, 3);
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;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.