Display term from custom taxonomy in single product using ACF

Hello,

Using CPT UI I create a custom taxonomy for Woo products, let’s call it CPT Brands (slug: cpt_brands).
Then using ACF I create the field ACF Note for the new taxonomy.
Under CPT Brands I create the term Company and add a text in the field ACF Note.
I edit a product and associate it with the brand Company.

Hooking into the single product I now wish to output the ACF Note using ACF:

add_action('woocommerce_after_add_to_cart_form','extra_product_info', 100);

if (!function_exists('extra_product_info')) {

  function extra_product_info() {

	echo '<pre>' . print_r(get_the_ID()) . '</pre>';
	echo '<pre>' . print_r(get_field('acf_note')) . '</pre>';

  }
}

It doesn’t work. What am I doing wrong?

Thanks!

Hello Gabriel,

Thanks for writing in!

Please be advised that the_field() and get_field() functions displays the value of a specific field which was added in the post or product item. You cannot use it to display additional field of a certain taxonomy. Please check out this documentation first:

Hope this helps.

Hi,

Thanks for the reply! I found a solution, get_field() can be used for this purpose by referencing the correct taxonomy. The taxonomy terms can be fetched using wp_get_post_terms():
https://codex.wordpress.org/Function_Reference/wp_get_post_terms

add_action('woocommerce_after_add_to_cart_form','extra_product_info', 100);

if (!function_exists('extra_product_info')) {

	function extra_product_info() {

		global $product;
		$terms = wp_get_post_terms($product->id, 'brand_cpt', array("fields" => "all"));

		echo '<pre>';
		echo 'Brand: ' . $terms[0]->name . '<br>';
		echo '<img src="' . get_field('acf_logo', 'brand_cpt_'.$terms[0]->term_id) .'" width="100">';
		echo '</pre>';

	}
}

Glad it’s working okay now, and thanks for sharing!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.