2017-09-14 91 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 
} 
} 

希望這會幫助你。