• Resolved botch

    (@botch)


    Hi,

    I am trying to create a list of categories and the price range of the products within those categories.

    E.g

    Headwear – £30-£150
    Shoes – £35-£300

    etc.

    Currently, I have been able to successfully display the categories, but am stuck on how to now pull and display the prices at the same time.

    I’m fairly new to WC so any help would be greatly appreciated, especially if there is a built in way of doing this.

    Here is the code so far for pulling and displaying the categories

    <?php
    
    // Taxonomy for product categories
    $taxonomy = 'product_cat';
    
    // Get parent product categories
    $parent_product_cats = get_terms( $taxonomy, array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) );
    
    if( !empty($parent_product_cats) ){
        echo '
     
    <ul>';
        foreach ($parent_product_cats as $key => $category) {
            echo '
     
    <li>';
            echo '<a href="'.get_term_link($category).'" >';
            echo $category->name;
            echo '</a>';
            echo '</li>';
        }
        echo '</ul>
     
    ';
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • You’re half way there. Basically you now need to get the products that belong to the category and their prices, after that simply determine min-max prices:

    $orderby = 'name';
    $order = 'asc';
    $hide_empty = false ;
    $cat_args = array(
        'orderby'    => $orderby,
        'order'      => $order,
        'hide_empty' => $hide_empty,
        'parent' => 0
    );
     
    $product_categories = get_terms( 'product_cat', $cat_args );
     
    if( !empty($product_categories) ){
        echo '
     
    <ul>';
        foreach ($product_categories as $key => $category) {
    
            // Get all products from this category
            // https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
            $products = wc_get_products(array(
                'category' => array($category->slug),
                'limit' => -1
            ));
    
            $all_prices = array();
    
            //get all prices from this category
            foreach ($products as $product) {
                $all_prices[] = (float)$product->get_price();
            }
    
            //Get minimum & maximum value from the price array
            $min_price = min($all_prices);
            $max_price = max($all_prices);
    
            echo '
     
    <li>';
            echo '<a href="'.get_term_link($category).'" >';
            echo $category->name . '('.$min_price.' - '.$max_price.')'; // changed here
            echo '</a>';
            echo '</li>';
        }
        echo '</ul>';
    }
    Mirko P.

    (@rainfallnixfig)

    Hi there,

    We’ve not seen any activity on this thread for a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution and the code from stefanue was helpful. If you have further questions, please feel free to open a new topic.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display a list of Categories and show the price range of their products’ is closed to new replies.