Using categories in WooCommerce

I have an element on product pages that will show a list of subcategories that have the same parent, but if the parent has no subcategories then no list to be shown.

Which means that this element will be only on products of a sub category, and not products of most top level categories.

Have implemented the following code from chat:

But now getting the following error:
syntax error, unexpected '}', expecting end of file in line 28

You can find the template file where the above code is implemented in the chat notes.

  • Lindeni Mahlalela

    Hello talk

    I hope you are having a great day today and thank you for contacting us.

    I have looked at the code and noticed there was an extra closing '}' on line 28 which was causing an end of file error. The following is the final code without the extra '}'.

    global $product;
    $args = array();
    $category = get_the_category($product->id);
    if ( has_parent($category->id) ) {
        $parent =  $category->category_parent;
        //get categories of parent
        $args = array(
            'parent' => $parent
        //get categories whose parent is $parent
        $categories = get_categories($args);
        //OR use get terms like so:
        $terms = get_terms( 'product_cat', $args );
        if ( $terms ) {
            echo '<ul class="product-cats">';
    		foreach ( $terms as $term ) {
    			echo '<li class="category">' . $term->name .'</li>';
    		echo '</ul>';
    function has_parent($catid){
        $category = get_category($catid);
        if ($category->category_parent > 0){
            return true;
        return false;

    The error should not appear after updating the code as above. Please note that if you are adding this code to an existing file you should remember the opening and closing php tags:
    <?php ?>
    The code above is missing the closing php tag '?>', it will run if it is the only code in the file and if it is the last in the file, if it is between some other code in the file then consider removing or adding the tags as relevant to avoid breaking your site. If you are pasting into html then you must have the closing tag '?>', if its all PHP, then one opening and one closing tag should be sufficient in the whole file.

    I hope this helps. Please let us know if you have any more issues or need help with something else and we will be happy to help in anyway possible.


Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.