Jump to content
Larry Ullman's Book Forums

Passing The Cart Id Rather Than The Sku


Recommended Posts

It is, but couldn't that code be updated? I will post my thoughts for the changes tomorrow. My code will look vastly different than that of the book, but the general idea will still be there. I have update the cart code to work with various options and am trying to add one more. It will require either the cart id or another variable for the cart functionality to work. So, here's to cart I'd coding.

Link to comment
Share on other sites

I haven't had an opportunity to change the other Stored Procedures, but these changes will allow the items in the cart to be removed. If this isn't clear, please let me know, and I will rewrite it. Also, if you would like me to post all of the changes made once I have had an opportunity, I would gladly do so.

 

To get the cartId to work instead of the sku, first you will need to change these Stored Procedures: addToCart, removeFromCart, updateCart and getShoppingCartContents. The corresponding Stored Procedures for the Wish List will also need to be updated. I have changed my Stored Procedures from remove_from_cart to camel case: removeFromCart, and when updating the stored procedures, I don't delete the originals, but rather add a description after them so that I can go back if needed, in this case _cartId.

 

First you must change the getShoppingCartContent to get the cartId in the ski rather than the productId in the sku.

 

Here is a sample of the Stored Procedure getShoppingCartContents_cartId:

-- -----------------------------

-- getShoppingCartContents_cartId --

-- -----------------------------

 

DROP PROCEDURE IF EXISTS getShoppingCartContents_cartId;

 

DELIMITER $$

 

CREATE PROCEDURE getShoppingCartContents_cartId (uid CHAR(32))

BEGIN

SELECT CONCAT("O", cartId) AS sku,

c.quantity, categories.category, products.name, products.price, products.stock, sales.price AS sale_price,

CONCAT(products.name) AS combinedName

FROM carts AS c

INNER JOIN products ON c.productsId=products.productsId

INNER JOIN categories ON categories.categoryId=products.categoryId

LEFT OUTER JOIN sales ON (sales.productsId=products.productsId AND ((NOW() BETWEEN sales.start_date AND sales.end_date) OR (NOW() > sales.start_date AND sales.end_date IS NULL)) )

WHERE c.user_session_id=uid AND c.sp_type = 'option1'

 

There are UNION's after this, but the important part is to get the cartId in the SELECT statement. Add that to the UNION arguments. In this case, I replaced the productId with cartId in the sku.

 

 

Here are the new Stored Procedure for removeFromCart_cartId:

 

-- -----------------------------

-- removeFromCart_cartId --

-- -----------------------------

 

DROP PROCEDURE IF EXISTS removeFromCart_cartId;

 

DELIMITER $$

CREATE PROCEDURE removeFromCart_cartId (uid CHAR(32), pid int(10))

BEGIN

DELETE FROM carts WHERE user_session_id=uid AND pid=cartId;

END$$

DELIMITER ;

 

At the top of the page, there is a reference to the function to break the sku into its parts. For this demonstration, I am going to keep the cartId looking like the sku, so the cartId will actually come across as the $pid. When references the stored procedure, it's important to remember that. All that was changed in the removeFromCart_cartId Stored Procedure was pid=cartId.

 

The code in cart.php will also need to be updated to show the new Stored Procedure and to accept the new argument:

 

} elseif (isset ($sp_type, $pid, $_GET['action']) && ($_GET['action'] == 'remove') ) { // Remove it from the cart.

 

$r = mysqli_query($dbc, "CALL removeFromCart_cartId('$uid', '$pid')");

Link to comment
Share on other sites

Ah, I think I see what you're doing. I don't know what you gain by doing it that way, but if it works for you, great. The argument for using SKUs is to be consistent, as every other cart-related page and functionality is based on SKUs, too.

Link to comment
Share on other sites

There is actually a descent reason to do this. I have a client who would like a variable to be added to the cart. One which I don't have control over. I have had a hard time deciding how to pass that since it doesn't come directly from the database. If I pass it as information from the cart or wishlist, I won't have to pass it in the url. It seem that this might be a safer way to pass the variable. Would you have another suggestion, or would this provide the most security for the database?

Link to comment
Share on other sites

 Share

×
×
  • Create New...