2017-09-14 92 views
-1

我是PHP/MySQL的新手,一直在关注一个简​​单购物车的教程,我已经设法启动并运行,但我现在试图让一些改变并且难以找到解决方案。简单购物车的PHP折扣代码

是否可以在下面的代码中添加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

首先:你的代码似乎不完整的,因为它有一个错误,他们在你的代码中使用之前的一些变量从未设置。

达到你想要什么,你需要:

  • 输入字段添加到您的形式 <input type="text" name="discount" />
  • 创建$discount变量,在你的代码false
  • 检查的值,如果(我假设邮政)$_POST['discount']已设置,如果它有您的折扣代码isset($_POST['discount']) && $_POST['discount']=="loyalty10"
  • 设置$discounttrue显示您的总和时
  • 添加添加条件

    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元素

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 
} 
} 

希望这会帮助你。