I got a problem with the following script, which is listing all products from a session. There are 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 – $product_name – Anzahl : $product_qty = <strong>" .number_format($item_price, 2, ",", "."). " ".$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, ",", "."). ' '.$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
...
?>
Problem is, if I have both products (product_code<100 and product_code>100) in the card, it regards only the last product I've given to the card.
So $shipping_price
is either 0.7 or 6.99.
答案 0 :(得分:0)
您必须将运费成本逻辑放在foreach循环中,这可能是:
$shipping_cost = 0.7;
$total_products_number = 0;
foreach($_SESSION["products"] as $product) {
... here is your existing code of the foreach loop
if($product_code > 100) {
$shipping_cost = 6.99;
}
$total_products_number += $product["product_qty"];
}
if($total_products_number>1) {
$shipping_cost = 6.99;
}
这样,默认运费为0.7,如果有产品带有$ product_code&gt;则只会更改为6.99。 100或者数量上有多个产品。