Jump to content


if else statement maybe?

  • Please log in to reply
2 replies to this topic

#1 gobbles

  • Members
  • PipPip
  • Member
  • 15 posts

Posted 22 May 2006 - 12:31 PM

Hey All,

I was wondering if someone could help me with a couple of statements im trying to write.

I have a shopping cart, and im trying to implement a individual shipping cost per product and a secondary shipping price for additional products and quantitys.

I just cant seem to get my head around the statement ... basically this is how it should go:

if theres more than one product in the cart then
get primary shipping cost of 1st product
how much qty? any additional product?
for each additional products and qty's use secondary shipping price ( which is defined for each product)
else if there is only one product and one qty then
use primary shipping cost.

Hows that sound ... I cant get my head around it at all.

to give a real world example:

Listed below are 2 products with their details

Product: Canon Digi Camera
Primary Shipping Cost: $20
Additional Shipping Cost: $1

Product: DVD Player
Primary Shipping Cost: $40
Additional Shipping Cost: $2

If i buy 2 digi cameras, the total shipping price will be $21
$20 + $1

If i buy 1 digi camera and 1 DVD player, the total shipping price will be $22
$20 + $2

If i buy 1 digi camera and 4 DVD players, the shipping will be $28
$20 + $2 + $2 + $2 + $2

I hope thats a better explaination of it.

If any could help me get my head around this it would be greatly appreciated, i have been pulling my hair out about this for months now.


#2 eves

  • Members
  • PipPip
  • Member
  • 26 posts

Posted 22 May 2006 - 01:41 PM

you might want to try these:

assuming you have this fields on your cart table
tbl_cart(cart_id, prod_id, qty, unit_price, pri_shipping, sec_shipping, initial, client_id)
- would greatly ease computation if you store primary and secondary shipping on the table
- pri_shipping - primary shipping cost of product
- sec_shipping - additional shipping cost of product

$result = mysql_query("select * from tbl_cart where client_id='$CLIENT_ID'");

$shipping = 0;
    if ($row['qty']>1)
        if ($row['initial']==1) //get primary shipping cost first then add secondary costs
            $shipping += $row['pri_shipping'];
            $shipping += ($row['qty']-1)*$row['sec_shipping'];
            $shipping += ($row['qty'])*$row['sec_shipping'];
                // since only 1 qty, should be the main product, thus primary shipping cost apply
        $shipping += $row['pri_shipping'];
the initial field is etheir 1 or 0, 1 if its the main product being pruchased, else 0

you might want to check for some syntax or typos, just basically made it up on this site's editor [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

hope that helps

#3 gobbles

  • Members
  • PipPip
  • Member
  • 15 posts

Posted 22 May 2006 - 02:16 PM

Thanks for your quick reply.

I tried to implement the code that you gave, but came through with some errors, the following code works perfect if theres only 1 product in the cart, if i update the qty, everything.

But it doesnt work with multiple products in the cart, when there are multiple products, it adds the primary ($products_ship_price) instead of adding the additional ($products_ship_price_two)

$this->vendor_shipping[$vendors_id]['ship_cost'] += ($products_ship_price);
if ($this->vendor_shipping[$vendors_id]['ship_cost'] >= 2.5000)
  $this->vendor_shipping[$vendors_id]['ship_cost'] = ($this->vendor_shipping[$vendors_id]['ship_cost']);
    if ($quantity > 1) {
      $this->vendor_shipping[$vendors_id]['ship_cost'] += ($products_ship_price_two * ($quantity-1));

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users