PHP - 无法更新会话中的购物车总金额

时间:2015-01-08 08:41:11

标签: php jquery ajax

正如标题所说,我无法更新购物车总金额。这是我的情况。

我有一个结帐页面,其中有一个发货金额选项,用户必须从下拉菜单中选择一个选项。一旦选择,购物车总金额应该更新,在我的导航链接所在的标题中添加运费,也在我希望用户点击按钮进行结帐的页面中。

这是运费HTML代码:

<select name="selectZone" id="custDelAddZone">
    <option value="-1">----- Select -----</option>
    <?php
    $queryForZone = "SELECT * FROM shipping_zones ORDER BY ZoneName";
    $validate->Query($queryForZone);
    if ($validate->NumRows() >= 1) {
        while ($row = $validate->FetchAllDatas()) {
            echo '<option value="'.$row['ZoneId'].'">'.$row['ZoneName'].'</option>';
        }
    }
    ?>
</select>                   
<button name="CustDelAddUpdateZone" id="btnCustDelAddUpdateZone" type="submit">Continue</button>

这里是按钮点击的AJAX部分:

$("#btnCustDelAddUpdateZone").click(function() {
    zoneId = $("#custDelAddZone").val();
    if (zoneId == '-1') {
        toastr.error("Please Select The Region", "Error");
        toastr.options.showMethod = "slideDown";
        toastr.options.hideMethod = "slideUp";
    }
    var dataStr = $("#chooseShippingZoneForm").serialize();
    $.ajax({
        url: 'http://localhost/ECommerceOnFlatastic/ActionFiles/Customers/UpdateDeliveryZone.php',
        type: 'POST',
        data: dataStr,
        success: function(msg) {
            toastr.success(msg);
            toastr.options.showMethod = "slideDown";
            toastr.options.hideMethod = "slideUp";

            $("#selectZoneForm").slideUp();
            $("#delMethod h3").removeClass("color_light active");
            $("#paytMethod").show();
            $("#paytMethod h3").removeClass("bg_light_color_1 color_dark");
            $("#paytMethod h3").addClass("color_light active");
            $("#payment").slideDown();
        }
    });
    return false;
});

这是PHP脚本(UpdateDeliveryZone.php):

<?php
session_start();

$zoneId = $custCode = "";

require_once '../../Classes/class.Validation.php';
$validate = new Validation('developi_ecommerce');

$q = "SELECT CustCode FROM customers WHERE CustEmailAdd = '".$_SESSION['Customer']['email']."'";
$validate->Query($q);
if ($validate->NumRows() >= 1) {
    while ($row = $validate->FetchAllDatas()) {
        $custCode = $row['CustCode'];
    }
} else {
    echo "No Customer Found";
}

if ( isset( $_POST['selectZone'] ) && $_POST['selectZone'] != "" ) {
    $zoneId = $validate->EscapeString( $_POST['selectZone'] );
    $query = "UPDATE customers_delivery_address SET ZoneId = '".$zoneId."' WHERE CustCode = '".$custCode."' AND CustDelAddLastInserted >= NOW() - INTERVAL 10 MINUTE";
    if ( $validate->Query( $query ) == TRUE ) {
        echo "Updated Successfully";
    } else {
        echo "Invalid Query";
    }
} else {
    echo "Value Not Set";
}
?>

以下是我试图用PHP获取购物车总金额的方法:

if ($validate->Query($sql) == TRUE) {
    if ($validate->NumRows() >= 1) {
        while ( $row = $validate->FetchAllDatas() ) {
            echo '<tr>';
            echo '<td style="width: 50%;" data-title="Product Image &amp; name" class="t_md_align_c"><img src="images/Products/'.$row['ProdCode'].'.jpg" alt="'.$row['ProdCode'].'" class="m_md_bottom_5 d_xs_block d_xs_centered" height="75" width="75"><a href="product.php?code='.$row['ProdCode'].'" class="d_inline_b m_left_5 color_dark">'.$row['ProdName'].'</a></td>';
            echo '<td style="width: 5%;" data-title="SKU">'.$row['ProdCode'].'</td>';
            echo '<td style="width: 5%;" data-title="Price"><p class="f_size_large color_dark">Rs. '.$row['ProdRate'].'</p></td>';
            echo '<td  style="width: 5%;" data-title="Quantity"><div class="clearfix d_inline_middle f_size_medium color_dark m_bottom_10">'.$_SESSION['cart'][$row['ProdCode']]['quantity'].'</div></td>';

            $sbTotal = $row['ProdRate'] * $_SESSION['cart'][$row['ProdCode']]['quantity'];
            $subTotal = $sbTotal;
            echo '<td style="width: 5%;" data-title="Subtotal"><p class="f_size_large fw_medium scheme_color t_align_r">'.number_format($sbTotal, 2).'</p></td>';
            $total += $subTotal;
            $_SESSION['cartTotalAmount'] = $total;
            $tax = $row['CatTaxPercent'];
            $taxAmt = (($sbTotal * $tax ) / 100);
            $taxAmount += $taxAmt;
            $amt = 0;
            $cartWeightPerProduct = ($row['weight'] * $_SESSION['cart'][$row['ProdCode']]['quantity']);
            echo '</tr>';
            $totalCartWeight += $cartWeightPerProduct;
        }
        $totalTaxAmount += $taxAmount;

        $_SESSION['cartWeight'] = $totalCartWeight; 

        $sessAmnt = ($total + $totalTaxAmount);
        $totalPayableAmnt = $sessAmnt + $_SESSION['TotalWeight']; // This is my cart total amount

        $_SESSION['sessionTotalPayable'] = number_format($totalPayableAmnt, 2);
        if ( isset( $_SESSION['sessionTotalPayable'] ) ) {
            $amt = $totalPayableAmnt;
        } else {
            $amt = "Rs. 0";
        }

        echo '<tr><td colspan="4"><p class="fw_medium f_size_large t_align_r t_xs_align_c">Cart Total:</p></td><td colspan="1"><p class="fw_medium f_size_large color_dark t_align_r">'.number_format($total, 2).'</p></td></tr>';

        echo '<tr><td colspan="4"><p class="f_size_large t_align_r t_xs_align_c">Taxes:</p></td><td colspan="1"><p class="f_size_large color_dark t_align_r">'. number_format($totalTaxAmount, 2) .'</p></td></tr>';

        echo '<tr><td colspan="4"><p class="f_size_large t_align_r t_xs_align_c">Shipping:</p></td><td colspan="1"><p id="shippingAmount" class="f_size_large color_dark t_align_r">'.number_format($_SESSION['TotalWeight'], 2).'</p></td></tr>';

        echo '<tr><td colspan="4"><p class="fw_medium f_size_large t_align_r t_xs_align_c">Total Payable Amount:</p></td><td colspan="1"><p class="fw_medium f_size_large color_dark t_align_r">'.number_format($amt, 2).'</p></td></tr>';
    }
}

对于那些想知道$_SESSION['TotalWeight']来自哪里的人:

<?php
session_start();

require_once 'Classes/class.Validation.php';

function SelectZoneAmount($amt, $id) {
    $validate = new Validation('developi_ecommerce');
    $zoneCol = 0;
    if ($amt >= 1 && $amt <= 499 ) {
        $zoneCol = 'SWC_0_500';
        $query = "SELECT $zoneCol FROM shipping_weight_charges WHERE ZoneId = '".$id."'";
        $validate->Query($query);
        while ($row = $validate->FetchAllDatas()) {
            return $row[$zoneCol];
        }
    } elseif($amt >= 500 && $amt <= 999) {
        $zoneCol = 'SWC_500_1000';
        $query = "SELECT $zoneCol FROM shipping_weight_charges WHERE ZoneId = '".$id."'";
        $validate->Query($query);
        while ($row = $validate->FetchAllDatas()) {
            return $row[$zoneCol];
        }
    } elseif($amt >= 1000 && $amt <= 1499) {
        $zoneCol = 'SWC_1000_1500';
        $query = "SELECT $zoneCol FROM shipping_weight_charges WHERE ZoneId = '".$id."'";
        $validate->Query($query);
        while ($row = $validate->FetchAllDatas()) {
            return $row[$zoneCol];
        }
    } elseif($amt >= 1500 && $amt <= 1999) {
        $zoneCol = 'SWC_1500_2000';
        $query = "SELECT $zoneCol FROM shipping_weight_charges WHERE ZoneId = '".$id."'";
        $validate->Query($query);
        while ($row = $validate->FetchAllDatas()) {
            return $row[$zoneCol];
        }
    } elseif($amt >= 2000 && $amt <= 2499) {
        $zoneCol = 'SWC_2000_2500';
        $query = "SELECT $zoneCol FROM shipping_weight_charges WHERE ZoneId = '".$id."'";
        $validate->Query($query);
        while ($row = $validate->FetchAllDatas()) {
            return $row[$zoneCol];
        }
    } elseif($amt >= 2500 && $amt <= 2999) {
        $zoneCol = 'SWC_2500_3000';
        $query = "SELECT $zoneCol FROM shipping_weight_charges WHERE ZoneId = '".$id."'";
        $validate->Query($query);
        while ($row = $validate->FetchAllDatas()) {
            return $row[$zoneCol];
        }
    } elseif($amt >= 3000 && $amt <= 3499) {
        $zoneCol = 'SWC_3000_3500';
        $query = "SELECT $zoneCol FROM shipping_weight_charges WHERE ZoneId = '".$id."'";
        $validate->Query($query);
        while ($row = $validate->FetchAllDatas()) {
            return $row[$zoneCol];
        }
    } elseif($amt >= 3500 && $amt <= 3999) {
        $zoneCol = 'SWC_3500_4000';
        $query = "SELECT $zoneCol FROM shipping_weight_charges WHERE ZoneId = '".$id."'";
        $validate->Query($query);
        while ($row = $validate->FetchAllDatas()) {
            return $row[$zoneCol];
        }
    } elseif($amt >= 4000 && $amt <= 4499) {
        $zoneCol = 'SWC_4000_4500';
        $query = "SELECT $zoneCol FROM shipping_weight_charges WHERE ZoneId = '".$id."'";
        $validate->Query($query);
        while ($row = $validate->FetchAllDatas()) {
            return $row[$zoneCol];
        }
    } elseif($amt >= 4500 && $amt <= 4999) {
        $zoneCol = 'SWC_4500_5000';
        $query = "SELECT $zoneCol FROM shipping_weight_charges WHERE ZoneId = '".$id."'";
        $validate->Query($query);
        while ($row = $validate->FetchAllDatas()) {
            return $row[$zoneCol];
        }
    } elseif($amt >= 5000 && $amt <= 10000) {
        $zoneCol = 'SWC_5000_10000';
        $query = "SELECT $zoneCol FROM shipping_weight_charges WHERE ZoneId = '".$id."'";
        $validate->Query($query);
        while ($row = $validate->FetchAllDatas()) {
            return $row[$zoneCol];
        }
    }
}
$region = 0;
if (isset($_POST['selectZone'])) {
    $region = SelectZoneAmount($_SESSION['cartWeight'], $_POST['selectZone']);
    $_SESSION['TotalWeight'] = $region;
    echo number_format($_SESSION['TotalWeight'], 2);
}

?>

当我进入结帐页面并选择送货区域时,我无法刷新购物车总金额。我知道某处肯定存在逻辑错误。请帮助我纠正错误。

提前谢谢你。

1 个答案:

答案 0 :(得分:0)

我认为,发现了错误:

 $_SESSION['sessionTotalPayable'] = number_format($totalPayableAmnt, 2);  

您可以使用session var功能在number_format()中设置值。这是错误的方式。

您可以使用仅number_format()功能查看部分...

因为,number_format()返回一个字符串。所以,它无法计算。

请根据以上条件检查您的代码。 那可能会解决。