mindapolis Posted October 4, 2011 Report Share Posted October 4, 2011 could you help me figure why it’s not displaying the shipping and total amount. The values are always 0. 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 getItems() { return array_keys($this->items); } public function GetItemQuantity($product_id) { return intval($this->items[$product_id]); } public function emptyCart() { $this->items = array(); } public function GetSsalesTax() { $salesTax = .07; $subTotal=GetItemQuantity * $salesTax; return $subTotal; } public function GetShippingCost($quantity) { if ($quantity <=2) return 10.95; else return 0; } public function ShippingCost() { $total = 0; foreach($this->items as $product_id -> $quantity) $total = $total + $this->GetItemQuantity($product_id); return $total; } /* public function GetTotal() { $salesTax = .07; return $this->GetSubTotal() + $salesTax; }*/ public function GetSubTotal() { $total = 0; foreach($this->items as $product_id => $quantity) $total = $total + $this->GetItemCost($product_id); return $total; } public function GetSalesTax($unit_cost, $quantity) { $salesTax = .07; return $unit_cost*$quantity*$salesTax; } public function GetTotal() { return $this->GetSubTotal() + $this->GetShippingCost(); } } templates.php <?php function render_shopping_cart_row(ShoppingCart $shopping_cart , $product_id , $line_item_counter) { $quantity = $shopping_cart->GetItemQuantity($product_id); $amount = $shopping_cart->GetItemCost($product_id); $unit_cost = get_item_cost($product_id); $shipping_amount = $shopping_cart->GetShippingCost($quantity); $salesTax = $shopping_cart->GetSalesTax($unit_cost, $quantity); $output = " <tr> <td> $product_id <br /> Sales Tax: $salesTax </td> <td> $quantity </td> <td> </td> <td> $$amount </td> </tr>"; return $output; } function render_shopping_cart_shipping_row(ShoppingCart $shopping_cart) { return " <tr> <td> </td> <td> Shipping: </td> <td> '$.$shopping_cart->GetShippingCost().' </td> </tr> "; } function render_shopping_cart_total_row(ShoppingCart $shopping_cart) { return " <tr> <td> </td> <td> Total: </td> <td> '.$shopping_cart->GetTotal().' </td> </tr> "; } /*video 11 function render_paypal_checkout(ShoppingCart $shopping_cart) { return " <form action='".PAYPAL_FORM_URL."' method='post'> <input type="hidden" name="business" value=" " /> <input type="hidden" name="cmd" value="_cart" /> <input type="hidden" name="upload" value="1" /> <input type="hidden" name="currency_code" value="USD" /> <input type="hidden" name="LC" value="US" /> " . render_shopping_cart($shopping_cart) ." <input type='image' name='submit' src='https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif' alt=' pay online with PayPal '> </form>"; } <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_s-xclick"> <input type="hidden" name="hosted_button_id" value="M8GEXMCBNPSPC"> <input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"> <img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"> </form>*/ ?> checkout.php <?php require_once('functions.php'); require_once('templates.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <link href="doggyTreats.css" rel="stylesheet" type="text/css" /> <style type="text/css"> .shoppingCart { width: 600px; margin: 0; } </style> </head> <body> <?php $shopping_cart = get_shopping_cart(); if (isset($_GET["id"])) { $product_id = htmlspecialchars($_GET["id"]); } if (product_exist($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++; } echo render_shopping_cart_shipping_row($shopping_cart); echo render_shopping_cart_total_row($shopping_cart); ?> </table> <?php set_shopping_cart($shopping_cart); ?> </body> </html> Link to comment Share on other sites More sharing options...
jstern Posted October 4, 2011 Report Share Posted October 4, 2011 I have a hard time sifting through this, so I'll recommend trying to use the var_dump($value) function for your variables to track when / if the totals your expecting are ever getting set. Working backwards is usually where I start, so before your echoing out the variable is a good start. If its 0 there, use var_dump($value) in the function that does any work and passes it along until you've couldnt the culprit. I believe its un-related but there's a weird function that doesnt get used as far as I can tell: public function GetSsalesTax() { $salesTax = .07; $subTotal=GetItemQuantity * $salesTax; return $subTotal; } probably wanted that to look like: public function GetSsalesTax() { $salesTax = .07; $subTotal=$this->GetItemQuantity($product_id) * $salesTax; return $subTotal; } if this function gets used. (which i doubt since theres a GetSalesTax just below that that your probably using instead. You might want to follow proper function naming conventions as well to make your code a little more readable / professional. some are named 'emptyCart' (lowercase start, uppercaseadditional wording) which is more widely accepted / used. Others start with captial. Again, a non-issue, but good practice. Link to comment Share on other sites More sharing options...
Recommended Posts