#### Archived

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

# if else statement maybe?

## Recommended Posts

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

Product: DVD Player
Primary Shipping Cost: \$40

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.

Cheers

##### Share on other sites
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

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

\$shipping = 0;
while(\$row=mysql_fetch_assoc(\$result))
{
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'];
}
else
{
\$shipping += (\$row['qty'])*\$row['sec_shipping'];
}

}
else
{
// since only 1 qty, should be the main product, thus primary shipping cost apply
\$shipping += \$row['pri_shipping'];

}

}
[/code]
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

##### Share on other sites

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)

[code]
\$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));
}
[/code]