Jump to content

How to enforce Minimum QTY


TCombs

Recommended Posts

Here's the scenario....

 

A Customer clicks on a product on the product page, this will open up the page for the individual product.

In this example, it's a shirt.

 

The shirt is available in many sizes and colors.  The Minimum QTY for this product is : 12 .

The customer can select as many sizes and colors as they want, as long they select a total of 12 (all sizes + all colors = total).

 

After making their selections for this shirt, there is an "Add to Cart" button.

 

I would like to run a check to make sure that the total is equal to or greater than the minimum QTY.

 

Here is the form :

<?php
session_start();

require("db.php");
require("functions.php");

$validid = pf_validate_number($_GET['id'], "redirect", $config_basedir);

$prodsql = "SELECT * FROM products WHERE id = " . $_GET['id'] . ";";
$prodres = mysql_query($prodsql);
$numrows = mysql_num_rows($prodres);
$prodrow = mysql_fetch_assoc($prodres);

$prodcatsql = "SELECT * FROM categories WHERE id = " . $_GET['id'] . ";";
$prodcatres = mysql_query($prodcatsql);
$bulkcat = mysql_num_rows($prodcatres);

if($numrows == 0)
{
	header("Location: " . $config_basedir);
}
else
{
		if($_POST['submit'])
		{
			if(!$_SESSION['SESS_ORDERNUM'])
			{
				if($_SESSION['SESS_LOGGEDIN'])
				{
					$sql = "INSERT INTO orders(customer_id, registered, date) VALUES(" . $_SESSION['SESS_USERID'] . ", 1, NOW())";
					mysql_query($sql);
					session_register("SESS_ORDERNUM");
					$_SESSION['SESS_ORDERNUM'] = mysql_insert_id();
				}
				else
				{
					$sql = "INSERT INTO orders(registered, date, session) VALUES(0, NOW(), '" . session_id() . "')";
					mysql_query($sql);
					session_register("SESS_ORDERNUM");
					$_SESSION['SESS_ORDERNUM'] = mysql_insert_id();
				}
			}

			foreach ($_POST as $name => $value) {
				if (substr($name, 0, 4) == 'qty_' && $value != '' && is_numeric($value)) {
					$arrOptions = explode('_', $name);

					$sizeid = $arrOptions[1];
					$colorid = $arrOptions[2];
					$quantity = $value;

					$itemsql = "INSERT INTO orderitems(order_id, product_id, size_id, color_id, quantity) VALUES (" . $_SESSION['SESS_ORDERNUM'] . ", " . $_GET['id'] . ", " . $sizeid . ", " . $colorid . ", " . $quantity . ")";
					mysql_query($itemsql);
				}
			}

			$totalprice = $prodrow['price'] * $_POST['amountBox'] ;

			$updsql = "UPDATE orders SET total = total + " . $totalprice . " WHERE id = " . $_SESSION['SESS_ORDERNUM'] . ";";
			mysql_query($updres);

			header("Location: " . $config_basedir . "showcart.php");
		}
		else
		{
				require("header.php");
				echo "<div id='adminhome'>";
				echo "<form action='addtobasket.php?id=" . $_GET['id'] . "' method='POST'>";

				echo "<table cellpadding='10' border='0'>";

				echo "<tr>";
					if(empty($prodrow['image2'])) {
						echo "<td width='205'><img src='store-images/no-image-large.jpg' width='200' alt='" . $prodrow['name'] . "'>";
					}
					else
					{
						echo "<td width='205'>
						<img src='store-images/" . $prodrow['image2'] . "' width='200' alt='" . $prodrow['name'] . "'>";
					}
					echo "</td>";

					echo "<td>";
					echo "<h1>";echo $prodrow['name'];echo "</h1>";
					echo "<h2>";echo $prodrow['description'];echo "</h2>";
					//echo "<pre>" . wordwrap( $prodrow['description'] , 30 ) . "</pre>";
					echo "<p>";echo $prodrow['details'];echo "</p>";

					echo "<br>";


					if($bulkcat==0)
					{
						echo "<div id='bulk1'>";
						echo "<table cellpadding='2' border='0'>";
						echo "<tr>";
						echo "<td>Quantity</td>";
						echo "<td>12</td>";
						echo "<td>24+</td>";
						echo "<td>48+</td>";
						echo "</tr>";
						echo "<tr>";
						echo "<td>Price</td>";
						echo "<td>$" . money_format('%i', $prodrow['price']) . "</td>";
						echo "<td>$" . money_format('%i', $prodrow['price2']) . "</td>";
						echo "<td>$" . money_format('%i', $prodrow['price3']) . "</td>";
						echo "</tr>";
						echo "</table>";
						echo "</div>";

						echo "<div id='bulk2'>";
						echo "<table cellpadding='2' border='0'>";
						echo "<tr>";
						echo "<br><div align='center'><strong>For Sizes 2XL - 5XL</strong></div>";
						echo "</tr>";
						echo "<tr>";
						echo "<td>Quantity</td>";
						echo "<td>12</td>";
						echo "<td>24+</td>";
						echo "<td>48+</td>";
						echo "</tr>";
						echo "<tr>";
						echo "<td>Price</td>";
						echo "<td>$" . money_format('%i', $prodrow['price4']) . "</td>";
						echo "<td>$" . money_format('%i', $prodrow['price5']) . "</td>";
						echo "<td>$" . money_format('%i', $prodrow['price6']) . "</td>";
						echo "</tr>";
						echo "</table>";
						echo "</div>";
					}
					else
					{
						echo "<div id='bulk1'>";
						echo "<table cellpadding='2' border='0'>";
						echo "<tr>";
						echo "<td>Quantity</td>";
						echo "<td>12</td>";
						echo "<td>24+</td>";
						echo "<td>48+</td>";
						echo "</tr>";
						echo "<tr>";
						echo "<td>Price</td>";
						echo "<td>$" . money_format('%i', $prodrow['price']) . "</td>";
						echo "<td>$" . money_format('%i', $prodrow['price2']) . "</td>";
						echo "<td>$" . money_format('%i', $prodrow['price3']) . "</td>";
						echo "</tr>";
						echo "</table>";
						echo "</div>";
					}




					echo "</td>";
					echo "</tr>";
					echo "</table>";

					echo '<div id="basketmatrix">';
					echo '<table cellpadding="1" border="1">';
					echo '<tr>';
					echo '<td></td>';

					$arrsizes = array();
					$i = 0;
					$result = mysql_query("SELECT DISTINCT s.id, s.size FROM sizes s INNER JOIN productoptions p ON s.id = p.sizeid WHERE p.productid = '" . $_GET['id'] . "' ORDER BY s.id");

					while ($row = mysql_fetch_assoc($result)) {
						echo '<td class="heading">' . $row['size'] . '</td>';

						$arrsizes[$i] = $row['id'];

						$i++;
					}

					echo '</tr>';

					$i = 0;
					$result = mysql_query("SELECT DISTINCT c.id, c.color FROM colors c INNER JOIN productoptions p ON c.id = p.colorid WHERE p.productid = '" . $_GET['id'] . "' ORDER BY c.id");

					while ($row = mysql_fetch_assoc($result)) {
						echo '<tr>';
						echo '<td class="heading">' . $row['color'] . '</td>';

						foreach ($arrsizes as $sizevalue) {
							echo '<td><input type="text" name="qty_' . $sizevalue . '_' . $row['id'] . '" size="5" /></td>';
						}
						echo '</tr>';
					}

					echo '</table>';
					echo '</div>';

					echo "<br>";
					echo "<br>";

					echo '<table>';
					echo '<tr>';
					echo '<td>';
					echo '<p>You MUST order a total of <font color="#ff0000"><strong>(' . $prodrow['minimum'] . ')</strong></font> or more to add this item to your cart.<br>';
					echo '(all colors + all sizes = total)</p>';
					echo '</td>';
					echo '<tr>';
					echo '<td>';
					echo"<input type='submit' name='submit' value='Add To Cart'>";
					echo '</td>';
					echo '</tr>';
					echo '</table>';

					echo "</form>";
					echo "</div>";


		}  
}

require("footer.php");
?>

Link to comment
Share on other sites

That's a lot of code to crawl through so - no offense - I'm not really reading it.

 

Are you able to count how many items of a type (eg, shirt) have been selected? Even if not in your code, you could probably run a SQL query to get those numbers back.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.