Home › Forums › General Issues › Sorting WooCommerce Category Listing Alphabetically
Hey all,
I’m currently working on a little WooCommerce goodness and have encountered a slight snag with some custom code that I’m trying to implement.
The idea is that products (artworks) are associated with a single category (an artist), and I wanted to create an alphabetical “Directory Style” listing on one of the site’s pages that shows the categories listed under the first letter of the artist’s last name.
To that end, I set up an ACF field called “Sorting Letter” within WooCommerce product categories to store the letter, and that’s all working fine.
Using the code below, I’ve managed to spit out the categories and letter headings alright, but there are two major issues!
1: The letters aren’t in alphabetical order, and I’d much rather prefer it if they were!
2: Artists’ last names aren’t ordered alphabetically.
An example of what the code generates can be seen here!
http://quad.publik.co.uk/artists/
I’m slowly going mad trying to figure this out, so if anyone’s got any thoughts and code examples, that would be amazing!
Thanks in advance for any help with this!
Alex
// Taxonomy term links grouped by alphabets
function sk_alphabetical_archives( $atts ) {
// Pull in shortcode attributes and set defaults
$atts = shortcode_atts( array(
'taxonomy' => 'product_cat'
), $atts, 'alphabetical-archives' );
// https://codex.wordpress.org/Function_Reference/sanitize_key
$taxonomy = sanitize_key( $atts['taxonomy'] );
$jump_links = '';
$list = '';
// get all the terms and store in $terms variable
$terms = get_terms( 'product_cat' );
$groups = array();
if ( $terms && is_array( $terms ) ) {
// for each term
foreach( $terms as $term ) {
// obtain the first letter and store it in $first_letter variable
$get_letter = get_field('sorting_letter', $term);
$first_letter = strtoupper($get_letter);
// set the key of $groups array to the first letter and its corresponding value as the term. Remember array has key => value pairs.
$groups[ $first_letter ][] = $term;
}
if ( ! empty( $groups ) ) {
$list .= '<div class="term-groups">';
foreach( $groups as $letter => $terms ) {
$list .= '<div class="terms-group" id="'. $atts['taxonomy'] . '-' . $letter .'"><h2>' . apply_filters( 'the_title', $letter ) . '</h2>';
$list .= '<ul class="row alphabet-group">';
foreach( $terms as $term ) {
$list .= '<li class="col-xxs-12 col-xs-6 col-sm-4 col-md-3"><a href="' . esc_url( get_term_link( $term ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts filed under %s', 'my_localization_domain' ), $term->name ) ) . '">' . $term->name . '</a> <em>(' . $term->count. ')</em></li>';
}
$list .= '</ul></div>';
}
$jump_links_html .= '<div class="alphabetical-terms"><div class="jump-links" id="'. $taxonomy .'_top">' . $jump_links . '</div>';
$list .= '</div></div>';
}
}
else {
$list .= 'Sorry, no items were found
';
}
return $jump_links_html . $list;
}
add_shortcode( 'alphabetical-archives', 'sk_alphabetical_archives' );
Hey @alexstanhope – I checked this out now and that URL appears to be properly alphabetical as you say you wanted it.
Safe to assume you fixed this problem?
The topic ‘Sorting WooCommerce Category Listing Alphabetically’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.