Home › Forums › General Issues › Conditional Display of ACF Single Product Page
Hello,
I am creating a page to sell to different categories of products. My issue is that not all the fields pertain to all categories. So what I would like to do is display only the fields that are relevant to the product. I am currently using this code to display fields in my product page (Works great, except is shows for all categories):
/* Custom fields on Proudct Page */
add_action( 'woocommerce_single_product_summary', 'sbnai_display_acf_field_1', 30 );
function sbnai_display_acf_field_1() {
echo '<b>Per Round Cost:</b> ' . get_field('per_round_cost') . '<br />';
echo '<b>UPC:</b> ' . get_field('upc') . '<br />';
echo '<b>Caliber:</b> ' . get_field('caliber') . '<br />';
echo '<b>Bullet Weight:</b> ' . get_field('bullet_weight') . '<br />';
echo '<b>Bullet Type:</b> ' . get_field('bullet_type') . '<br />';
echo '<b>Case Type:</b> ' . get_field('case_type') . '<br />';
echo '<b>Combined Reviews:</b> ' . get_field('combine_reviews') . '<br />';
}
add_action( 'woocommerce_single_product_summary', 'sbnai_display_acf_field_2', 31 );
function sbnai_display_acf_field_2() {
echo '<b>Width:</b> ' . get_field('width') . '<br />';
echo '<b>Height:</b> ' . get_field('height') . '<br />';
echo '<b>Depth:</b> ' . get_field('depth');
}
I’ve separated the fields based on relevance, group 1 if for Category “Ammo” the second group is for category “guns”.
Any help will be greatly appreciated.
Inside of your functions you need to get the product and then get the WC category that the product is in and then only create output when it is the right category.
Hello,
Thank you for getting back to me. I’m not sure that I understand either of the two explanations or at least I would not know how to incorporate that into the code that I am using. Any clarifications?
Hi
Please use below code. i think it will work for you.
$id = get_the_ID();
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$post_id = ‘product_cat_’.$term_id;
echo get_field(‘per_round_cost’, $post_id);
echo get_field(‘upc’, $post_id);
echo get_field(‘caliber’, $post_id);
echo get_field(‘bullet_weight’, $post_id);
echo get_field(‘bullet_type’, $post_id);
echo get_field(‘case_type’, $post_id);
echo get_field(‘combine_reviews’, $post_id);
Thanks to everyone for your input. I don’t think that the issue is being understood, so let me try to clarify. I have two categories: Ammo & Guns.
The first part of the code deals with fields related to Ammo, while the second part of the code is strictly for guns. The code below works well but it shows all the fields for both categories. So What I am looking to accomplish is as follows:
If category equals “Ammo” then
add_action( ‘woocommerce_single_product_summary’, ‘sbnai_display_acf_field_1’, 30 );
function sbnai_display_acf_field_1() {
echo ‘<b>Per Round Cost:</b> ‘ . get_field(‘per_round_cost’) . ‘<br />’;
echo ‘<b>UPC:</b> ‘ . get_field(‘upc’) . ‘<br />’;
echo ‘<b>Caliber:</b> ‘ . get_field(‘caliber’) . ‘<br />’;
echo ‘<b>Bullet Weight:</b> ‘ . get_field(‘bullet_weight’) . ‘<br />’;
echo ‘<b>Bullet Type:</b> ‘ . get_field(‘bullet_type’) . ‘<br />’;
echo ‘<b>Case Type:</b> ‘ . get_field(‘case_type’) . ‘<br />’;
echo ‘<b>Combined Reviews:</b> ‘ . get_field(‘combine_reviews’) . ‘<br />’;
}
Else if category equals “Guns” then
add_action( ‘woocommerce_single_product_summary’, ‘sbnai_display_acf_field_2’, 31 );
function sbnai_display_acf_field_2() {
echo ‘<b>Width:</b> ‘ . get_field(‘width’) . ‘<br />’;
echo ‘<b>Height:</b> ‘ . get_field(‘height’) . ‘<br />’;
echo ‘<b>Depth:</b> ‘ . get_field(‘depth’);
}
The issue is being understood. You are showing fields on a WC product page. While you are using ACF to hold those fields the conditions involved and the code required is specific to Woo Commerce (WC), not ACF.
In your function, you need to get the product ID as outlined in the first link I provided: https://stackoverflow.com/questions/27385920/woocommerce-get-current-product-id
Then you need to use that product ID to get the categories that the product is in as outlined in the second link I provided: https://stackoverflow.com/questions/15303031/woocommerce-get-category-for-product-page
Then you need to loop over that list of categories to see if you product is in a given category and display the fields based on that condition.
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.