0

I'm trying to accomplish the following scenario:

Show the number of products based on category and when applying filters then dynamically change the count of products on that page.

I'm using the snippet below

add_shortcode( 'products-counter', 'products_counter' );

function products_counter( $atts ) {
    $atts = shortcode_atts( [
        'category' => '',
    ], $atts );

    $taxonomy = 'product_cat';
    if ( is_numeric( $atts['category'] ) ) {
        $cat = get_term( $atts['category'], $taxonomy );
    } else {
        $cat = get_term_by( 'slug', $atts['category'], $taxonomy );
    }

    if ( $cat && ! is_wp_error( $cat ) ) {
        return $cat->count;
    }
    return '';
}

On a page I'm using the [products-counter category="dresses"] for example to show the number of products from the Dresses category.

It seems that work perfectly fine but the problem is each time I'm applying some filters the number doesn't change.

How can expand the snippet in order to change the count dynamically after filters are applied?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.