-
-
Save kloon/4218605 to your computer and use it in GitHub Desktop.
// [product_count] shortcode | |
function product_count_shortcode( ) { | |
$count_posts = wp_count_posts( 'product' ); | |
return $count_posts->publish; | |
} | |
add_shortcode( 'product_count', 'product_count_shortcode' ); |
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)"?
@pipolocosisi you can check it here : https://gist.github.com/LetsCommit/c821b96845998375769dcabfcc9c8ff5
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.
Use example: [products-counter category="Apple"]