Jump to content

Shopping cart shipping costs issue


timdan

Recommended Posts

Hello,

 

I have a website with a shopping cart, however trying to add shipping costs dependent on the total cost of all items in the check out.

 

For example for items less than £10 then shipping will be £1.95 for items less between £10.01 and £20 then shipping will be £2.50 and for orders above £15 then shipping will be £3.75.

 

I currently have the following code:

$cart_sub_total = 0;
$cart_total = 0;
$cart_shipping = 1.95;
$_SESSION['_simplecart_items'] = 0;
foreach($cart_products as $product_id => $product){
   $quantity = max(1,(int)$product['quantity']);
   $_SESSION['_simplecart_items']+= $quantity;
   $product_total = $product['price'] * $quantity;
   $cart_sub_total += $product_total;
   if(isset($product['shipping']) && $product['shipping']>0){
      $cart_shipping += ($product['shipping'] * $product['quantity']);
   }
}

 

 

as you can see the shipping cost is 1.95 all the time. I was wondering how I would add some if statements to this code to have the three different shipping costs depending on the $cart_total.

 

Any help would be appreciated!

 

Thanks,

 

Tim

 

EDITed for CODE tags. 

Link to comment
https://forums.phpfreaks.com/topic/243623-shopping-cart-shipping-costs-issue/
Share on other sites

 $cart_total= ( > 1.25) ? 'This is greater than $1.25 echo code here' :
(> 10) ? 'This is greater than $10 insert code here' :
(> 50) ? 'This is Greater than $50 insert code here';

 

i think that is correct ternary usage may be wrong someone can correct me but it would suffice.

yeah i knew it was missing something mainly the false condition

 

$cart_total=( ( > 1.25) ? 'This is greater than $1.25 echo code here' : 'else code equals $1.25')
( (> 10) ? 'This is greater than $10 insert code here' : 'else could equals less than $10')
((> 50) ? 'This is Greater than $50 insert code here' : 'else code equals less than $50');

 

Actually i have 3 operators the condition true and now false the brackets i assume replace the ifstatements and the brackets around the condition also.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.