framefreak framefreak - 1 month ago 8
PHP Question

how to specify shipping_cost regarding all products in session?

I´m a noob and just started with PHP.
So I got a problem with the following script, which is listing all products from a session. Ive products with different $shipping_cost (0.7 or 6.99).
If I have in my card :


products with product_code<100 = shipping_cost should be 0.7,

products with product_code>100 = shipping_cost should be 6.99,

products with product_code<100 AND product_code>100 = shipping_cost keep 6.99.


<?php

if(isset($_SESSION["products"]) && count($_SESSION["products"])>0){
$total = 0;
$list_tax = '';
$gs = 0.70;

$cart_box = '<ul class="view-cart">';

foreach($_SESSION["products"] as $product){ //Print each item, quantity and price.
$product_name = $product["product_name"];
$product_qty = $product["product_qty"];
$product_price = $product["product_price"];
$product_code = $product["product_code"];

$item_price = ($product_price * $product_qty); // price x qty = total item price

$cart_box .= "<li class=\"view-cart-total\">$product_code &ndash; $product_name &ndash; Anzahl : $product_qty = <strong>" .number_format($item_price, 2, ",", "."). "&nbsp;".$currency."</strong></li>";

$subtotal = ($product_price * $product_qty); //Multiply item quantity * price
$total = ($total + $subtotal); //Add up to total price
}


if($product_code < 100){
$shipping_cost = $gs;} //Gutschein-Versandkosten
elseif($product_code > 100){
$shipping_cost = $shipping_cost;} //Gutschein-Versandkosten
else {$shipping_cost = $shipping_cost;}

$grand_zw = number_format($total, 2, ",", "."); //Zwischensumme
$grand_total = $total + $shipping_cost; //Gesamtbetrag

foreach($taxes as $key => $value){ //list and calculate all taxes in array
$tax_amount = ($grand_total - ($grand_total / 119 * 100)); //MwSt
$tax_item[$key] = $tax_amount;
$grand_total = $grand_total + 0;
}

foreach($tax_item as $key => $value){ //taxes List
$list_tax .= $key. ' ' .number_format($value, 2, ",", "."). '&nbsp;'.$currency. '<br />';
}


$grand_netto = ($grand_total - $tax_amount);
$shipping_cost = ($shipping_cost)?'Versandkosten = '. number_format($shipping_cost, 2, ",", ".").' '.$currency.'<br />':'';

//Print Shipping, VAT and Total
...
?>


Problems is, if I have both products (product_code<100 and product_code>100) in the card, it regards only the last product Ive given to the card.
So shipping_price is either 0.7 or 6.99. I hope you understand my problem -.-
What do I wrong and how to fix it ?

Answer

You have to put the shipping cost logic inside the foreach loop, this would be a possibility:

$shipping_cost = 0.7;    

foreach($_SESSION["products"] as $product) {
  ... here is your existing code of the foreach loop

  if($product_code > 100) {
    $shipping_cost = 6.99;
  }
}

if(count($_SESSION["products"])>1) {
  $shipping_cost = 6.99;
}

That way the default shipping costs are 0.7 and are only changed to 6.99 if there is a product with $product_code > 100 or if there is more than one product.