Skip to content

Instantly share code, notes, and snippets.

@kloon
Created December 5, 2012 19:13
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kloon/4218605 to your computer and use it in GitHub Desktop.
Save kloon/4218605 to your computer and use it in GitHub Desktop.
WooCommerce Product Count Shortcode
// [product_count] shortcode
function product_count_shortcode( ) {
$count_posts = wp_count_posts( 'product' );
return $count_posts->publish;
}
add_shortcode( 'product_count', 'product_count_shortcode' );
@thisisbbc
Copy link

Hi,
If we'd like to narrow down the count to only a specific product category, could you guide me as to how I would do that? Thank you.

@dpm39560
Copy link

dpm39560 commented Jun 4, 2016

Ditto the request for a shortcode to provide a count of products in a designated category. For example, the shortcode call could look like this:

[woocommerce_product_category_count category="christmas"]

Many thanks to whoever can help with this.

@CHADREX
Copy link

CHADREX commented Nov 14, 2019

Ditto the request for a shortcode to provide a count of products in a designated category. For example, the shortcode call could look like this:

[woocommerce_product_category_count category="christmas"]

Many thanks to whoever can help with this.

You can use:
// [products-counter category="28"]
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 '';
}

@SevenAxis
Copy link

SevenAxis commented Dec 30, 2020

Nice! Thank you, is there an easy way to only count IN STOCK products?

Edit: Found a way, here's the code in case anyone needs it.

add_shortcode( 'products-counter', 'products_counter' );
function products_counter( $atts ) {
	
	$atts = shortcode_atts( [
        'category' => '',
    ], $atts );
	
	$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => $atts['category']
        )
    ),
    'meta_query' => array(
        array(
            'key' => '_stock_status',
            'value' => 'instock'
        ),
    )
);

$query = new WP_Query($args);
$inStockCount = $query->found_posts;

    if ( $query && ! is_wp_error( $query ) ) {
		return $inStockCount;
    }
    return '';
}

Use example: [products-counter category="Apple"]

@pipolocosisi
Copy link

Ditto the request for a shortcode to provide a count of products in a designated category. For example, the shortcode call could look like this:
[woocommerce_product_category_count category="christmas"]
Many thanks to whoever can help with this.

You can use:
// [products-counter category="28"]
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 '';
}

I'd like to use this to bypass the woocommerce product counter (which is not displayed for obscure reasons)

How can I add static text in the result to be displayed like "There are X product(s)"?

@vkartk
Copy link

vkartk commented Jan 17, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment