jcc5018 Posted October 5, 2014 Share Posted October 5, 2014 (edited) 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. Edited October 5, 2014 by jcc5018 Quote Link to comment https://forums.phpfreaks.com/topic/291445-php-classes-or-straight-code/ Share on other sites More sharing options...
Frank_b Posted October 5, 2014 Share Posted October 5, 2014 (edited) your post is too long to read it completely. starting with classes is not so easy. I guess you did read some tutorials. First of all: give a class only one responsibility! as soon you start to say "this class do this AND do that" you are on the wrong way. Second: a Class use other classes! (Normaly we don't speak about classes but about objects) class User { private $username; // more code } class Authenticate { public function Login(User $user) { echo 'Hello ' . $user->getName(); } } Third: Functions inside classes that we call methods can share data inside that class class User { private $username; public function getUsername() { return $this->username; } public function setUsername($username) { $this->username = $username; } } Edited October 5, 2014 by Frank_b Quote Link to comment https://forums.phpfreaks.com/topic/291445-php-classes-or-straight-code/#findComment-1492760 Share on other sites More sharing options...
Frank_b Posted October 5, 2014 Share Posted October 5, 2014 (edited) another hint: i see everywhere 'mid' 'high' etc. This is too much 'green' or constants. Change that like this: // configuration: define('USERLEVEL_LOW', ; define('USERLEVEL_MEDIUM', 32); define('USERLEVEL_HIGH', 128); // In the rest of you code: class ClientLvl { private $client_lvl = USERLEVEL_HIGH; } Edited October 5, 2014 by Frank_b Quote Link to comment https://forums.phpfreaks.com/topic/291445-php-classes-or-straight-code/#findComment-1492762 Share on other sites More sharing options...
Frank_b Posted October 5, 2014 Share Posted October 5, 2014 Keep the variables inside your classes private like my example with the class User. Quote Link to comment https://forums.phpfreaks.com/topic/291445-php-classes-or-straight-code/#findComment-1492763 Share on other sites More sharing options...
jcc5018 Posted October 6, 2014 Author Share Posted October 6, 2014 Thanks for the feed back. I am sorry it is long, but I'm not sure how to explain things without showing the whole set of code.keep in mind, the functions above, unless otherwise stated were designed first without classes. I havent converted everything yet, because I was confusing myself.I'm not sure I understand your example for the client levels. I have no idea what those numbers are that you included. My guess is you may be misunderstanding the purpose of the level. So Let me explain.The purpose of the client level is to allow for multiple price list for my customers. Say I want to give one customer a discounted rate, then their level may be medium or low. No numbers are associated with the client level. The default rate is high. This could also be used to set site wide discounts, as my levels are not simply calculated based on percentages off.Each product has three different multipliers. For example, A photo print will have a multiplier for the high, med, and low as 0.5, 0.4, 0.3 . Then it will also have one of 6 m_types, in this case "area" as all prints are calculated based on the area of the print.other types would calculate based on cost of good sold, flat rate, commission, or the number of images selected for the product (photo albums)So the page will load the proper multiplier value based on what the client level is.Next, the page loops through all the product variants that are children of the product and sets them in a list. Each variant has the missing information needed to calculate the final price. (area, cogs, ect.)So my 8x10 photo variant would use the calculation for area which is area *multiplier and display the proper price.The results of the high, med, low price would be $40, $32, and $24 respectively. If the client level is anything but high, then a was/ now price is displayed.There are two other values assigned to each product. That is img_min and img_maxThese are used to define the min and max number of images allowed to be selected for the particular product.Hopefully that clears some things up. Quote Link to comment https://forums.phpfreaks.com/topic/291445-php-classes-or-straight-code/#findComment-1492832 Share on other sites More sharing options...
Frank_b Posted October 6, 2014 Share Posted October 6, 2014 I understand now that you talk about clients or customers and i talked about users. Not so much difference... The numbers are just random codes. I like to keep some space between the numbers for in case that you want to insert another level later. The problem that i see in your code is that the words low, medium and hig appear everywhere in your code. Would you like to change something later then you will have search all your codefiles and replace them. Quote Link to comment https://forums.phpfreaks.com/topic/291445-php-classes-or-straight-code/#findComment-1492888 Share on other sites More sharing options...
Frank_b Posted October 6, 2014 Share Posted October 6, 2014 (edited) While reading your last message i wrote the following possible classnames: Product ProductCategory Customer Pricelist To be clear: the classes can hold only one of the object it stands for: class Product can hold only one product class Customer can hold only one customer etc. To hold more entities eg customers you should use array's class Customer { private $companyName; private $email; // and more properties like password, clientlevel, address, telephone etc public function setCompanyName($companyName) { $this->companyName = $companyName; } public function getCompanyName() { return $this->companyName; } public function setEmail($email) { $this->email = strtolower($email); } public function getEmail() { return $this->email; } } $customers is array() $customer = new Customer(); $customer->setCompanyName('Google'); $customer->setEmail('info@google.com'); $customers[] = $customer; $customer = new Customer(); $customer->setCompanyName('Microsoft'); $customer->setEmail('info@microsoft.com'); $customers[] = $customer; foreach($customers as $customer) echo $customer->getName() . '<br>'; Edited October 6, 2014 by Frank_b Quote Link to comment https://forums.phpfreaks.com/topic/291445-php-classes-or-straight-code/#findComment-1492890 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.