문제

I am trying to setup a “view shopping cart/basket” page within a site in which logged in users earn points/credits. Once they earn a certain amount of these points they can then go to a shopping cart and pay with these points only. (No money changes hands, so no paypal/checkout/shipping/taxes etc are involved)

So far I have got the login, points total table, add product to cart and change of quantity feature to work.

What I am trying to do on this ‘view_cart.php’ page (code below) is to make the ‘Checkout’ link (submit_cart.php) disappear or be disabled if the user's points total is less than the total shopping cart price. Is there anyway I can do this on this script? The ‘You don’t have enough points to proceed to checkout’ prompt works if this is the case but if I can this checkout link to disappear that would be great.

My php knowledge is limited as I’m more of a front end designer but please feel free to offer any suggestions or changes of approach. Thanks!

<?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');
?>
도움이 되었습니까?

해결책

It looks to me like you're darn close:

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 />";
}

In your sample, these lines are in the while which will cause a problem. Just move them out to where you want this to display and you're on your way.

다른 팁

$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;

Use the above code instead of the code bellow the following comment in your code

// products the footer, close the table, and the form.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top