I know there are many open source shopping carts available, but I wanted to write my own to gain a better understanding of the coding practices involved. Below is my almost finished shopping cart class for an e-commerce website I am building. I want to be able to handle all purchasing capability from the Cart class as you can see from what I have below. I was wondering if anyone can see any areas for improvement before I go implementing it into my website. Any help is appreciated and thank you in advance.
<?php
/**
* Cart class handles all shopping cart activity.
*
* This class also handles saving order information for each purchase.
*
* After each order is complete be sure to call the Product::inventoryCheck()
* to notify administrator of low quantities.
*/
class Cart
{
public $db;
private $_cartName;
/**
* Creates a new shopping cart if one does not exist already.
* Initiates the database connection for future use.
*/
public function __construct($cartName = 'cart') {
$this->_cartName = $cartName;
if (!isset($_SESSION[$this->_cartName])) {
$_SESSION[$this->_cartName];
}
$this->db = Database::getInstance();
}
/**
* Returns all items in the shopping cart and their information.
* I want to be able to return the quantity as well.
* I will take this information and use it to display the shopping cart to the customer.
*/
public function getCartItems() {
try {
foreach ($_SESSION[$this->_cartName][$id] as $productId => $quantity) {
$stmt = $this->db->prepare(
"SELECT
product_id,
product_name,
product_thumb,
product_price,
product_discount
FROM product
WHERE product_id = ?");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute(array($productId));
return array($stmt->fetchAll(), $quantity);
}
} catch (Exception $e) {
return FALSE;
}
}
/**
* Adds an item to the shopping cart.
* Also used to change quantities of items already in shopping cart.
* @param $id - integer
* @param $quantity - integer ($quantity comes from a drop down menu populated by the database)
*/
public function addItem($id, $quantity) {
try {
$_SESSION[$this->_cartName][$id] = $quantity;
} catch (Exception $e) {
return FALSE;
}
}
/**
* Deletes an item from the shopping cart.
* @param $id - integer
*/
public function deleteItem($id) {
try {
unset($_SESSION[$this->_cartName][$id]);
} catch (Exception $e) {
return FALSE;
}
}
/**
* Empties all contents out of the shopping cart.
* Call this function when all purchase activity is complete so the user has a new cart to start with.
*/
public function clearCart() {
try {
unset($_SESSION[$this->_cartName]);
} catch (Exception $e) {
return FALSE;
}
}
/**
* Saves a purchase after it is successful.
* @param $subTotal - integer
* @param $tax - integer
* @param $discount - integer
* @param $total - integer
*/
public function savePurchase($subTotal, $tax, $discount, $total) {
try {
$stmt = $this->db->prepare(
"INSERT INTO purchase
(purchase_customer_id,
purchase_timestamp,
purchase_ip,
purchase_subtotal,
purchase_tax,
purchase_discount,
purchase_total,
purchase_status)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute(array($_SESSION['customer_id'], NOW(), $_SERVER['REMOTE_ADDR'], $subTotal,
$tax, $discount, $total));
return $purchaseId;
} catch (Exception $e) {
return FALSE;
}
}
/**
* Saves each item from a purchase.
* Updates product quantities in product table.
* @param $purchaseId - integer
*/
public function saveCartItems($purchaseId) {
try {
foreach ($_SESSION[$this->_cartName][$id] as $productId => $quantity) {
$stmt = $this->db->prepare(
"INSERT INTO purchase_item
(purchase_item_product_id,
purchase_item_purchase_id,
purchase_item_quantity)
VALUES
(?, ?)");
$stmt->execute(array($productId, $purchaseId, $quantity));
$update = $this->db->prepare(
"UPDATE product
SET product_quantity = product_quantity - $quantity
WHERE product_id = ?");
$update->execute(array($productId));
}
} catch (Exception $e) {
return FALSE;
}
}
/**
* Calculates shipping cost and applies it to the shopping cart.
*/
public function shipping() {
// need to find out pricing structure
}
/**
* Applies a coupon discount if coupon parameters are met
* Checks for coupon code validity from the coupon table
* @param $couponCode - varchar
*/
public function useCoupon($couponCode, $cartPrice) {
try {
$stmt = $this->db->prepare(
"SELECT coupon_amount, coupon_minimum_price
FROM coupon
WHERE coupon_code = ? AND coupon_active = 'Yes'");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute(array($couponCode));
$stmt->fetch();
// make sure there is a coupon available that matches customer input
if (count($stmt) != 1) {
return FALSE;
}
// chcek if the current cart price (minus taxes and shipping) is greater than coupon minimum price
if ($cartPrice < $stmt['coupon_minimum_price']) {
return FALSE;
}
// this will go in the coupon field in the cart view
return $stmt['coupon_amount'];
} catch (Exception $e) {
return FALSE;
}
}
/**
* Allows for the customer to get a free product if purchasing three or more.
*/
public function buyThreeGetOneFree() {
// check that the cart has three or more items
// get the lowest item price
// display all items that are equal to or less than the lowest item price
}
}