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

まず:それはエラーがあり、彼らはあなたのコードで使用される前に、いくつかの変数が設定されなかったとして、あなたのコードが不完全なようです。あなたがする必要が望むものを達成する

  • (あれば、あなたのコード内でfalse
  • チェックの値を持つ$discount変数を作成し、フォームに <input type="text" name="discount" />
  • を入力フィールドを追加します私はPOSTと仮定します)$_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 } ?> 
    

を追加します。たとえば、html要素の代わりにcssクラスを使用することができます。

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

を作成し、これはあなたを助けることを願っています。

関連する問題