Jump to content
Larry Ullman's Book Forums

Adding Variables With Products


Recommended Posts

I have added a size and color option to some of the products. I can get everything to work except for the update cart. Sometimes it will update some items, but not all of them. I changed the code to receive the information, I'm just not sure why it will update some items and not others. Size and color can be 0 if there are no options presented (I send a hidden input field with a value=0). I have some conditions set up to check and see if the value is 0. The options are working fine, and so is everything else. Inside the wishlist and the cart, everything but update is working, including moving the items to their other respective cart and removing them. I also couldn't decipher how it was choosing which ones to update and which ones not to update. Thanks for the help.

 

The stored procedure I am using is:

 

 

sid = sizeId

op2 = colorId

 

DELIMITER $$

CREATE PROCEDURE updateCart (uid CHAR(32), pid MEDIUMINT, sid VARCHAR(6), op2 VARCHAR(6), qty TINYINT)

BEGIN

IF qty > 0 THEN

UPDATE carts SET quantity=qty, date_modified=NOW() WHERE user_session_id=uid AND productsId=pid AND sizeId=sid and colorId=op2;

ELSEIF qty = 0 THEN

CALL removeFromCart (uid, pid, sid, op2);

END IF;

END$$

DELIMITER ;

 

The table structure for carts is:

 

 

 

CREATE TABLE `carts` (

`cartId` int(10) unsigned NOT NULL AUTO_INCREMENT,

`quantity` tinyint(3) unsigned NOT NULL,

`user_session_id` char(32) NOT NULL,

`productsId` mediumint(8) unsigned NOT NULL,

`sizeId` varchar(6) NOT NULL,

`colorId` varchar(6) NOT NULL,

`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

`date_modified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',

PRIMARY KEY (`cartId`),

UNIQUE KEY `productsId` (`productsId`,`sizeId`,`colorId`),

KEY `user_session_id` (`user_session_id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=23 ;

 

I am using:

PHP Version 5.2.14

MYSQL 5.0.77

 

  • 0

  • Quote

  • MultiQuote



Link to comment
Share on other sites

Here is a link to a product page: http://jwarrenconsulting.com/baertrax-tshirts/browse/Experiment/2. If you add different products to the page, that's when it stop working. If there are no size or color options, the variable that gets add for sizeId and colorId is 0. That corresponds with the database. Here is some HTML code, please let me know if you need additional code:

 

 

<form action="<?php echo '' . BASE_URL . ''; ?>cart.php" method="POST">

<table class="cart">

<tr>

<th class="Item">Item</th>

<th class="Quantity">Quantity</th>

<th class="Price">Price</th>

<th class="Subtotal">Subtotal</th>

<th class="Options">Options</th>

</tr>

 

<?php // Display the products:

 

$alternate = "2"; // colour rows

 

// Initialize the total:

$total = 0;

 

// Fetch each product:

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

 

// Get the correct price:

$price = get_just_price($row['price'], $row['sale_price']);

 

// Calculate the subtotal:

$subtotal = $price * $row['quantity'];

 

//zebra colouring for rows

if ($alternate == "1") {

$colour = "even";

$alternate = "2";

} else {

$colour = "odd";

$alternate = "1";

}

 

// Print out a table row:

echo '

<tr class="' . $colour . '">

<td class="Item"><a href="' . BASE_URL . 'products.php?sku=' . $row['sku'] . '">' . $row['name'] . '';

if ($row['sizeId'] !== '0') {

echo ', ' . $row['sizeId'] . '';

}

if ($row['colorId'] !== '0') {

echo ', ' . $row['colorId'] . '';

}

echo '</a></td>

<td class="Quantity">

<input type="text" name="quantity[' . $row['sku'] . ']" value="' . $row['quantity'] . '" size="2" class="small" />

<input type="hidden" name="sizeId" id="sizeId" value="' . $row['sizeId'] . '"/>

<input type="hidden" name="colorId" id="colorId" value="' . $row['colorId'] . '"/>

</td>

<td class="Price">$' . $price . '</td>

<td class="Subtotal">$' . number_format($subtotal, 2) . '</td>

<td class="Options"><a href="' . BASE_URL . 'wishlist.php?colorId=' . $row['colorId'] . '&sizeId=' . $row['sizeId'] . '&sku=' . $row['sku'] . '&action=move&qty=' . $row['quantity'] .'">Move to Wish List</a><br /><a href="' . BASE_URL . 'cart.php?color=' . $row['colorId'] . '&size=' . $row['sizeId'] . '&sku=' . $row['sku'] . '&action=remove">Remove from Cart</a></td>

</tr>';

 

// Check the stock status:

if ($row['stock'] < $row['quantity']) {

echo '

<tr class="error">

<td colspan="5" align="center">There are only ' . $row['stock'] . ' left in stock of the ' . $row['name'] . '. Please update the quantity, remove the item entirely, or move it to your wish list.</td>

</tr>';

}

 

// Add the subtotal to the total:

$total += $subtotal;

 

} // End of WHILE loop.

 

// Add the shipping:

$shipping = get_shipping($totalitems);

$total += $shipping;

echo '

<tr>

<th colspan="3" class="Price">Shipping & Handling</th>

<th class="Subtotal">$' . $shipping . '</th>

<th class="Options"> </th>

</tr>';

 

// Display the total:

echo '

<tr>

<th colspan="3" class="Price">Total</th>

<th class="Subtotal">$' . number_format($total, 2) . '</th>

<th class="Options"> </th>

</tr>';

 

?>

</table>

<input type="submit" value="Update Quantities" class="button" />

</form>

Link to comment
Share on other sites

Okay, so if I add a couple of things, I see that the product IDs are like O3 and O5 (those are letter O's, not zeros?). Is that correct? How are you distinguishing between two sizes of the same shirt?

 

For me to see why it's not updating, I'd need to see the code that does the actual updating.

Link to comment
Share on other sites

I think that it's in the array. I amusing $sid for the size. In the database it's sizeId and $op2 (since $cid is taken) for color, and it's colorId in the database. I haven't changed this: foreach ($_POST['quantity'] as $sku => $qty). I believe that's where the problem is. In this code, it only gets the sku (which I haven't changed from your book) and the quantity. Somehow I need to get the sizeId and the colorId. The variables are being sent correctly to the page, there just not populating here. This is the code that I am using in this section of cart.php (it works the same in wishlist.php):

 

 

} elseif (isset($_POST['quantity'])) { // Update quantities in the cart.

 

// Loop through each item:

foreach ($_POST['quantity'] as $sku => $qty) {

 

// Parse the SKU:

list($sp_type, $pid) = parse_sku($sku);

 

$sid=$_POST['sizeId'];

$op2=$_POST['colorId'];

 

if (isset($sp_type, $pid, $sid, $op2)) {

 

// Determine the quantity:

$qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 0)) !== false) ? $qty : 1;

 

// Update the quantity in the cart:

$r = mysqli_query($dbc, "CALL updateCart('$uid', $pid, '$sid', '$op2', $qty)");

 

$r = mysqli_query ($dbc, "SELECT name FROM products WHERE productsId=$pid");

 

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

 

if ($op2 !== '0' AND $sid !== '0') {

$message = 'You now have changed the quantity of ' . $row['name'] . ', ' . $sid . ', ' . $op2 . ' to ' . $qty . '.';

}

elseif ($sid !== '0') {

$message = 'You now have changed the quantity of ' . $row['name'] . ', ' . $sid . ' to ' . $qty . '.';

}

else {

$message = 'You now have changed the quantity of ' . $row['name'] . ' to ' . $qty . '.';

}

}

}

 

}

 

This also has come up in the billing.php an checkout.php. I am unable to delete the products that do not have enough inventory. I haven't changed that code, because I am trying to figure this out and have been unsuccesful. I believe the problem comes in here:

 

 

$remove[$row['sku']] = $row['quantity'];

 

How do I pass the sizeId and colorId here? I have tried multiple things and searched online and have been unsuccesful. Thank you for all you do.

Link to comment
Share on other sites

You need to create a way to assign a unique SKU to each custom product. This issue has come up elsewhere in this forum and it's the same thing I do with the coffees in the second ecommerce example: the type and size of coffee is built into each SKU.

Link to comment
Share on other sites

 Share

×
×
  • Create New...