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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.