-
-
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.
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 '';
}
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"]
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
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.