【发布时间】:2014-08-19 05:07:22
【问题描述】:
好吧,我可能让这对自己来说有点太复杂了,但我真的很想弄清楚这一点,而且我一直在思考如何让它发挥作用。这是一个wordpress网站
所以我要完成的过程是这样的:
确定客户级别以确定客户将拥有的价格点。 选项:高、低、中
根据客户级别,选择合适的产品乘数用于我的计算 选项:p_high、p_medium 或 P_low
运行一个方程式,根据所选乘数类型计算最终价格。
如果客户级别不高,价格将显示过去/现在的价格点,因此必须同时使用 p_high 和 p_low 值。
我尝试创建一个函数,该函数将数组设置为在 foreach 循环中运行以计算 price1 和 price2。但是虽然我还没有测试过,但我很确定这不会实现我正在寻求的目标。
我假设我可以增加变量名来创建 price1 和 price 2 变量,但我不完全确定最好的方法,或者它是否是正确的解决方案。如果有人可以帮助我,那就太好了。
注意:switch 语句确实包含先前定义的关联数组中存在的变量。一旦我弄清楚如何正确传递变量,存在的 echo 语句将被函数替换。
function get_client_lvl ()
{$user_id = get_current_user_id();
If ($user_id == 0)
{return "high";}
Else
{get_user_meta($user_id, wpcf-client-lvl, true); }
}
$client_lvl=get_client_lvl();
function display_right_price($client_lvl, $multiplier)
{If ($client_lvl == "high")
{echo "\${$price1}";}
else {
echo "WAS:\${$price1}<br/>";
echo "Now: \${$price2}";
}}
$p_high= 2; //get_post_meta($post_id,$key
$p_med= 1.5;
$p_low =1.1;
$cogs= 10;
$img_count =3;
$img_cost= 10;
$area= 40;
$m_type= 'Area';
if ($client_lvl == 'med')
{$prices=array($p_high, $p_med);}
elseif ($client_lvl == 'low')
{$prices=array($p_high, $p_low);}
else
{$prices=array($p_high);}
Foreach($prices as $multiplier)
{
switch ($m_type) {
case 'Area':
$price= $multiplier * $area;
echo "The {$lvl} price is \${$price}<br/>";/>"; //This will be replaced with display_right_price() function once the price variables are established
break;
case 'Image':
$price= $multiplier * $img_count *$cogs;
echo "The {$lvl} price is \${$price}<br/>";/>"; //This will be replaced with display_right_price() function once the price variables are established
break;
case 'Commission':
$price= $multiplier + $cogs;
echo "The {$lvl} price is \${$price}<br/>";/>"; //This will be replaced with display_right_price() function once the price variables are established
break;
case 'Flat':
$price= $multiplier;
echo "The {$lvl} price is \${$price}<br/>"; />"; //This will be replaced with display_right_price() function once the price variables are established
break;
case 'Commission+Image':
$price= $multiplier + ($img_cost*$img_count);
echo "The {$lvl} price is \${$price}<br/>";/>"; //This will be replaced with display_right_price() function once the price variables are established
break;
case 'Price':
$price= $multiplier * $cogs;
echo "The {$lvl} price is \${$price}<br/>";/>"; //This will be replaced with display_right_price() function once the price variables are established
break;
}}
编辑:我想通了(现在只需要弄清楚如何从两种不同的帖子类型中动态提取变量:
function get_client_lvl ()
{$user_id = get_current_user_id();
If ($user_id == 0)
{return 'high';}
Else
{get_user_meta($user_id, wpcf-client-lvl, true); }
}
//$client_lvl=get_client_lvl();
$postid = get_the_ID();
$client_lvl='high';
function display_right_price($client_lvl)
{
$p_high= 3.2; //get_post_meta($post_id,$key)
$p_med= 1.3;
$p_low =1.1;
$cogs= 11.53;
$img_count =3;
$img_cost= 10;
$area= 40;
$m_type= 'Price';
$price=array();
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);}
Foreach($prices as $multiplier)
{
switch ($m_type) {
case 'Area':
$total[]= $multiplier * $area;
break;
case 'Image':
$total[]= $multiplier * $img_count *$cogs;
break;
case 'Commission':
$total[]= $multiplier + $cogs;
break;
case 'Flat':
$total[]= $multiplier;
break;
case 'Commission+Image':
$total[]= $multiplier + ($img_cost*$img_count);
break;
case 'Price':
$total[]= $multiplier * $cogs;
break;
}}
$p1=number_format($total[0],2,'.',',');
If ($client_lvl == "high")
{
echo "\${$p1}";
}
else {
$p2=number_format($total[1],2,'.',',');
echo "WAS: \${$p1}<br/>";
echo "NOW: \${$p2}";
}}
display_right_price($client_lvl);
【问题讨论】:
标签: php arrays wordpress function switch-statement