Topic: problem - Shopping Cart tutorial, video 7

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

<table class="shoppingCart">
    <tr>
        <th>Product ID</th>
        <th>Quantity</th>
        <th>Amount</th>
    </tr>
<?php
    $line_item_counter = 1;
    foreach($shopping_cart->GetItems() as $product_id) {
        echo render_shopping_cart_row($shopping_cart, $product_id, $line_item_counter);
        $line_item_counter++;
    }

function render_shopping_cart_row(ShoppingCart $shopping_cart, $product_id, $line_item_counter) {
    $quantity = $shopping_cart->GetItemQuantity($product_id);
    
    $output = "    
    <tr>
        <td>
            $product_id
        </td>
        <td>
            $quantity
        </td>
        <td>
            $ amount
        </td>
    </tr>
    ";
    
    return $output;
}
?>    
    
</table><!--end shoppingCart-->

<?php set_shopping_cart($shopping_cart);

?>

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

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?

Vote up Vote down

Re: problem - Shopping Cart tutorial, video 7

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

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: problem - Shopping Cart tutorial, video 7

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

Vote up Vote down

Re: problem - Shopping Cart tutorial, video 7

Update:  my shopping cart is 'magical' roll ...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:

<table class="shoppingCart">
    <tr>
        <th>Product ID</th>
        <th>Quantity</th>
        <th>Amount</th>

    </tr>    
    <tr>
        <td>TULIPCHOC</td>
        <td>3</td>
        <td>$74.97</td>
    </tr>
    
        <tr>
            <td></td>
            <td>Shipping:</td>
            <td>$32.97</td>
        </tr>
        <tr>
            <td></td>
            <td>Total:</td>
            <td>$107.94</td>

        </tr>
</table><!--end shoppingCart-->

it's good math, but not a practical application lol ...thoughts?

Vote up Vote down

Re: problem - Shopping Cart tutorial, video 7

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

???

Vote up Vote down

Re: problem - Shopping Cart tutorial, video 7

wahoo!...i've finally fixed this by using a button with addToCart.php as the action:

<form action="addToCart.php" method="post">
    <div>
    <input type="hidden" name="id" value="'.$product->id.'" />
    <input type="submit" value="Add To Cart" />
    </div>
</form>

but i have no idea why it works and the link doesn't...

Vote up Vote down

Re: problem - Shopping Cart tutorial, video 7

Provide an example of a link you are using? They both should work... you shouldn't be limited to just using a button.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: problem - Shopping Cart tutorial, video 7

here's the link code i'm using:

<a href="addToCart.php?id='.$product->id.'">Add to Cart</a>

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

Vote up Vote down

Re: problem - Shopping Cart tutorial, video 7

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.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: problem - Shopping Cart tutorial, video 7

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.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: problem - Shopping Cart tutorial, video 7

This is really weird... but I think I have found the issue.

In your "DocType.php" file, the last line is this: "<script type="text/javascript" src=""></script>"

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.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down