Jump to content

problem - Shopping Cart tutorial, video 7


mcChris

Recommended Posts

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?

Link to comment
Share on other sites

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);
}
?>

Link to comment
Share on other sites

Update: my shopping cart is 'magical' :rolleyes: ...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 :lol: ...thoughts?

Link to comment
Share on other sites

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...

 

???

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...