Skip to content
Advertisement

WooCommerce product custom additional taxonomy in a loop issue

I am trying to display custom taxonomy field’s names and descriptions for single products in a loop.

I’ve created custom product taxonomy

add_action( 'init', 'create_distinctions_nonhierarchical_taxonomy', 0 );
 
function create_distinctions_nonhierarchical_taxonomy() {
 
// Labels part for the GUI
 
  $labels = array(
    'name' => _x( 'Distinction', 'taxonomy general name' ),
    'singular_name' => _x( 'Distinction', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Distinction' ),
    'popular_items' => __( 'Popular Distinction' ),
    'all_items' => __( 'All Distinction' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Distinction' ), 
    'update_item' => __( 'Update Distinction' ),
    'add_new_item' => __( 'Add New Distinction' ),
    'new_item_name' => __( 'New Distinction Name' ),
    'separate_items_with_commas' => __( 'Separate distinctions with commas' ),
    'add_or_remove_items' => __( 'Add or remove distinctions' ),
    'choose_from_most_used' => __( 'Choose from the most used distinctions' ),
    'menu_name' => __( 'Distinctions' ),
  ); 
 
// Now register the non-hierarchical taxonomy like tag
 
  register_taxonomy('distinctions',array('product'),array(
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'distinctions' ),
  ));
}

next created sample tags: One, Two and assigned them to the Product (via ACF plugin)

I want them to be displayed on product page. I was trying to use this code:

<?php
add_action( 'woocommerce_product_meta_end', 'action_product_meta_end' );
function action_product_meta_end() {
global $post;
$terms = get_the_terms( $post->ID , 'distinctions' ); 
foreach ( $terms as $term ) {
?>
    <ul>
        <li>
            <h5><?php echo $term->name; ?></h5>
            <p><?php echo $term->description; ?></p>
        </li>
    </ul>

<?php
}}
?>

but there is an issue I cannot address:

Warning: foreach() argument must be of type array|object, bool given in

What am I doing wrong?

Advertisement

Answer

Update (related to your comment)

You need to check that $terms variable is not empty before in your last function and also that each term exists like:

add_action( 'woocommerce_product_meta_end', 'action_product_meta_end' );
function action_product_meta_end() {
    global $post;

    $taxonomy = 'distinction';
    $terms    = get_the_terms( $post->ID , $taxonomy ); 

    if ( $terms && ! empty( $terms ) ) {
        foreach ( $terms as $term ) {
            if ( term_exists( $term, $taxonomy ) ) {
                ?>
                <ul>
                    <li>
                        <h5><?php echo $term->name; ?></h5>
                        <p><?php echo $term->description; ?></p>
                    </li>
                </ul>
                <?php
            }
        }
    }
}

This should solve the issue.

You If your otheer issue persist, you can also try to replace from my code:

if ( term_exists( $term, $taxonomy ) ) {

with:

if ( is_a( $term, 'WP_Term' ) ) {
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement