Jump to content
Larry Ullman's Book Forums

Problems With Ecommerce Example 2 Demo Site


Recommended Posts

I am having two problems with regards to the demo site that Larry has posted on his website for Example 2, the coffee-related shopping cart. Because the site is on larryullman.com, my PHP/MySQL versions that I am running are not relevant.

 

PROBLEM 1:

 

I have several items in my shopping cart, and I am attempting to remove some items from the cart. The "remove from cart" link works just fine, and removes the product from my shopping cart. However, I should be able to remove an item from my cart simply by updating the quantity to 0. As stated in the tip on p. 232, "The site offers two ways to remove items from the cart: clicking a link and entering zero for the quantity." However, anytime I try to remove an item by entering 0 as the quantity, the site will simply reduce the quantity to 1.

 

PROBLEM 2:

 

I am having problems with the "billing" part of the checkout process. On the demo site, I am given several test credit card numbers. No matter the credit card I try, I get the following error:

 

__________________________________________________________________________________

 

6011825550978435

There is a way to test the payment system to get card rejections, but that would require manually changing the code. If you use an expiration date in the past, the card will fail, though. (You'll see the error message appear just after this.)

 

The merchant login ID or password is invalid or the account is inactive. Please fix the error or try another card.

 

__________________________________________________________________________________

 

Furthermore, I made sure to indicate that the expiration date of the credit card is in the future (i.e., I changed the default expiration date from January 2011).

 

 

Has anyone else had either of these problems, or is it just me? I've been playing around with this for about an hour.

Link to comment
Share on other sites

I haven't personally seen these problems but I'm looking into it. For the first problem, for seem reason the filter_var() function is converting quantities of 0 to 1, which you'll see if the quantity of an item is more than 1, and you attempt to set it to 0: it comes back as 1. I'll have to look into this further. As for the second issue, it's possible the merchant ID has expired. I'll confirm that, too.

 

Also, just to confirm, the demos are on DMCInsights.com, not LarryUllman.com. Thanks for pointing out the problem!

Link to comment
Share on other sites

I haven't personally seen these problems but I'm looking into it. For the first problem, for seem reason the filter_var() function is converting quantities of 0 to 1, which you'll see if the quantity of an item is more than 1, and you attempt to set it to 0: it comes back as 1. I'll have to look into this further. As for the second issue, it's possible the merchant ID has expired. I'll confirm that, too.

 

Also, just to confirm, the demos are on DMCInsights.com, not LarryUllman.com. Thanks for pointing out the problem!

 

Hi Larry,

 

1. Yes, you're correct -- the site is on dmcinsights.com.

2. Just to clarify, even when the quantity is 1 (and not greater than 1), and I reset to 0, the quantity remains 1.

 

Thanks!

Link to comment
Share on other sites

As for point 2, yes, exactly. What I'm saying is that you'll see the effect if the quantity is greater than 1 and you set it to 0: it becomes 1. If the quantity is 1 and you set it to 0, it also becomes 1 but you can't tell that because it looks like no change occurred. I'll debug this and see why it's failing.

Link to comment
Share on other sites

  • 3 weeks later...

Thanks again for catching this. It was a bug in the code and I'll add a note to the errata page after I post this reply. The code in question is this:

 


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

What the code is supposed to do is assign the value of $qty to the $qty variable if it's an integer greater than 0. If not, the value 1 should be assigned to $qty.

 

To understand that bug, we've got to think about how filter_var() works and how conditionals work in PHP. The filter_var() will return the filtered data, if it works, or FALSE. With a $qty value of 0, filter_var() will return 0. In PHP, the conditional

if (0) {

is FALSE. So, in such cases, $qty is assigned the value of 1. This is a common enough kind of bug to put in code that I should have thought of it earlier (like when I was writing the book).

 

The solution is not to use filter_var() straight up as the condition, but rather compare its output to FALSE:

 

 


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

 

And the bug is gone. This change needs to be made in cart.php and wishlist.php. And one would want to use the !== false construct whenever the basis of a condition could return a valid value that PHP would interpret as FALSE (i.e., you need to do this in conditionals when there's a distinction between FALSE-ish values such as 0 and actual FALSE).

 

Apologies again for the confusion and thanks for pointing it out.

Link to comment
Share on other sites

Good question. == is the equality operator and === is the identical operator. For example 0 and "0" are equal in PHP but they are not identical. The same goes for 0 and FALSE: equal, not identical. Conversely, != is the not equals operator and !== is the not identical operator. Since 0 can be interpreted as false, using != false wouldn't fix the bug, because 0 does equal false. By using !==, the conditional confirms that the returned result isn't FALSE, specifically.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...