2010-01-10 25 views
0

目前我加收運費65Kr。如何通過PHP找到最大尺寸來增加運輸成本?

店內只有四個價格。

我需要將運費更改爲以下方式。

產品198Kr和268Kr需要25Kr和超過該價格(418和498Kr)需要65Kr。

如果客戶購買198Kr和418kr,那麼她需要支付65Kr。這意味着如果有一件物品需要65Kr,那麼裝運將是65Kr。

如果客戶購買198Kr和268Kr,那麼她需要支付25Kr。

我不確定如何將此運費添加到總費用中。

我使用下面的代碼來更新總價格。

我在結賬時加65Kr。

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; 
... 

正如你看到的,我可以使用的會話跟蹤尺寸還是價格。所以我想我可以用它們來找到最終的運費。

我會感謝任何幫助的手。

在此先感謝。

回答

1

您的購物車迭代,計算總價

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; 
    } 
} 
+0

我收到語法錯誤。意外的事情}事後65.0 – shin 2010-01-10 09:39:20

+0

好吧,我只是需要。我怎樣才能保持這個會話,以便我可以在結帳時顯示。 – shin 2010-01-10 09:42:34

+1

@shin你肯定聽說過這個成語,「謝謝」,它的意思,它的用途? – moritz 2010-01-10 09:44:17

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); 
    } 
} 

這樣你可以有任何海運價格,你會只收取較高的一個。

然而,這種方式只考慮了一件物品的運費。我也會考慮數量或項目。因爲裝運一件產品時以及裝運一百件時,運輸費用會發生變化。