【问题标题】:Disabling checkout button from shopping cart - php禁用购物车中的结帐按钮 - php
【发布时间】:2011-04-30 01:13:41
【问题描述】:

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

到目前为止,我已经完成了登录、积分总表、将产品添加到购物车和更改数量功能。

我试图在这个“view_cart.php”页面(下面的代码)上做的是让“结帐”链接(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');
?>

【问题讨论】:

    标签: php mysql shopping-cart checkout


    【解决方案1】:
    $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;
    

    在你的代码中使用上面的代码而不是下面的代码注释

    // 生成页脚、关闭表格和表单。

    【讨论】:

    • 看起来史蒂夫和KMK的方法都有效,谢谢大家
    【解决方案2】:

    在我看来你已经很接近了:

    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 中,这会导致问题。只需将它们移到您想要显示的位置即可。

    【讨论】:

    • 是的,你是对的。结帐链接出现在购物车上方,该购物车位于我已向下移动到购物车内容下方的 while 循环中。再次感谢史蒂夫,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    相关资源
    最近更新 更多