timdan Posted August 2, 2011 Share Posted August 2, 2011 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. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 2, 2011 Share Posted August 2, 2011 $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. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 2, 2011 Share Posted August 2, 2011 @darkfreaks the ternary operator takes 3 operands: a condition, a result for true and a result for false: $something = $i > 1 ? 'True' : 'False'; you have 4 operators in yours. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 2, 2011 Share Posted August 2, 2011 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. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 2, 2011 Share Posted August 2, 2011 nope, that's not going to work either. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.