开发者

How to add shipping cost by finding the largest size with PHP?

At the moment I add开发者_如何学C shipping charge 65Kr.

There are only four prices in the shop.

I need to change the shipping charge to the following way.

Product which cost 198Kr and 268Kr needs 25Kr and more than that price (418 and 498Kr) needs 65Kr.

If a customer buys 198Kr and 418kr, then she needs to pay 65Kr. This means if there is one item which needs 65Kr , then shipping will be 65Kr.

If a customer buys 198Kr and 268Kr, then she needs to pay 25Kr.

I am not sure how to add this shipping to the total cost.

I use the following code to update the total price.

And I add 65Kr at the checkout.

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

at the checkout

...
$shipping= 65;
$grandtotal = (int)$totalprice + $shipping;
...

As you can see I can use session to keep track of size or price. So I think I can use them to find the final shipping price.

I will appreciate any helping hands.

Thanks in advance.


in your cart iteration that computes the total price

foreach ($cart as $id => $product){
   $totalprice += $product['price'] * $product['count'];
} 

also compute the shipping price and place it in the session too.

$shippingprice = 25.0;
foreach ($cart as $id => $product){
   $totalprice += $product['price'] * $product['count'];
   if ( $product['price'] > 268 ){
       $shippingprice = 65.0;
   }
} 


I'd use something like:

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

This way you can have any shipping price and you'll charge only the higher one.

However this way it only takes into account the shipping price for one item. I'd take into account the number or items too. Because the cost of shipping change when shipping one product and when shipping one hundred.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜