I need to add some information about the category in Woocommerce category page. I added the following code in my theme functions.php, but it didn’t work. Not even the first echo that I use to debug if the hook is working was displayed. What am I doing wrong?
//Adiciona informações da categoria na página de categoria
function brg_informacoes_de_categoria(){
echo 'it works!';
if(is_product_category()){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
$single_cat = array_shift( $product_cats );
$field_cod = get_field('codigo', $single_cat->ID);
$field_teatro = get_field('teatro', $single_cat->ID);
$field_info = get_field('informações', $single_cat->ID);
$field_cidade = get_field('cidade', $single_cat->ID);
$field_estado = get_field('estado', $single_cat->ID);
$field_inicio = get_field('inicio', $single_cat->ID);
$field_termino = get_field('termino', $single_cat->ID);
echo 'Código do Evento' . $field_cod . '<br >';
echo 'Teatro' . $field_teatro . '<br >';
echo 'Informações' . $field_info . '<br >';
echo 'Cidade' . $field_cidade . '<br >';
echo 'Estado' . $field_estado . '<br >';
echo 'De ' . $field_inicio . ' a ' . $field_termino . '<br >';
}
add_action('woocommerce_before_main_content','brg_informacoes_de_categoria', 2 );
It appears that you’ve overwritten the woocommerce single-product.php template in your theme, and your single-product.php file is lacking the following code:
So you just have to add this line somewhere in your single-product.php file in the woocommerce directory of your theme.
Another option is to delete the single-product.php file – then WooCommerce will use the default single-product.php file, which contains this line.
UPDATE
You’re trying to hook your function too early. Try using priority 10 or higher when you attach your function.
To do this, replace the following line:
with the following: