In this session we show how to add add error checking to the PHP function that returns the California local sales tax rate. We also try changing the cities to see if the tax rates will change.
Rick: Now that we know that our thing is working right, we’re going to add the new code to this and that new code is this for each. So for each local rates as key value, cleaned key equals string to lower preg_replace such and such key and then we’ll just say caliRates clean key equals value. Okay that’s fine. So what this does is this creates a brand new array called caliRates.
First thing it does is it cycles through this entire thing. Each one, it cleans the key and then it adds that cleaned key to a new array with the original value.
The next part of this function is cleaned city. So cleaned city equals string to lower preg_replace city and then… so now it’s… yeah, so clean city calibrates cleaned city, that’s right. So now, local rate equals caliRates and cleaned city. This is back in the bad, old days when I didn’t do this properly. Okay, cleaned key. I should just fix this. Cleaned key, caliRates, okay.
Member: If you don’t mind, I just want to walk through this. So you’re setting about 4 lines up. You’re setting the variable city equal to that line of code there, I guess, is coming directly from Shopp.
Rick: It’s coming from the order form, the shipping city.
Member: Okay so that’s the user-entered city and then you’re cleaning that with your string to lower function.
Rick: And preg_replace, right.
Member: And that’s giving you something called cleaned city.
Member: Okay and then…
Rick: Which is essentially the city name with all lowercase, no spaces and no special characters.
Member: Okay great. And then for each is… let me see, you’re testing your local rates. Now is local rates this table? Is that what you’ve called that?
Rick: yeah. It’s not a table. It’s an array. Local rates is that array.
Member: Right. And then you set the key to… value is the value in your array? Is that what you called it? I can’t see what you have above.
Rick: You’re talking about right here, key and value. These are variables that represent each side of the array. So local rates has key values so it’s key and value.
Member: Okay, perfect.
Rick: It could be fish and sticks and it would still be the same.
Member: No, that’s fine. I just didn’t know what you were calling it above.
Member: Okay, I’m all set. Thanks.
Rick: Okay and so this clean city needs to go in there, not city. Okay, clean city and it’s not caliRates anymore. It is this caliRates. There we go, properly formatted value. Okay, local rate equals caliRates clean city. If the local rate is blank then rate is not equal to blank then rate equals local rate. Otherwise, rate is equal to 7.25 and return the rate.
So we save this. We upload it. We’re going to edit our shopping cart then proceed to checkout. We’re going to do Tara Bella. Oh, it still got that wrong. Tara Bella… did I upload this? I don’t think I did, did I?
Member: You need to clear the cache?
Rick: No. I think I probably failed to upload it. Estimate shipping and taxes should be giving us a new value right now. It should be giving me the higher value.
Member: You’re still on the estimate screen though.
Rick: Yeah. The estimate screen should be changing. Oh no, right. Of course, I’ve gotten rid of the…
Member: This is still just the USPS function within Shopp that’s testing based on that zip code, right?
Rick: Well, except that the estimated tax is coming from the estimated tax as well.
Member: But you’re not plugging… on this screen, you’re not plugging that function back into Shopp’s checkout. It only happens on the next page, right?
Rick: Yes, that’s right. Okay and so then update the subtotal, proceed to checkout. Nonsense.
Member: It’s such a long list that might be in there.
Rick: Okay so it gave the default. However, what was the… you said Redondo Beach?
Rick: If we do Redondo Beach, the tax didn’t change. Okay so $30, no.
Member: $2.17 divided by 30 is 0.725.
Rick: Okay so what’s happening at the moment, I’ve got a variable issue here, I think. Clean city equals string to lower city. Local rate, caliRates… no, this is not clean city. This is cleaned key. Okay caliRates cleaned key equals value. That’s what it was. And then clean city… okay, there we go. Yeah, you have to have the right values in the right place in order for it to work which is why I love copying code rather than… okay. So now, it gave the higher estimate, $2.62. And then because that’s what we told it to do, give a high estimate or 8.75. And so now that we have a city in here, we can go to Redondo Beach and checkout PayPal and it’s $2.62.
Member: Which it was.
Rick: However, if we just do Redondo…
Member: You can type in Tara Bella and see if it changes.
Rick: Okay, Tara Bella. If we do Tara Bella, it’s $2.33. If we do Tara Bella, hopefully it’s $2.33. Yeah, it is. That’s not what I wanted. I didn’t want to go to PayPal. I want to come back here. If we say fish sticks, it’s going to be $2.17. Okay so that part of it is working.
Rick: Okay so I’m going to post this code on the forum and the next time we talk about it, I’ll add in the caching part.
Member: Which code are you uploading?
Member: shopp_custom functions?
Rick: What I was going to do is just upload the function.
Rick: Although you’re right. What I really should do is just make this into a zip file and upload the whole file.