I’m not sure. Are you sure about the taxonomy name being “product_cat_” I’m assuming it is based on the name of the template file being used.
and you may need your action and function that you’d normally use for woocommerce. I don’t know a lot about how ACF plays with woocommerce.
What I do know is that your original code the_field('custom_category_content');
was not working because you were not supplying it with the needed id value to get the value of a custom field that is attached to a term according to the ACF doc (http://www.advancedcustomfields.com/resources/functions/the_field/)
You can make sure that $term_id = get_queried_object()->term_id;
is returning a valid term ID.
$object = get_queried_object();
echo $object->term_id;
you can even check the entire queried object to see what’s in there
object = get_queried_object();
print_r($object);
It’s still not showing up. I added the code below to my taxonomy template:
<div>
<?php
$term_id = get_queried_object()->term_id;
$post_id = ‘taxonomy-name_’.$term_id;
the_field('custom_category_content', $post_id);
?>
</div>
I also double checked my custom field location. It’s set to: “Taxonmony Term” is equal to “Product Categories”. Also, do I still need the woocommerce “add_action” function (in my functions file)? IF so, is the one I have correct? So sorry for this… thanks again.
Hey guys.
Sorry about the account issues. It looks like the WooCommerce migration did not like the passwords from shopp….
Please email [email protected] with your user info, and I’ll manually reset you p/w.
Thanks
Elliot
Thanks, that was the same thing I just figured out.
There was a plugin installed named “PBP Own post & media for Author” that used $wp_query->set( 'author', $current_user->ID );
I didn’t resolve it completely, but I think I have a workaround. It is for WooCommerce, and I bought a $79 extension to accomplish what I was trying to do.
I appreciate your time.
Eventually figured this out! FYI, here’s the code in case it helps anyone. The name of my WYSIWYG field is ‘about_this_product_category’, which you would replace with your own field name.
<?php
// vars
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
// load desc for this taxonomy term (term object)
$thumbnail = get_field('about_this_product_category', $queried_object);
// load desc for this taxonomy term (term string)
$thumbnail = get_field('about_this_product_category', $taxonomy . '_' . $term_id);
?>
<?php the_field( 'about_this_product_category', $queried_object ); ?>
I copied the archive-product.php from the WooCommerce plugin directory into a new /woocommerce/ folder inside my theme.
Hi,
I’m not sure how you went with this, but I am trying to add some ACF fields to the Single Products Page in Woocommerce. I was hoping to use the hooks that woocommerce provides to add the custom fields stuff, but I can’t seem to get the fields to output anything.
in my functions.php I have added an action like so:
// Add ACF Info to Product display page
add_action( 'woocommerce_after_single_product_summary', "ACF_product_content", 10 );
function ACF_product_content(){
echo '<h2> ACF Content </h2>';
if (function_exists('the_field')){
echo '<p>Woohoo, the_field function exists! </p>';
the_field('test_field');
}
}
The h2 and the p are displayed, but the field itself doesn’t get rendered. Any thoughts?
Cheers.
Thanks for the follow up. I just did some testing, and can confirm that there is a bug preventing the Woocommerce products from appearing.
I have fixed the issue and pushed the fix to github. Please download the latest version, and let me know how you go.
Thanks
E
Thanks for the response Elliot but unfortunately selecting “post” does not give me access to the actual individual products themselve – these are not appearing.
Are they suppose to Elliot b/c from what I can see, you are not classifying Woocommerce (individual) products as a post – is this correct?
Selecting post in the location rules only displays actual posts within the “Post” menu option in WordPress and that’s it.
At the moment within the Products section of Woocommerce, I have two products, Product A and Product B – it is these I am trying to access, inorder to place specific field groups against them.
If I look within the wp_posts table, I can see my two products
where post_type = 'product'
what I need in addition are access to the columns post_name or post_title, as these will give me access to a specific product but I am not able to get to these via ACF location rules.
Any further ideas/workarounds Elliot would be much appreciated as I only want specific custom field groups against a specific product, i.e. Product A.
Thanks again.
Hi @QueenEve
I can’t confirm that the ACF shortcode would work with this 3rd party plugin.
Just to confirm, this plugin allows you to add 1 extra tab to the woocommerce product page?
Did you change the ‘${field_name}’ to the actual field name?
Can you confirm that any shortcode (non ACF) would work in this tab text area?
My suspicions are that shortcodes do not yet work in this plugin’s content area.
Thanks
E
Hi @QueenEve
No worries.
1. Use ACF to create a field group containing the fields you want and then use location rules to match the post_type to the Woocomerce product
2. Research on the Woocomerce website to find out how you can customize the single product page
3. Edit that template and use the ACF functions such as get_field / the_field to display your custom field data
Hope that helps.
To better improve the docs, can you please provide some feedback? You say that you don’t know how to use the docs? How come?
Thanks
E
Thanks elliot for your quick response but I tried to review it many times but still I cannot understand how it works :'( :'(
can you please give me a guide how to use the documentation at least , I am really confused
Hi @QueenEve
Please review the documentation page to understand how ACF works.
Thanks
E
We are using default settings for WYSIWYG
We have not set any custom caps, however we are using Woocommerce – which does, i think.
If you have problems recreating the issue – this was my case also. Other admin users of the site reported the problem, but my user did not have any issues – so I did not believe them … until I created a new admin user and logged in as that guy – then I saw that they where telling the truth..
Now after using CCleaner to clear everything – my user is also gets the error.
1. we have a repeater field that has a post obj field as a sub field.
2. we have tested it with Woocommerce’s ‘products’ as post object.
3. we have tested it with a personal created CTP called Deals as post object.
4. Happened on the woo single product
5. happened on another single CTP as well (when testing if this was woo related)
6. Problem does not occur when repeater field is disabled..
Possible workaround is to use relationship field instead of repeater field.
Hi @ayottepl
Your code is incorrect.
Please read over the docs and make sure to use the field_name and post_id parameters correctly.
Thanks
E
Hi @elliot,
My issue was that I wanted to add a second description field in my category pages on Woocommerce (with a Wysiwyg editor). The field was correctly displayed in the back-office, however in the front-office it didn’t work with the following code : <?php the_field(‘description_categorie_2’); ?>
And your code in the section “Finding the term related to the current post” (here http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-a-taxonomy-term/) didn’t work either.
But ayottepl gave me a clever tip to resolve it
Have a great day !
Hi @ayottepl
Sorry I can’t be of much help, I don’t read or speak french, although I wish I did.
The $post issue can be solved by looking on woocomerce’s documentation to find a function which finds the current product ID.
Knowing the ID will fix the issue, because you can use the ID as a second parameter to the get_field function!
Thanks
E
Ça fait plaisir!
Bonne continuité et bon WE @elsappy 🙂
@elliot i think Woocommerce does not use $post for products on archive-products.php.
Have you any idea what i can use instead of $post? I’ll try to look further more to solve my body class trick for next use of this.
Wow tu es un génie ma parole ! Ca fonctionne enfin… C’est peut être une méthode un peu bricolée mais bon ça marche c’est l’essentiel !
MERCI 1000 fois je vais enfin pouvoir avancer 🙂 🙂
Bonne journée et bon week-end !
Crap.. j’avais tout écris et en voulant ajouter un link ça fermé l’onglet.. ok je recommence mais je vais faire un peu plus court!
Bonne déduction, dynamique étant le ID selon la catégorie dans laquelle tu te trouves.
De plus, je crois que comme tu dis, le $post ne s’applique pas dans Woocommerce.
Tu peux donc utiliser le code que j’ai créé pour aller chercher le ID dans la classe de l’élément <body>
<?php
//Get product_cat id
foreach(get_body_class() as $term){
if(preg_match('/term-[0-9]/', $term)){
$exploded = explode('term-', $term);
$id = $exploded[1];
}
}
?>
Ensuite, tu dois créer une variable pour aller chercher ton champ ACF pour chaque catégorie spécifiquement :
<?php
$banner = get_field('field_51fbcc4b53e5f', 'product_cat_'.$id);
?>
Comment aller chercher ce ID (field_51fbcc4b53e5f)?
Vas dans l’admin, sous Custom fields, et trouve ton champ ACF. Ensuite, clique droit et inspect l’élément. Voici une capture d’écran du ID de mon champ ACF en exemple plus haut :
http://cl.ly/image/1U1V432w0n46
Pas très compliqué à trouver 😉
Donc par la suite tu n’auras aucune difficulté à utiliser ta variable (ici en exemple $banner) comme suit :
<?php echo $banner ?>
Voilà!
J’espère que ça va fonctionner 🙂
Bonne chance!
Merci infiniment pour ta réponse !
Premier test plutôt concluant je tiens quelque chose grâce à toi : j’ai inséré l’id de l’une des mes catégories (par exemple ma catégorie s’appelle “caramel” dont l’id est 27), jusqu’ici tout va bien, le texte s’affiche.
Mais voila, j’ai 30 autres (sous-)catégories à éditer et en utilisant le code que tu m’a donné avec l’id de cette catégorie et en paramètrant dans ACF “Taxonomie = Caramel”, ce même texte s’affiche sur toutes les catégories confondues 🙂
Alors pour en revenir à tes explications, tu dis : “Ensuite, pour le faire dynamique, Elliot a publié un lien utile qui pourrait fonctionner (je ne l’ai pas testé)” : comme je suis assez nulle, pourrais-tu juste me dire ce que tu entends par dynamique ? Et le code que tu m’as donné tu le placerais dans le fichier archive-product aussi ? *
Je vais me creuser la tête maintenant mais c’est vraiment pas évident ^^ Merci encore, enfin un résultat un peu positif !
* En fait je pense que dynamique veut dire que l’id changera en fonction de la page de catégorie c’est ça ? Pour le code, je l’ai copié dans archive-product, remplacé ce qu’il fallait comme tu m’as dis, mais ca ne marche pas. Alors j’ai une question un peu bête mais le $post qui est écrit au tout début ça ne fait pas référence aux articles ça ?
Bonjour elsappy,
Pour être capable d’afficher ça dans la page archives, tu dois avoir le ID du terme utilisé pour cette archive.
Pour ma part j’ai utilisé une fonction php afin de sortir le ID qui apparait dans une des classes du body… c’est pas très propre mais à ce moment là au moins ça fonctionnait 😉
Tu peux commencer en essayant avec un ID hardcodé peut-être?
<?php the_field(‘description_categorie_2′, ‘ product_cat_19′); ?>
admettons. Je mets 19 à titre d’exemple mais tu vas retrouver cette classe sur ton champ body avec le bon id de page à la place. Par contre, sur ton champ body, le id va être relié à “term-” mais tu te dois d’utiliser product_cat_ pour les champs acf!
Ensuite, pour le faire dynamique, Elliot a publié un lien utile qui pourrait fonctionner (je ne l’ai pas testé) : http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-a-taxonomy-term/
En utilisant ce code suivant :
<?php
global $post;
// load all 'category' terms for the post
$terms = get_the_terms($post->ID, 'category');
// we will use the first term to load ACF data from
if( !empty($terms) )
{
$term = array_pop($terms);
$custom_field = get_field('category_image', 'category_' . $term->term_id );
//ici je remplacerais get_field('category_image', 'category_' . $term->term_id
//par get_field('description_categorie_2', 'product_cat_' . $term->term_id
// do something with $custom_field
}
?>
Tu pourrais obtenir le ID de catégorie que tu as besoin.
En gros, tu as obligatoirement besoin du 2e paramêtre dans ton “the_field” qui se trouve à pointer la bonne catégorie 🙂
Bonne chance! N’hésite pas si ça ne fonctionne pas, il y a toujours moyen de s’entraider 😉
Bonjour Ayottepl,
Je constate que tu es français donc j’en profite pour te poser une question puisque j’ai un problème presque similaire : je suis en train de faire ma boutique sur Woocommerce et dans mon cas je voudrais ajouter une seconde description de catégorie tout en bas de mes pages de catégorie.
J’ai donc paramétré un champ Wysiwyg et créé la règle suivante :
Term est égal à Catégories
Dans mon template archive-product.php, j’ai ajouté le code suivant : <?php the_field(‘description_categorie_2’); ?>
L’éditeur Wysiwyg s’affiche parfaitement dans l’admin, je peux éditer mon texte et l’enregistrer sans problème, mais côté front-office rien se passe ! J’avoue que je suis débutante en php et je n’y connais pas grand chose (donc j’ai eu un peu de mal à comprendre comment tu as résolu ton problème :D). Si jamais tu as une idée pour mon problème je t’en serais grandement reconnaissante parce que comme toi je m’arrache les cheveux depuis des jours et des jours….
Bonne journée,
Elsa
Hi Elliot,
Thanks for your reply!
Would this be possible to do manually? I’ve been trying to get this to work for so long now, and thought I’d cracked it using the acf plugin.
Basically I need to show a piece of code on certain woocommerce category pages, but I can’t use the conditional statement is_product_cat within the single product page template.
If you could advise me on the manual code I would need to add, in order to show a custom field, maybe I could rework it so that it resolves my issue?
Any help would be greatly appreciated!
It turns out it was an issue with WooCommerce’s function returning the correct taxonomy id. I appreciate the help and sorry for the false alarm.
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.