Ask your WordPress questions! Pay money and get answers fast! (more info)

Snippet to show lowest price of a product in a category WordPress

  • SOLVED

In Woo-commerce the default shop page can be set to show the products by their categories. When doing so it lists their thumbnail/title/link/how many products are available.

It does not however show price.

The goal is to list out the product categories and list the starting price of their products. For example: "Category-1 priceing starts at $9.99" (category-1 cheapest products is 9.99). "Category-2 pricing starts at $3.99" (so the cheapest product in that category is 3.99).

Below is the default template that is used for that page:


<?php
/**
* The template for displaying product category thumbnails within loops.
* Override this template by copying it to yourtheme/woocommerce/content-product_cat.php
*/

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $woocommerce_loop;

// Store loop count we're currently on
if ( empty( $woocommerce_loop['loop'] ) )
$woocommerce_loop['loop'] = 0;

// Store column count for displaying the grid
if ( empty( $woocommerce_loop['columns'] ) )
$woocommerce_loop['columns'] = apply_filters( 'loop_shop_columns', 4 );

// Increase loop count
$woocommerce_loop['loop']++;
?>
<li class="product-category product<?php
if ( ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] == 0 || $woocommerce_loop['columns'] == 1 )
echo ' first';
if ( $woocommerce_loop['loop'] % $woocommerce_loop['columns'] == 0 )
echo ' last';
?>">

<?php do_action( 'woocommerce_before_subcategory', $category ); ?>

<a href="<?php echo get_term_link( $category->slug, 'product_cat' ); ?>">

<?php
/**
* woocommerce_before_subcategory_title hook
*
* @hooked woocommerce_subcategory_thumbnail - 10
*/
do_action( 'woocommerce_before_subcategory_title', $category );
?>

<h3>
<?php
echo $category->name;

if ( $category->count > 0 )
echo apply_filters( 'woocommerce_subcategory_count_html', ' <mark class="count">(' . $category->count . ')</mark>', $category );
?>
</h3>

<?php
/**
* woocommerce_after_subcategory_title hook
*/
do_action( 'woocommerce_after_subcategory_title', $category );
?>

</a>

<?php do_action( 'woocommerce_after_subcategory', $category ); ?>

</li>

Answers (2)

2014-12-08

Dbranes answers:

Hi, I'm not sure I understand the setup, but here's one idea that you could try to play with:

<?php echo 'minprice: ' . wpq_get_min_price_per_product_cat( $product_cat_id = 9 ); ?>


where:

/**
* Get the minimum price per product category.
*
* @see http://www.wpquestions.com/question/showChronoLoggedIn/id/10243
*/

function wpq_get_min_price_per_product_cat( $term_id ) {

global $wpdb;

$sql = "
SELECT MIN( meta_value+0 ) as minprice
FROM {$wpdb->posts}
INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)
INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)
WHERE
( {$wpdb->term_relationships}.term_taxonomy_id IN (%d) )
AND {$wpdb->posts}.post_type = 'product'
AND {$wpdb->posts}.post_status = 'publish'
AND {$wpdb->postmeta}.meta_key = '_price'
";

return $wpdb->get_var( $wpdb->prepare( $sql, $term_id ) );
}



Mabye you can use the filters like (untested):

add_action( 'woocommerce_after_subcategory', 'wpq_after_subcategory' );

function wpq_after_subcategory( $category ) {

if( function_exists( 'wpq_get_min_price_per_product_cat' ) ) {
printf( "%s pricing starts at %s ", $category->name, wpq_get_min_price_per_product_cat( $category->term_id ) );
}
}


Notice that you might want to consider to cache the minimum price for each product category.


Rory Heaney comments:

Very impressed!

This works 100%, i'm going to assign you the prize money. I had already created a variable for the category ID's so this was plug in and play.

Can you please write a small description of what is going on in the function so I can get a better Idea of what you did?

2014-12-08

Romel Apuya answers:

try this

<?php
/**
* The template for displaying product category thumbnails within loops.
*
* Override this template by copying it to yourtheme/woocommerce/content-product_cat.php
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce_loop,$woocommerce;
// Store loop count we're currently on
if ( empty( $woocommerce_loop['loop'] ) )
$woocommerce_loop['loop'] = 0;
// Store column count for displaying the grid
if ( empty( $woocommerce_loop['columns'] ) )
$woocommerce_loop['columns'] = apply_filters( 'loop_shop_columns', 4 );
// Increase loop count
$woocommerce_loop['loop']++;
?>
<li class="product-category product<?php
if ( ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] == 0 || $woocommerce_loop['columns'] == 1 )
echo ' first';
if ( $woocommerce_loop['loop'] % $woocommerce_loop['columns'] == 0 )
echo ' last';
?>">
<?php do_action( 'woocommerce_before_subcategory', $category ); ?>

<a href="<?php echo get_term_link( $category->slug, 'product_cat' ); ?>">
<?php
/**
* woocommerce_before_subcategory_title hook
*
* @hooked woocommerce_subcategory_thumbnail - 10
*/
do_action( 'woocommerce_before_subcategory_title', $category );
?>
<h3>
<?php
$o =0;
$args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $category->name, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php
$prices[$o] = floatval($product->price);
$o++;
?>
<?php endwhile; ?>
<?php wp_reset_query();
sort($prices);
?>
<?php echo $category->name; ?>
<?php
if ( $category->count > 0 )
echo apply_filters( 'woocommerce_subcategory_count_html', ' <mark class="count">(' . $category->count . ')</mark>', $category );
?>
<?php echo 'Priceing starts at $'.$prices[0]; ?>
</h3>
<?php
/**
* woocommerce_after_subcategory_title hook
*/
do_action( 'woocommerce_after_subcategory_title', $category );
?>
</a>
<?php do_action( 'woocommerce_after_subcategory', $category ); ?>
</li>