Tagged: categories, woocommerce
-
AuthorPosts
-
November 26, 2015 at 11:54 am #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
KosNovember 26, 2015 at 6:25 pm #543335Hey 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!
ElliottNovember 26, 2015 at 6:58 pm #543347Thanks 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 showCategories: Audi, Car, Q5
whereas I want to have it based on hierarchy
Categories: Car, Audi, Q5
Does it makes sense?
Regards
KosNovember 26, 2015 at 10:05 pm #543410Hey!
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,
ElliottNovember 27, 2015 at 10:25 am #543512Hi 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.
December 1, 2015 at 1:39 pm #545143Hi!
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 -
AuthorPosts
- The topic ‘sort categories on woocommerce single product page’ is closed to new replies.