[英]How to calculate total shipping fee using jquery select box change?
所以系统是这样工作的。 如果用户购买超过 500 美元,他们将获得免费送货费,否则将收取 10 美元的订单少于 500 美元。
所以我的问题是用户有两个选项可以选择接收他们的物品。 一种是“自取”,一种是“外送”
如何使我的系统根据下拉值更改计算总额的方式。 因为如果用户选择“自取”,则运费应自动设置为 0 美元。
我现在用 php 进行了计算。 所以当订单少于 500usd 时,它会自动在总价中增加 10usd。 下面是用于计算的 php 代码。
while ($row = $query->fetch_assoc()) {
$seller_id = $row['seller_id'];
$product_id = $row['product_id'];
$quantity = $row['cart_qty'];
$quantity_unit = $row['cart_unit'];
$purchase_price = $row['cart_price'] * $quantity;
if($purchase_price < 500.00){
$grand_total += ($row['cart_price'] * $row['cart_qty'])+10.00;
}else{
$grand_total += $row['cart_price'] * $row['cart_qty'];
}
$order_status = 1;
$datetime = date('Y-m-d H:i:s');
如何使用 jQuery select 换箱方法根据我上面解释的问题进行计算?
如果您使用普通的帖子形式进行计算,只需给 select 一个名称,然后在 php 中使用 $_POST['name'];
if you rather want to do the calculation each time the user changes the select, apply a onChange Handler in Javascript, get the value and send it to php using AJAX. 然后 PHP 应该返回总金额,因此您可以将值返回给 Javascript。
你可以测试一下:
<?php
//...
$grand_total = 0;
while ($row = $query->fetch_assoc()) {
$seller_id = $row['seller_id'];
$product_id = $row['product_id'];
$quantity = $row['cart_qty'];
$quantity_unit = $row['cart_unit'];
$purchase_price = $row['cart_price'] * $quantity;
$grand_total += $purchase_price;
}
//
// check user shipping select:
//
if(isset($_POST["shippingway"])) {
$haspost = true;
$shippingway = $_POST["shippingway"];
} else {
//
// by default we deliver to home:
//
$haspost = false;
$shippingway = 2;
}
//
// when deliver to home,
// add 10$ to grand total for a smaller order than 500$:
//
if($shippingway == 2) {
if($grand_total < 500) {
$grand_total += 10;
}
}
//
$order_status = 1;
$datetime = date('Y-m-d H:i:s');
//
if($haspost) {
echo "grand_total = $grand_total";
exit;
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("select[name='shippingway']").on("change", function(event) {
$.ajax({
url: location.href,
method: "POST",
data: {shippingway: this.value},
success: function (data) {
$("#info").html(data);
}
});
});
});
</script>
</head>
<body>
<form method="post">
<select name="shippingway">
<option value="1">self pick</option>
<option value="2" selected="selected">delivery to home</option>
</select>
...
</form>
<div id="info">grand_total = <?php echo $grand_total; ?></div>
</body>
</html>
我们添加 select "shippingway" 让用户 select 他最喜欢的运输方式,1=自取,2=送货到家,...
当然没有问题。这将是使用 AJAX 的示例:
我假设您的 HTML 看起来像这样:
<select name="delivery" id="delivery">
<option selected value="pickup">Pickup</option>
<option value="delivery">Delivery</option>
</select>
<p id="grand_total"></p>
那么你的 Javascript 看起来像这样(假设你已经包含了 JQuery)
$('document').ready(function(ev){
$('#delivery').on('change',function(ev2){
// get the value
var delivery = $(this).val();
// send to your php file using AJAX
$.get("URL TO YOUR PHP FILE?delivery="+delivery, function(data, status){
// Data contains your grand total you could now display it
$('#grand_total').html(data);
});
});
});
最后在这里您的 PHP 文件:
$delivery = $_GET['delivery'];
// I would add variable for this
$shipping_fee = 0;
while ($row = $query->fetch_assoc()) {
$seller_id = $row['seller_id'];
$product_id = $row['product_id'];
$quantity = $row['cart_qty'];
$quantity_unit = $row['cart_unit'];
$purchase_price = $row['cart_price'] * $quantity;
if($purchase_price < 500.00){
$shipping_fee = 10.00;
}else{
$shipping_fee = 0.00;
}
// Also Check for the delivery option here
if( $delivery == 'pickup')
{
// Shipping Fees should always be 0
$shipping_fee = 0.00;
}
// Finally do the calculation here
$grand_total += ($row['cart_price'] * $row['cart_qty'])+$shipping_fee;
}
$order_status = 1;
$datetime = date('Y-m-d H:i:s');
// For your ajax request print and return result
echo $grand_total;
return $grand_total;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.