Ive been hard at work trying to build some functions to display products on my site. The prices for these products are calculated based on many variables.

I think the first set of functions work, but I'm not sure about the best place to call these functions to prevent multiple database calls. I'm not sure if these functions will cache the value by simply being set in the functions file, or if they should be called in the header or elsewhere.

EDIT: Although I feel like i have a lot of calls to the database with all the get_post_meta and such, I suppose my biggest concern should be making the functions work. As currently some do not. I know the first function works, but I'm not sure the best place to call it. (header, or per page)

All the functions rely on values from another function as well. And a few I am having problems trying to structure correctly.

I am also currently learning about classes and methods and wondering if some of these would be better suited in that format. If so, I could use some assistance setting that up.

Here are my functions so far.

NOTE: value $m_type = get_post_meta(get_the_ID(), 'm_type', true); will be called in the template file.

  1. check the client level from user meta. I'm thinking this should be called in the header so client level is defined globally.

    function get_client_lvl () 
        {$user_id = get_current_user_id();
        global $client_lvl;
        If ($user_id == 0)
        {$client_lvl= 'high';}
        {$client_lvl=get_user_meta($user_id, wpcf-client-lvl, true); }
        }//End function
  2. Used on collection and product pages to determine which multiplier value to use for calculation based on client level

    function get_multiplier($client_lvl)
      $p_high= get_post_meta(get_the_ID(),'p_high','single');
    $p_med= get_post_meta(get_the_ID(),'p_med','single');
    $p_low =get_post_meta(get_the_ID(),'p_low','single');
          if ($client_lvl == 'med')
    {$prices=array('high'=>$p_high,'med'=> $p_med);}
    elseif ($client_lvl == 'low')
    {$prices=array('high'=>$p_high,'low'=> $p_low);}
    }//End function
  3. These values will be used in another function to limit the number of images that a user can select for their product.

    $img_min= get_post_meta(get_the_ID(),'img_min','single');
    $img_max= get_post_meta(get_the_ID(),'img_max','single');
  4. These are the formulas that are used to calculate the displayed price. I am not sure how to handle the image and image+commission options as they use the number of selected images to determine the price. But if no images are selected, I want it to display "This is the price/ image. One idea I thought of was to include the echo statements inside the switch options, but Im not sure how to handle the array. If the client level is High the function will only loop through once and will not have a was now price. but if it is anything else, then it will loop through twice and will have the was now echo statement. If I didnt have the special case in the image based calculations, then the was now function would work just fine. But I'm not sure where to put the conditional echo's for those functions.

    function calculate_price($m_type)
    global $cogs;
        $cogs= get_post_meta(get_the_ID(),'cogs','single');
        $prices= get_multiplier();
            Foreach($prices as $multiplier)
        switch ($m_type) {
             case 'Area':
                 $area= get_post_meta(get_the_ID(),'area','single');
                 $total[]= $multiplier * $area;
             case 'Image':
                 {$total[]= $multiplier  * $cogs * $img_count;}
             {$total[]= $multiplier  * $cogs ;} // Displayed as  $price/ Image using $img_count
             case 'Commission':
                 $total[]= $multiplier + $cogs;
             case 'Flat':
                 $total[]= $multiplier;
                     case 'Commission+Image':
                     {$total[]= $multiplier + ($img_cost*$img_count);}
                     {$total[]= $multiplier; } //Display "Price: ".$multiplier. "+".$img_cost ." /image"
             case 'Price':
                 $total[]= $multiplier * $cogs;
  5. Function that displays the price result from the previous function. Does not currently account for the image or image+commission statements. I need to create a similar function that finds the lowest calculated price of a particular product group to display on the archive page for products.

    function was_now($client_lvl,$m_type)
            If ($client_lvl == "high")
            echo "is \${$p1}";
    else {
            echo "WAS: \${$p1}<br/>";
        echo "NOW: \${$p2}";
  6. This is the subloop of the product page that displays the variants for that product. Not fully coded yet. (This code is currently displayed in the template file itself opposed to a function.

        function uc_list_product($ID,$m_type)
        <div claas="clear_left">
        echo $client_lvl."<br/>";
        $children= new WP_Query($args);
        if($children->post_count > 0) { ?>
        <table width="100%" border="0" cellspacing="5" cellpadding="5">
        while ($children->have_posts()) {
        /*  if($m_type=='Image'){
            $display= 'Price  '. was_now($client_lvl,$m_type);
            //if images are selected display price*img count
        $display= 'Price  '. was_now($client_lvl,$m_type);}
                //display multiplier, img cost, img count
        {$display= 'Price  '. was_now($client_lvl,$m_type);}*/
                 $display= 'Price  '. was_now($client_lvl,$m_type);
        echo "<tr>";
        echo "<td>". the_title()."</td><td>".$display ."</td><td><a href='#'>Select Product</a></td></tr>" ; 
        echo "  </tbody></table>";
        echo '</div>';

The way I think this will work is that function get_client_lvl () will be called once when a user enters the site. (Changes if user logged in)

The Product page calls uc_list_product($ID,$m_type), which calls was_now($client_lvl,$m_type), which calls calculate_price($m_type), then get_multiplier($client_lvl).

If there is a simpler way of doing this that would require less database calls, please let me know.


1 Answer 1


If you think that you are making too many DB requests then you need to reorganize the way you store your data to be more efficient.

It might even be totally irelevant if you employ the right type of caching. If your visitors are usually not logged in then a full page caching that W3TC, super cache, or other plugin do will nullify the concern. Even if not, result caching with object cache will probably mitigate the problem.

  • Ok, that is fine. I suppose my main concern should be getting my functions to work. As right now they are not working correctly.
    – Jon C
    Sep 19, 2014 at 4:53

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.