Home › Forums › Backend Issues (wp-admin) › ACF + Woocommerce Categories Backend
Hi,
in recent days I had opened a request on the general forum of the repository regarding this question of mine.
I created some groups of fields in ACF that are shown in the sidebar, when inserting a product on woocommerce.
What I can’t do is show the fields in relation to the woocommerce categories.
Ex.
– woocat 1 -> group_1
– woocat 2 -> group_2In Position Rules I set the following rules:
1. Content Type = Product
2. User Role = Administrator
3. Item Taxonomy = woocat 1But despite this, the group of fields appears in every product page.
But having read the documentation for developers I created this function which should solve it and could possibly help other users too:
// Verifica se ACF è installato prima di utilizzare i filtri ACF
if (function_exists('acf')) {
add_filter('acf/load_field/name=attributo_montaggio', 'campoextra_acf_montaggio');
function campoextra_acf_montaggio( $field ) {
global $current_screen, $post;
// Verifica se l'utente corrente ha le autorizzazioni necessarie
if (!current_user_can('edit_posts')) {
// L'utente corrente non ha le autorizzazioni necessarie, esci
return $field;
}
// Ottieni i termini della categoria corrente
$terms = get_the_terms($post->ID, 'product_cat');
// Verifica se $terms è un array non vuoto
if (is_array($terms) && !empty($terms)) {
//$category_id = $terms[0]->term_id;
$category_name = $terms[0]->name;
// Array di nomi di categorie da confrontare
$allowed_categories = array(
"Cat Name 1",
"Cat Name 2",
// Aggiungi altri nomi di categorie secondo necessità
);
// Verifica se non siamo in modalità amministratore o se il tipo di post non è 'product' o se l'ID della categoria non è nell' array
if ((!is_admin() && $current_screen->post_type != 'product') || !in_array(strtolower($category_name), array_map('strtolower', $allowed_categories))) {
return false;
}
}
return $field;
}
}
I used the load filter and not the prepend.
Do you think it can be improved?
Thanks
You must be logged in to reply to this topic.
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.