我需要找出WordPress WooCommerce中添加的产品总数。我怎样才能得到它?
通过以下代码,您可以获取woo-commerce中的产品数量。
在主题函数.php文件中添加此函数
function get_productcount() {
$product_count = 0;
// loop through all categories to collect the count.
foreach (get_terms('product_cat') as $term)
$product_count += $term->count;
return $product_count;
}
现在,从您的任何主题页面中,您可以调用此函数。在我的示例中,我在标头中添加了以下代码行.php如,
<?php echo your_product_count() ?>
或者以其他方式,您可以直接添加模板
$terms = get_terms( 'product_cat' );
foreach( $terms as $term )
{
$product_count += $term->count;
echo 'Product Category: '
. $term->name
. ' - Count: '
. $term->count;
}
echo "Total count:". $product_count;