Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #543020

    Hi,

    I would be grateful for some of your much valued help/advice…

    I am looking to sort categories that are displayed on a woocommerce single product page by the hierarchical order and not alphabetical. I know its the meta.php file that needs adapting but cannot identify how to do it.

    Can you point me in the right direction please?

    Many thanks
    Kos

    #543335

    Hey koszachos!

    I’m not really sure what you mean by “hierarchical order”. Do you mean you have some products set as sub categories?

    You can change the default product sorting order in Dashboard > Woocommerce > Products > Display > Default Product Sorting.

    Cheers!
    Elliott

    #543347

    Thanks Elliot, but what i mean is something else, see image https://www.dropbox.com/s/l0hgmcmazj42jht/Screenshot%202015-11-26%2018.49.29.png?dl=0
    basically I have categories (e.g. Car), sub-categories (e.g. Audi and VW) as well as sub-sub-categories (e.g. S4 and Q5). At the moment the order in which the categories are shown on the product page is alphabetical, e.g. if a product was related to Q5 then it would show

    Categories: Audi, Car, Q5

    whereas I want to have it based on hierarchy

    Categories: Car, Audi, Q5

    Does it makes sense?

    Regards
    Kos

    #543410

    Hey!

    I see what you mean now. It’s more of a Woocommerce question than Enfold though so it would be best to contact Woocommerce support to see if it’s possible.

    I found this in the Woocommerce support forums which may help, https://support.woothemes.com/hc/communities/public/questions/202932106-Single-product-category-display.

    Regards,
    Elliott

    #543512

    Hi Elliot

    just thought I post a solution that I figured out myself.
    The below code needs to be pasted in woocommerce/templates/single-product/meta.php and delete or comment the following line
    echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', $cat_count, 'woocommerce' ) . ' ', '</span>' );

    $terms = get_the_terms( $post->ID, 'product_cat' );
    $args = array(
                       'type'                     => 'product',
                        'child_of'                 => 0,
                        'parent'                   => '',
                        'orderby'                  => 'term_group',
                        'order'                    => 'ASC',
                        'hide_empty'               => false,
                        'hierarchical'             => 1,
                        'exclude'                  => '',
                        'include'                  => '',
                        'number'                   => '',
                        'taxonomy'                 => 'product_cat',
                        'pad_counts'               => false
    );
    
    $categories = get_categories( $args );
    $category_list_Names = array();
    $category_list_Slugs = array();
    foreach($categories as $cat ){
    	foreach ($terms as $term ) {
      	  if ($cat->name == $term->name && !in_array($cat->name, $category_list_Names)) {
          	$category_list_Names[] = $cat->name;
          	$category_list_Slugs[] = get_term_link($cat->slug, 'product_cat');
          	break;
          	}
    	}
    }
    
    $category_list = array();
    for ($x = 0; $x <= count($category_list_Names); $x++) {
    	$category_list[] = '<a href="'. $category_list_Slugs[$x] .'">'. $category_list_Names[$x] .'</a>';
    }
    
    	if( ! empty( $category_list ) ) {
    		echo '<span class="posted_in">';
    		echo _n( 'Category: ', 'Categories: ', sizeof( $category_list ) );
    		echo implode( ',', $category_list );
    		echo '</span>';
    	}

    Regards
    Kos

    • This reply was modified 8 years, 11 months ago by koszachos.
    #545143

    Hi!

    thanks a lot for sharing your solutions with us!
    Let us know in a new ticket if you have some more questions related to the theme. We are happy to assist you.

    Cheers!
    Andy

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘sort categories on woocommerce single product page’ is closed to new replies.