Alphabetical woocommerce pagination in products categories

Viewed 69

I'm trying to do an alphabetical pagination of categories on the home page of a Woocommerce store, but it only works in products, and not in categories.

Here is the code in function.php:

function os_restrict_by_first_letter( $where ) {
    if ( isset( $_GET['az'] ) ) {
        global $wpdb;
        if ( ! is_tag() && ! is_date() && is_archive() && is_main_query() ) {
            $where .= $wpdb->prepare( " AND SUBSTRING( {$wpdb->posts}.post_title, 1, 1 ) = %s ", sanitize_text_field( wp_unslash( $_GET['az'] ) ) );
        }
    }
    return $where;
}
add_filter( 'posts_where', 'os_restrict_by_first_letter' );

Here is the code in archive-product.php:

<?php if ( ! is_tag() && ! is_date() ) : ?>
    <nav aria-label="Alphabet" class="mb-4">
        <ul class="pagination pagination-sm flex-wrap justify-content-center">
            <?php
            $the_cat_id = get_queried_object_id();
            $az_range_arr = range( 'a', 'z' );

            foreach ( $az_range_arr as $letter ) :
                ?>
                <li class="page-item"><a class="page-link text-uppercase" href="<?php echo esc_url( add_query_arg( 'az', $letter, get_category_link( $the_cat_id ) ) ); ?>"><?php echo esc_html( $letter ); ?></a></li>
                <?php
            endforeach;
            ?>
            <li class="page-item"><a class="page-link text-uppercase" href="<?php echo esc_url( get_category_link( $the_cat_id ) ); ?>">Все</a></li>
        </ul>
    </nav>
<?php endif; ?>

What to change to work in product categories?

0 Answers
Related