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.
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';} Else {$client_lvl=get_user_meta($user_id, wpcf-client-lvl, true); } }//End function
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);} else {$prices=array('high'=>$p_high);} }//End function
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');
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'); $img_cost=10; $prices= get_multiplier(); Foreach($prices as $multiplier) { switch ($m_type) { case 'Area': $area= get_post_meta(get_the_ID(),'area','single'); $total[]= $multiplier * $area; break; case 'Image': if(isset($img_count)) {$total[]= $multiplier * $cogs * $img_count;} else {$total[]= $multiplier * $cogs ;} // Displayed as $price/ Image using $img_count break; case 'Commission': $total[]= $multiplier + $cogs; break; case 'Flat': $total[]= $multiplier; break; case 'Commission+Image': if(isset($img_count)) {$total[]= $multiplier + ($img_cost*$img_count);} else {$total[]= $multiplier; } //Display "Price: ".$multiplier. "+".$img_cost ." /image" break; case 'Price': $total[]= $multiplier * $cogs; break; }}}
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) { $total=calculate_price($m_type); $p1=number_format($total[0],2,'.',','); If ($client_lvl == "high") { echo "is \${$p1}"; } else { $p2=number_format($total[1],2,'.',','); echo "WAS: \${$p1}<br/>"; echo "NOW: \${$p2}"; }}
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"> <?php echo $client_lvl."<br/>"; $children= new WP_Query($args); if($children->post_count > 0) { ?> <table width="100%" border="0" cellspacing="5" cellpadding="5"> <tbody> <?php while ($children->have_posts()) { $children->the_post(); /* if($m_type=='Image'){ $display= 'Price '. was_now($client_lvl,$m_type); //if images are selected display price*img count } elseif($m_type=='Commission+Image'){ $display= 'Price '. was_now($client_lvl,$m_type);} //display multiplier, img cost, img count else {$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>"; wp_reset_query(); } 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.