mcChris Posted October 5, 2009 Report Share Posted October 5, 2009 i'm working my way thru the shopping cart tutorial...in video 7, 12:13 minutes in...i've just completed the code for $quantity: ><?php $product_id = $_REQUEST['id']; if (product_exists($product_id)) { $shopping_cart->AddItem($product_id); } ?> </pre> <table>Product IDQuantityAmount $product_id $quantity $ amount </table> <br><br><?php set_shopping_cart($shopping_cart);<br><br in the video, when he refreshes the page, the item quantity goes up 1 as expected...when i refresh my page, the item quantity goes up by 2 each time...not good... i've compared my code with the code base and don't see an error...in my first attempt, i continued on with the videos, hoping the issue would resolve, but it didn't...so, i scrapped that code and started again from scratch, with the same result...checked the code again and i'm still not seeing an error...any thoughts as to what might be the cause? Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 5, 2009 Report Share Posted October 5, 2009 The code you posted looks like it is from the page that views the current shopping cart... not the actual code that adds items to the shopping cart/keeps track of quantity. Can you post the code you are using for the AddItem method? (related to this line: $shopping_cart->AddItem($product_id) Quote Link to comment Share on other sites More sharing options...
mcChris Posted October 5, 2009 Author Report Share Posted October 5, 2009 from ShoppingCart.php: <?php class ShoppingCart { protected $items = array(); public function AddItem($product_id) { if(array_key_exists($product_id, $this->items)) { $this->items[$product_id] = $this->items[$product_id] + 1; } else { $this->items[$product_id] = 1; } } public function GetItemCost($product_id) { $cost_string = get_item_cost($product_id); $cost_float = "$cost_string" + 0; return $cost_float * $this->GetItemQuantity($product_id); } public function GetItemShippingCost($product_id) { return $this->GetItemQuantity($product_id) * $this->getShippingCostFor(get_item_cost($product_id)); } private function getShippingCostFor($price) { if ($price < 10) { return 1.99; } elseif ($price < 50) { return 10.99; } else { return 34.99; } } public function GetItems() { return array_keys($this->items); } public function GetItemQuantity($product_id) { return intval($this->items[$product_id]); } public function EmptyCart() { $this->items = array(); } } ?> and functions.php: <?php // DEFINE GLOBALS define ('STORE_XML_FILE', '../catalog.xml'); // DEFINE REFERENCES require_once ('templates.php'); require_once ('../classes/ShoppingCart.php'); // FUNCTIONS session_start(); function get_xml_catalog() { return new SimpleXMLElement(file_get_contents(STORE_XML_FILE)); } function get_item_cost($product_id) { foreach(get_xml_catalog() as $product) { if ($product_id == $product->id) { return $product->price; } } throw new Exception('item not found: ' . $product_id); } function product_exists($product_id) { foreach(get_xml_catalog() as $product) { if ($product->id == $product_id) return true; } return false; } function get_shopping_cart() { if (!isset($_SESSION['cart'])){ return new ShoppingCart(); } else return unserialize($_SESSION['cart']); } function set_shopping_cart($cart) { $_SESSION['cart'] = serialize($cart); } ?> Quote Link to comment Share on other sites More sharing options...
mcChris Posted October 6, 2009 Author Report Share Posted October 6, 2009 Update: my shopping cart is 'magical' ...this situation is truly bizarre! after staring at this code for another day, i've noticed that when i click on the 'addToCart' link, the addToCart page displays 1 item in the quantity with the correct amount, shipping, and total; however, on viewing the source code i see this: > </pre> <table>Product IDQuantityAmountTULIPCHOC3$74.97Shipping:$32.97Total:$107.94</table> it's good math, but not a practical application ...thoughts? Quote Link to comment Share on other sites More sharing options...
mcChris Posted October 6, 2009 Author Report Share Posted October 6, 2009 to further complicate things, the problem behavior occurs in both firefox 3.0 and google chrome; but in ie6 (i'm sorry), the shopping cart code performs perfectly, as expected...i can add an item, go back and add more, etc and the source codes is correct... ??? Quote Link to comment Share on other sites More sharing options...
mcChris Posted October 7, 2009 Author Report Share Posted October 7, 2009 wahoo!...i've finally fixed this by using a button with addToCart.php as the action: but i have no idea why it works and the link doesn't... Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 7, 2009 Report Share Posted October 7, 2009 Provide an example of a link you are using? They both should work... you shouldn't be limited to just using a button. Quote Link to comment Share on other sites More sharing options...
mcChris Posted October 7, 2009 Author Report Share Posted October 7, 2009 here's the link code i'm using: Add to Cart it's very weird and hard to explain...the two browsers (ff & chrome) were seemingly refreshing the page when i clicked the link, thus adding another product in the page transition, giving me 1 product in the cart and 2 in the source code... ie6 doesn't do this...and i've been conditioned to believe if ie6 gets something right, it's really wrong... i also put the project code on my server, only changing the '<?=' to '<?php echo' and got the same weird browser behavior...and, i've been over the code several times and can't see an error anywhere... so i gave up and used the button...and it works...i don't know why... Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 7, 2009 Report Share Posted October 7, 2009 I really have no idea on this one. The code itself seems to be fine. If you create a zip file with all your project files in it, and email it to ben [at] falkencreative.com, I can run it on my PHP server and take a look... That's probably the only way I can help at the moment. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 7, 2009 Report Share Posted October 7, 2009 Thanks for sending the files over. Yeah, I'm getting the same weird issue you are... When I click the "add to cart" link, it takes me to the Add To Cart page, and adds one item... but the source code shows two items being added. Clicking on the "View Cart" button then shows the real amount. Haven't run into this issue before, but I'll take a look and see if I have any ideas. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 8, 2009 Report Share Posted October 8, 2009 This is really weird... but I think I have found the issue. In your "DocType.php" file, the last line is this: "" As long as the src="", you'll have an issue where items get added to the cart two at a time. The minute you add something within the src="", or remove that line completely, it'll work fine. I'm guessing that the browser is having some sort of Javascript error because of the missing src value. Why that would cause the page to reload (or whatever is causing the doubling of adding products to the cart), I'm not exactly sure. Try that, and let me know if that fixes things for you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.