如何通过使用PHP查找最大尺寸来增加运输成本?
现在我加上运费65Kr 这家商店只有四种价格 我需要把运费改成以下方式 成本分别为198Kr和268Kr的产品需要25Kr,超过该价格(418和498Kr)的产品需要65Kr 如果客户购买198Kr和418kr,则需要支付65Kr。这意味着如果有一个项目需要65Kr,那么运输将是65Kr 如果客户购买198Kr和268Kr,则需要支付25Kr 我不知道该如何将此运费添加到总成本中 我使用以下代码来更新总价 我在收银台加了65Kr如何通过使用PHP查找最大尺寸来增加运输成本?,php,shipping,Php,Shipping,现在我加上运费65Kr 这家商店只有四种价格 我需要把运费改成以下方式 成本分别为198Kr和268Kr的产品需要25Kr,超过该价格(418和498Kr)的产品需要65Kr 如果客户购买198Kr和418kr,则需要支付65Kr。这意味着如果有一个项目需要65Kr,那么运输将是65Kr 如果客户购买198Kr和268Kr,则需要支付25Kr 我不知道该如何将此运费添加到总成本中 我使用以下代码来更新总价 我在收银台加了65Kr function updateCart($productid,$f
function updateCart($productid,$fullproduct){
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
$productid = id_clean($productid);
$totalprice = 0;
if (count($fullproduct)){
if (isset($cart[$productid])){
$prevct = $cart[$productid]['count'];
$prevname = $cart[$productid]['name'];
$prevprice = $cart[$productid]['price'];
$cart[$productid] = array(
'name' => $prevname,
'price' => $prevprice,
'count' => $prevct + 1
);
}else{
$cart[$productid] = array(
'name' => $fullproduct['name'],
'price' => $fullproduct['price'],
'count' => 1
);
}
foreach ($cart as $id => $product){
$totalprice += $product['price'] * $product['count'];
}
$_SESSION['totalprice'] = $totalprice;
$_SESSION['cart'] = $cart;
$msg = $this->lang->line('orders_added_cart');
$this->session->set_flashdata('conf_msg', $msg);
}
}
结账时
...
$shipping= 65;
$grandtotal = (int)$totalprice + $shipping;
...
正如您所看到的,我可以使用session跟踪大小或价格。所以我想我可以用它们来找到最终的运输价格
我将感谢任何帮助
提前感谢。在您的购物车迭代中计算总价
foreach ($cart as $id => $product){
$totalprice += $product['price'] * $product['count'];
}
还要计算运费并将其放入会话中
$shippingprice = 25.0;
foreach ($cart as $id => $product){
$totalprice += $product['price'] * $product['count'];
if ( $product['price'] > 268 ){
$shippingprice = 65.0;
}
}
我会使用类似于:
function updateCart($productid,$fullproduct)
{
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
$productid = id_clean($productid);
$totalprice = 0;
if (count($fullproduct))
{
if (isset($cart[$productid]))
{
$prevct = $cart[$productid]['count'];
$prevname = $cart[$productid]['name'];
$prevprice = $cart[$productid]['price'];
$cart[$productid] = array(
'name' => $prevname,
'price' => $prevprice,
'count' => $prevct + 1
);
}
else
{
$cart[$productid] = array(
'name' => $fullproduct['name'],
'price' => $fullproduct['price'],
'shipping' => $fullproduct['shipping']
'count' => 1
);
}
$shipping = 0;
foreach ($cart as $id => $product)
{
$shipping = $shipping < $product['shipping'] ? $product['shipping'] : $shipping;
$totalprice += $product['price'] * $product['count'];
}
$_SESSION['totalprice'] = $totalprice;
$_SESSION['cart'] = $cart;
$_SESSION['shipping'] = $shipping;
$msg = $this->lang->line('orders_added_cart');
$this->session->set_flashdata('conf_msg', $msg);
}
}
函数updateCart($productid,$fullproduct)
{
$cart=isset($_会话['cart'])?$_会话['cart']:数组();
$productid=id\u clean($productid);
$totalprice=0;
if(计数($fullproduct))
{
if(isset($cart[$productid]))
{
$prevct=$cart[$productid]['count'];
$prevname=$cart[$productid]['name'];
$prevprice=$cart[$productid]['price'];
$cart[$productid]=数组(
'name'=>$prevname,
“价格”=>$prevprice,
“计数”=>$prevct+1
);
}
其他的
{
$cart[$productid]=数组(
'name'=>$fullproduct['name'],
“价格”=>$fullproduct[“价格”],
“配送”=>$fullproduct[“配送”]
“计数”=>1
);
}
$shipping=0;
foreach($id=>$product的购物车)
{
$shipping=$shipping<$product['shipping']?$product['shipping']:$shipping;
$totalprice+=$product['price']*$product['count'];
}
$\会话['totalprice]=$totalprice;
$\会话['cart']=$cart;
$\会话['shipping]=$shipping;
$msg=$this->lang->line('orders\u added\u cart');
$this->session->set_flashdata('conf_msg',$msg);
}
}
这样,你可以有任何航运价格,你只收取较高的一个
然而,这种方式只考虑了一个项目的运输价格。我也会考虑数量或项目。因为运输一种产品和一百种产品时,运输成本会发生变化。好的,我只需要;。我怎样才能在会议中保持这一点,以便我能在结帐时展示。@shin你肯定听说过“谢谢”这个成语,它的含义和用途?就像你在会议中计算总价一样