0

I'm trying to display a list of all product categories and the price range of the products within those categories.

E.g

Headwear – £30-£150

Shoes – £35-£300

Where "Headwear" is the Product Category Name, "£30" is the lowest price of a product within that category and "£150" is the highest.

So far I have used the following code to list the categories, but not sure how to check for products within each and get the prices etc.

$order = 'asc';
$hide_empty = false ;
$cat_args = array(
    'orderby'    => $orderby,
    'order'      => $order,
    'hide_empty' => $hide_empty,
);
 
$product_categories = get_terms( 'product_cat', $cat_args );
 
if( !empty($product_categories) ){
    echo '
 
<ul>';
    foreach ($product_categories as $key => $category) {
        echo '
 
<li>';
        echo '<a href="'.get_term_link($category).'" >';
        echo $category->name;
        echo '</a>';
        echo '</li>';
    }
    echo '</ul>
 
 
';
}

Any help would be greatly appreciated. I've asked this on the woocommerce support forum but no responses.

1 Answer 1

0

You can get using SQL query -

global $wpdb;
 $results = $wpdb->get_col( "
        SELECT pm.meta_value
        FROM {$wpdb->prefix}term_relationships as t
        INNER JOIN {$wpdb->prefix}term_taxonomy as tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
        INNER JOIN {$wpdb->prefix}terms as t ON tr.term_taxonomy_id = t.term_id
        INNER JOIN {$wpdb->prefix}postmeta as pm ON tr.object_id = pm.post_id
        WHERE tt.taxonomy LIKE 'product_cat'
        AND t.term_id = {$term->term_id}
        AND pm.meta_key = '_price'
    ");

    sort($results, SORT_NUMERIC);
print_r($results);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.