My code is so far is error free, I've managed to grasp the concept.
So far the shop stores data in cookies. After visiting some ecommerce sites, I noticed that's the way they do it as well however they all seemed to have problems when cookies are disabled. So I added a code to check if cookies are disabled and I will probably use that to redirect the user in a customer registration page so I can store his items in a mysql database, server side using his account.
One problem I am having though is that I want to try out implementing Paypal checkout and Google checkout as payment methods. Like I said my cart items are currently stored as cookies on the user's browser. The cookie format is as follows:
2_5
this means 2 is the product id and 5 is the quantity while _ is the delimiter,
So, will I have to modify my cookie format for it to work across Paypal and Google? Do these services use cookies?
Am I doing something wrong in the first place and I must do it somehow else?
Here's my code for the whole thing:
Code: Select all
<?php
ob_start();
require_once('database-connect.php');
//check if cookie support is on after each purchase
if ($_GET['check_cookies']==1)
{
if (isset($_COOKIE['is_cookie_enabled']))
{}else{
echo "Cookies were rejected! Please enable cookies in your browser, in order to store items in your cart!";
}
}
echo "<h2>Shopping Cart</h2>
<img src='cart.png' alt='Your cart' border='0' /> Cart Items:<br/>";
/*LIST PRODUCTS IN CART*/
$cookievalue=0;
if (isset($_COOKIE['cart']))
{
$total_cost=0;
$total_shipping_cost=0;
foreach ($_COOKIE['cart'] as $cookievalue)
{
$cart_array = explode("_", $cookievalue);
$get_id=$cart_array[0];
$get_quantity=$cart_array[1];
$get_id=mysql_real_escape_string($get_id,$db_connection);
$prod_query="SELECT * FROM products WHERE row_id='$get_id'";
$prod_result = mysql_query($prod_query) or die('Error: ' . mysql_error());
while($row = mysql_fetch_array($prod_result))
{
$get_id=$row['row_id'];
$get_title=$row['row_title'];
$get_price=$row['row_price'];
$get_shipping_cost=$row['row_shipping_cost'];
$get_price=$get_price*$get_quantity;
$get_shipping_cost=$get_shipping_cost*$get_quantity;
$total_cost=$total_cost+$get_price;
$total_shipping_cost=$total_shipping_cost+$get_shipping_cost;
$total_checkout_cost=$total_cost+$total_shipping_cost;
$i=$i+1;
echo "$get_title x<strong>$get_quantity</strong> | $get_price € | Shipping: $get_shipping_cost € | <img src='cart_remove.png' alt='Your cart' border='0' /><a href='index.php?action=removeitem&id=$get_id'>Remove</a><br/>";
}
}
echo "<br/>
<a href='index.php?action=removeall' onclick='javascript:if (confirm(\"Do you really want to clear your shopping cart?\")){return true;}else{return false;}'>Empty Cart</a><br/>
<em>Total Cost</em>: $total_cost €<br/>
<em>Total Shipping Cost</em>: $total_shipping_cost €<br/>
<strong>Final Total</strong>: $total_checkout_cost €<br/>
<a href='index.php?action=checkout'>CheckOut</a>";
}
else
{
echo "There are no items in your shopping cart!<br/>";
}
echo "<br/><br/>";
/* LIST eShop products */
$prod_query="SELECT * FROM products";
$prod_result = mysql_query($prod_query) or die('Error: ' . mysql_error());
while($row = mysql_fetch_array($prod_result))
{
$get_id=$row['row_id'];
$get_title=$row['row_title'];
$get_price=$row['row_price'];
$get_description=$row['row_description'];
$get_shipping_cost=$row['row_shipping_cost'];
echo "<strong>$get_title</strong>, Price: $get_price € | Shipping: $get_shipping_cost € | <form name='form$get_id' id='form$get_id' style='display:inline;' method='get' action='index.php'>
Quantity: <input type='text' name='quantity' value='1' style='width:32px;' />
<input type='hidden' name='action' value='addtocart' />
<input type='hidden' name='id' value='$get_id' />
<input type='hidden' name='title' value='$get_title'/>
<img src='cart_add.png' alt='Your cart' border='0' /> <input type='submit' value='Add to cart' /></form><br/>
$get_description
<br/>--------<br/>";
}
//add an item
if ($_GET['action']=='addtocart')
{
if (isset($_GET['id']))
{
$id=strip_tags($_GET['id']);
$title=strip_tags($_GET['title']);
$quantity=strip_tags($_GET['quantity']);
foreach ($_COOKIE['cart'] as $cookievalue)
{
$cart_array = explode("_", $cookievalue);
$get_id=$cart_array[0];
if ($get_id==$id)
{
$get_quantity=$cart_array[1];
$quantity=$get_quantity+$quantity;
}
}
//cookie format productid_quantity
setcookie("cart[$id]", $id.'_'.$quantity,time()+60*60*24*365);
setcookie('is_cookie_enabled', 1);
header('Location: index.php?check_cookies=1');
exit;
}
}
//remove an item
if ($_GET['action']=='removeitem')
{
$id=strip_tags($_GET['id']);
setcookie("cart[$id]",$id,time()-200);
header('Location: index.php?check_cookies=1');
exit;
}
//remove all items - empty cart
if ($_GET['action']=='removeall' && isset($_COOKIE['cart']))
{
foreach ($_COOKIE['cart'] as $cookievalue)
{
$cart_array = explode("_", $cookievalue);
$get_id=$cart_array[0];
$get_quantity=$cart_array[1];
setcookie("cart[".$cart_array[0]."]",$cart_array[1],time()-200);
}
header('Location: index.php?check_cookies=1');
exit;
}
ob_end_flush();
?>