2
votes

I am having trouble adding the category description for my products at the end of each description for each product at the 'description' tab beneath each item. So far, my function (at bottom) puts ALL category descriptions instead of the one category the product s actually in.

Here's the example of the category with description page.

Sample product page with description (Starting at: Stories Native American stories and legends have traditionally served... ...)

This is the code I am using to combine two functions. It works to add all category descriptions but I am trying to make it only show the one relevant description:

add_filter( 'the_content', 'customizing_woocommerce_description' );
function customizing_woocommerce_description( $content ) {

    // Only for single product pages (woocommerce)
    if ( is_product() ) {

        // The custom content
        $args  = array( 'taxonomy' => 'product_cat' );
        $terms = get_terms('product_cat', $args);

        $count = count($terms); 
        // if ($count > 0) {

            foreach ($terms as $term) {
                /*  echo $term->description;*/
                $custom_content = '<p class="custom-content">' . __($term->description, "woocommerce").'</p>';

                // Inserting the custom content at the end
                $content .= $custom_content;
            }
        // }
    }
    return $content;
}

https://pastebin.com/GHBxe23i

So far I am implementing the code in a non destructive php plugin for wordpress.

1

1 Answers

2
votes

You should replace in your code get_terms() by wp_get_post_terms() this way:

add_filter( 'the_content', 'woocommerce_custom_product_description', 20, 1 );
function woocommerce_custom_product_description( $content ) {
    global $post;

    // Only for woocommerce single product pages
    if ( ! is_product() ) 
        return $content; // exit

    $terms =  wp_get_post_terms( $post->ID, 'product_cat' );
    if ( count( $terms ) == 0 ) 
        return $content; // exit

    foreach ( $terms as $term )
        $content .= '<p class="custom-content">' . $term->description . '</p>';

    return $content;
}

Code goes in function.php file of your active child theme (or theme). Tested and works.