'Show product count in archive page Title in WooCommerce

How to show in the archive page TITLE, the number of products, in WooCommerce? Thanks!



Solution 1:[1]

If you want to change the title of the page:

Try this:

add_filter( 'woocommerce_page_title', function( $title = '' ) {
    if ( is_tax('product_cat') ) {
        $product_cat_obj = get_queried_object();
        $title .= !empty($product_cat_obj->count) ? ' '.$product_cat_obj->count : '';
    }
    return $title;
});

If you want to change the meta title:

Ok, then you could use this code (if you want to get only title and number):

add_filter( 'pre_get_document_title', function( $title = '' ) {
    if ( is_tax( 'product_cat' ) ) {
        $product_cat_obj = get_queried_object();
        $title = !empty( $product_cat_obj->name ) ? $product_cat_obj->name : '';
        $title .= !empty( $product_cat_obj->count ) ? ' '.$product_cat_obj->count : '';
    }
    return $title;
}, 10);

Or this code (if you want to get title + number + separator + sitename):

add_filter( 'document_title_parts', function( $title = array() ) {
    if ( is_tax('product_cat') ) {
        if( !empty( $title['title'] ) ){
            $product_cat_obj = get_queried_object();
            $title['title'] = !empty( $product_cat_obj->count ) ? $title['title'].' '.$product_cat_obj->count : '';
        }
    }
    return $title;
}, 10);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1