我是PHP / MySQL的新手,并且已经关注了一个简单的购物车教程,我已经设法启动并运行但是我现在正在尝试做一些更改并且遇到一些困难溶液
是否可以在以下代码中添加10%的简单折扣选项。基本上只是一个带有“应用”按钮的文本框,然后当用户输入折扣代码“loyalty10”时。购物车更新后可以通过原始金额的一条线给予总金额10%的折扣。
view_cart.php
<form method="post" action="cart_update.php">
<table width="100%" cellpadding="6" cellspacing="0"><thead><tr><th>Quantity</th><th>Name</th><th>Price</th><th>Total</th><th>Remove</th></tr></thead>
<tbody>
<?php
if(isset($_SESSION["cart_products"]))
{
$total = 0;
$b = 0;
foreach ($_SESSION["cart_products"] as $cart_itm)
{
$product_name = $cart_itm["product_name"];
$product_qty = $cart_itm["product_qty"];
$product_price = $cart_itm["price"];
$product_code = $cart_itm["product_code"];
$product_weight = $cart_itm["weight"];
$subtotal = ($product_price * $product_qty);
$bg_color = ($b++%2==1) ? 'odd' : 'even';
echo '<tr class="'.$bg_color.'">';
echo '<td><input type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /></td>';
echo '<td>'.$product_name.'</td>';
echo '<td>'.$currency.$product_price.'</td>';
echo '<td>'.$currency.$subtotal.'</td>';
echo '<td><input type="checkbox" name="remove_code[]" value="'.$product_code.'" /></td>';
echo '</tr>';
$total = ($total + $subtotal);
}
$grand_total = $total + $shipping_cost;
foreach($taxes as $key => $value){
$tax_amount = round($total * ($value / 100));
$tax_item[$key] = $tax_amount;
$grand_total = $grand_total + $tax_amount;
}
$list_tax = '';
foreach($tax_item as $key => $value){
$list_tax .= $key. ' : '. $currency. sprintf("%01.2f", $value).'<br />';
}
$shipping_cost = ($shipping_cost)?'Shipping Cost : '.$currency. sprintf("%01.2f", $shipping_cost).'<br />':'';
}
?>
<tr><td colspan="5"><span style="float:right;text-align: right;"><?php echo $shipping_cost. $list_tax; ?>Amount Payable : <?php echo sprintf("%01.2f", $grand_total);?></span></td></tr>
<tr><td colspan="5"><a href="index.php" class="button">Add More Items</a><button type="submit">Update</button></td></tr>
</tbody>
</table>
<input type="hidden" name="return_url" value="<?php
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
echo $current_url; ?>" />
</form>
答案 0 :(得分:0)
首先:您的代码似乎不完整,因为它有错误,并且在代码中使用之前从未设置过某些变量。
实现您想要的目标:
<input type="text" name="discount" />
$discount
变量,其值为false
$_POST['discount']
已设置且是否有折扣代码isset($_POST['discount']) && $_POST['discount']=="loyalty10"
$discount
设为true
显示您的和时添加条件
if($discount){
<tr><td colspan="5"><span style="float:right;text-align: right;"><?php echo $shipping_cost. $list_tax; ?>Amount Payable : <s><?php echo sprintf("%01.2f", $grand_total);?></s></span></td></tr>
<tr><td colspan="5"><span style="float:right;text-align: right;">Amount Payable : <?php echo sprintf("%01.2f", $grand_total * 0.9);?></span></td></tr>
<?php }else{ ?>
<tr><td colspan="5"><span style="float:right;text-align: right;"><?php echo $shipping_cost. $list_tax; ?>Amount Payable : <?php echo sprintf("%01.2f", $grand_total);?></span></td></tr>
<?php } ?>
有更好,更清洁的方法来做到这一点。您可以使用css类而不是html元素,例如
答案 1 :(得分:0)
使用ajax检查促销代码是否正确无需重新加载整个页面。
$(document).ready(function(){
$("#submitbuttonid").click(function(){
var discount=$.trim($("#discountfeildid").val());
$.ajax({
url:"page.php",
method:"POST",
data:{discount:discount},
cache:false,
success:function(data){
if(data){
$("#wherepriceisbeingshowID").load(location.href + " #wherepriceisbeingshowID");
}
}
error:function(){
alert('An Error Occured');
}
});
});
});
创建page.php
if(isset($_POST['discount'])){
if($_POST['discount']=='your discount code'){
//reset your cart total amount
//return some data
}
}
希望这会对你有所帮助。