Home › Forums › General Issues › Show the ACF field in WooCommerce Category inside the Shop page
I’ve been cracking my brain over this being a non-programmer. And have gotten so far as to retrieving either the product ID or category ID for each product in the Shop page.
But I’m stumped on how to combine them to get my ACf field to show.
Here’s what I got so far..
function show_shop_suffix() {
// If shop page
if ( is_shop() ) {
global $product;
// to get product ids
//$product_id = $product -> get_id();
// to show product arrays
//$terms = get_the_terms( $product_id, 'product_cat' );
// get the product category ids
$product_cat_id = wc_get_product_term_ids( $product -> get_id(), 'product_cat' );
// vars to get the ACF field and product ID
$sugo_loop_item_suffix = get_field( 'product_archive_suffix', 'product_cat' . '_' . $product_cat_id );
}
echo ($sugo_loop_item_suffix);
var_dump ($product_cat_id);
}
add_action( 'woocommerce_after_shop_loop_item', 'show_shop_suffix', 5 );
Entering this code via Code Snippets, instead of a child theme and modifying the WooCommerce template.
Hey John, thank you for helping me out with this.
Buddy of mine who’s more experienced in development helped me out already. And here was the final code being used now:
function sd_loop_product_cat_note() {
// Get the category terms
$terms = get_the_terms( $post->ID, 'product_cat' );
// Only proceed if there is at least 1 category
if ( $terms && ! is_wp_error( $terms ) ) {
// Get the first term
$first_term = $terms[0];
// Get the product note for that term
$sd_loop_item_suffix = get_field( 'sd_product_archive_suffix', 'product_cat_' . $first_term -> term_id );
// Output the product note
echo $sd_loop_item_suffix;
}
}
add_action( 'woocommerce_after_shop_loop_item', 'sd_loop_product_cat_note', 5 );
This code displays the needed ACF field value in the Shop loop and Category loop in my website. (^_^)
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.