I have been trying to figure out a somewhat complex set of functions to handle some product price calculations and display. I was thinking that building classes may be the best solution, but I don't completely know what I am doing with that either. I was hoping someone may be able to help me figure out the best solution for all this. This site is being built within wordpress. The pages load as follows, 1. Site determines client level: If no user is logged in, it defaults to client_lvl = high, if user is logged in, client_lvl may be set to an alternate value. (high, med, low) This should be loaded just once per site visit. I created this function:
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
I tried converting it to a class, but I'm not sure I am doing it correctly.
class ClientLvl
{
public $client_lvl ='high';
private $user_id;
private function __construct()
{
$this->user_id= get_current_user_id();
}
public function get_client_lvl () //check the client level from user meta-
{
$this->client_lvl
If ($user_id != 0)
{return get_user_meta($user_id, wpcf-client-lvl, true); }
The rest of the functions are based on the following arrangement. Collections have multiple products which have multiple variations. The Products have a series of multiplier values attached to them. One for each client level. It also has a multiplier type (m_type) The variants then have the item specific details such as cost of goods sold (cogs), Area, sku, and name. 2. This function is used to determine which multiplier value will be used in the calculation.
Used on collection and product pages to determine which multiplier value to use for calculation based on client level
f
unction 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
I am assuminng that if I made this a class, I should just define the p_high (med, low) values and then display them with another object creation in another 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');
$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;
}}}
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)
{
$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}";
}}
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)
{
get_page_children($ID);
echo "<table>
";
while ( have_posts() )
{ the_post();
?>
<tr><?php
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);}
echo "<td>". the_title()."</td><td>Price ".$display ."</td><td><a href='#'>Select Product</a></td></tr>" ;
}
echo "</table>";
wp_reset_query();
}
This will probably be doable with classes, but I I'm not entirely sure how to go about that. I hope someone can help me figure this out.