从购物车禁用结帐按钮 - php

时间:2010-10-25 05:45:49

标签: php mysql shopping-cart checkout

我正在尝试在登录用户获得积分/积分的网站中设置“查看购物车/购物篮”页面。一旦他们赚取了一定数量的这些积分,他们就可以去购物车并只用这些积分付款。 (没有钱换手,所以不涉及PayPal /结账/运费/税收等)

到目前为止,我已获得登录,积分总表,将产品添加到购物车并更改数量功能。

我想在这个'view_cart.php'页面(下面的代码)上做的是让'Checkout'链接(submit_cart.php)消失或被禁用,如果用户的积分总和小于总购物车价钱。无论如何我可以在这个脚本上做到这一点吗? 如果是这种情况,“你没有足够的积分继续结账”提示工作,但如果我可以将这个结账链接消失,那就太棒了。

我的PHP知识有限,因为我更像是一名前端设计师,但请随时提供任何建议或改变方法。 谢谢!

<?php
$page_title = 'Your Rewards Shopping Cart';
include ('./includes/header.html');

if (!isset($_SESSION['users_id'])) {

   $url = 'http://' . $_SERVER['HTTP_HOST']
    . dirname($_SERVER['PHP_SELF']);
   if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
        $url = substr ($url, 0, -1); 
   }
   $url .= '/login.php'; 

ob_end_clean(); // Delete the buffer.
header("Location: $url"); 
exit(); // Quit the script.
}

$rwp = $_SESSION['reward_user_points']; 

$problem = FALSE; 

if (isset($_POST['submitted']))
   { 

foreach ($_POST['qty'] as $k => $v) {

$pid = (int) $k;
$qty = (int) $v;

if ( $qty == 0 ) { 
unset ($_SESSION['cart'][$pid]);
} elseif ( $qty > 0 ) { 
$_SESSION['cart'][$pid] ['quantity'] = $qty;
}

} // End of FOREACH.
} // End of SUBMITTED IF.

$empty = TRUE;
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key =>$value) {
if (isset($value)) {
$empty = FALSE;
break; // Leave the loop.
}

} // End of FOREACH.
} // End of ISSET IF.

if (!$empty) {

require_once ('/MySQL/database.php');

$query = "SELECT users_id, reward_user_points FROM reward_points
            WHERE reward_points.users_id = users.users_id";
$result = mysql_query($query);  

$query = "SELECT products_id, products_name FROM categories, products
   WHERE categories.categories_id = products.categories_id AND products.products_id 
   IN (";foreach ($_SESSION['cart'] as $pid =>$value) {
$query .= $pid . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY categories.categories_name ASC';
$result = mysql_query($query);
?>

 <h1>Your Shopping Cart</h1>

    <div id="sidebar">
    <div id="statusbar">
    <p><span class="statusbar_highlight">Name:</span><br />
    <?php echo " {$_SESSION['users_first_name']} " . " {$_SESSION['users_surname']}<br> ";?></p>
    <p><span class="statusbar_highlight">Outlet:</span><br />
    <?php echo " {$_SESSION['users_outlet']} ";?></p>
    <p><span class="statusbar_highlight">Sales Number:</span><br />
    <?php echo " {$_SESSION['users_sales_no']} ";?></p>     
    <p><span class="statusbar_highlight">My Points:</span><br />
    <font size="+1"><?php echo " {$_SESSION['reward_user_points']} ";?></font></p> 
    </div>
    <br /><br /><br /><br /><br /><br /><br /><br />
    </div>

  <div id="maincontent_inner">
  <div id="maincontent_inner2"> 

<?php
echo '<table border="0" width="100%" cellspacing="1" cellpadding="5" align="center">
<tr class="top">
<td align="left" width="46%"><b>Reward Product</b></td>
<td align="right" width="18%"><b>Price</b></td>
<td align="center" width="16%"><b>Qty</b></td>
<td align="right" width="20%"><b>Sub Total</b></td>
</tr>
<form action="view_cart.php" method="post">';

$total = 0; // Total cost of the order.

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

// Total and subtotals.
$subtotal = $_SESSION['cart'][$row
   ['products_id']]['quantity'] *
   $_SESSION['cart'][$row ['products_id']]['price'];
$total += $subtotal;

if ($rwp >= $total) {
    }   
    else {
    echo "You do not have enought points to proceed to checkout <br />";
    }

// Print the row.
echo " <tr>
<td align=\"left\">{$row['products_name']}</td>
<td align=\"right\">{$_SESSION['cart'][$row['products_id']] ['price']} pts</td>
<td align=\"center\"><input type=\"text\" size=\"3\"
   name=\"qty[{$row['products_id']}]\"
   value=\"{$_SESSION['cart'][$row['products_id']]['quantity']}\" /></td>
<td align=\"right\">" . number_format ($subtotal) . " pts</td>
</tr>\n";
} // End of the WHILE loop.

mysql_close($dbc); // Close the database connection.

// products the footer, close the table, and the form.
echo ' <tr class="even">
<td colspan="3" align="right"><b> TOTAL:<b></td>
<td align="right"><b>' . number_format ($total) . ' pts </b></td>
</tr>
</table>
<br />
<div align="center"><input type="submit" name="submit"
   value="Update" />
<input type="hidden" name="submitted"value="TRUE" />
</form><br /><br /></div>
<p><a href="browse_rewards.php"><img src="images/but_continue.png" /></a></p>
<p><a href="submit_cart.php"><img src="images/but_checkout.png" /></a></p>';

} else {
echo '<h1>Shopping Cart</h1><p>Your cart is currently empty.</p>
<p><a href="browse_rewards.php"><img src="images/but_continue.png" /></a></p>
<div id="maincontent_inner">
<div id="maincontent_inner2"> ';
}

?>
<br />
<p>
<span class="extras"><strong>Please Note the following:</strong><br />
1. To delete any item off your cart, simply type in '0' and click 'Update'<br />
2. To add in more than one item, simply click the desired amount and click 'Update'<br />
3. Your cart will be emptied upon logging out of your session<br />
</span></p>

</div>
</div>
</div>
</div>


<?php
include ('./includes/footer.html');
?>

2 个答案:

答案 0 :(得分:1)

在我看来,你似乎很接近:

if ($rwp >= $total) {    
  echo '<button>Checkout</button>';  //Just put the code you want here
}   
else {
  echo "You do not have enought points to proceed to checkout <br />";
}

在您的示例中,这些行位于while,这会导致问题。只需将它们移动到您想要显示的位置,即可开始使用。

答案 1 :(得分:0)

$str = '<tr class="even">
<td colspan="3" align="right"><b> TOTAL:<b></td>
<td align="right"><b>' . number_format ($total) . ' pts </b></td>
</tr>
</table>
<br />
<div align="center"><input type="submit" name="submit" value="Update" />
<input type="hidden" name="submitted"value="TRUE" />
</form><br /><br /></div>
<p><a href="browse_rewards.php"><img src="images/but_continue.png" /></a></p>
<p><a href="submit_cart.php"><img src="images/but_checkout.png" /></a></p>';
if($rwp >= $total) {
 $str .='<a href="submit_cart.php"><img src="images/but_checkout.png" /></a></p>';
}
else {
 $str .='<p>You donnot have enough points to buy</p>'; 
}   
echo $str;

使用上面的代码代替代码中以下注释下面的代码

//产生页脚,关闭表格和表单。