..
if (self::$_mCartId == '')
{
// If the visitor's cart ID is in the session, get it from there
if (isset ($_SESSION['cart_id']))
{
self::$_mCartId = $_SESSION['cart_id'];
}
// If not, check whether the cart ID was saved as a cookie
elseif (isset ($_COOKIE['cart_id']))
{
// Save the cart ID from the cookie
self::$_mCartId = $_COOKIE['cart_id'];
$_SESSION['cart_id'] = self::$_mCartId;
// Regenerate cookie to be valid for 7 days (604800 seconds)
setcookie('cart_id', self::$_mCartId, time() + 604800);
}
else
{
/* Generate cart id and save it to the $_mCartId class member,
the session and a cookie (on subsequent requests $_mCartId
will be populated from the session) */
self::$_mCartId = md5(uniqid(rand(), true));
// Store cart id in session
$_SESSION['cart_id'] = self::$_mCartId;
// Cookie will be valid for 7 days (604800 seconds)
setcookie('cart_id', self::$_mCartId, time() + 604800);
}
}
}
// Returns the current visitor's card id
public static function GetCartId()
{
// Ensure we have a cart id for the current visitor
if (!isset (self::$_mCartId))
self::SetCartId();
return self::$_mCartId;
}
CHAPTER 12 ?– CREATING YOUR OWN SHOPPING CART 373
// Adds product to the shopping cart
public static function AddProduct($productId, $attributes)
{
// Build SQL query
$sql = 'CALL shopping_cart_add_product(
:cart_id, :product_id, :attributes)';
// Build the parameters array
$params = array (':cart_id' => self::GetCartId(),
':product_id' => $productId,
':attributes' => $attributes);
// Execute the query
DatabaseHandler::Execute($sql, $params);
}
// Updates the shopping cart with new product quantities
public static function Update($itemId, $quantity)
{
// Build SQL query
$sql = 'CALL shopping_cart_update(:item_id, :quantity)';
// Build the parameters array
$params = array (':item_id' => $itemId,
':quantity' => $quantity);
// Execute the query
DatabaseHandler::Execute($sql, $params);
}
// Removes product from shopping cart
public static function RemoveProduct($itemId)
{
// Build SQL query
$sql = 'CALL shopping_cart_remove_product(:item_id)';
// Build the parameters array
$params = array (':item_id' => $itemId);
// Execute the query
DatabaseHandler::Execute($sql, $params);
}
// Gets shopping cart products
public static function GetCartProducts($cartProductsType)
{
$sql = '';
// If retrieving "active" shopping cart products .
Pages:
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484