function delete_cart_detail($cartDetail) { if (!(isset($cartDetail['user_id']) && isset($cartDetail['line_id']))) { throw new Exception('Must provide user_id and line_id'); } $line_id = $cartDetail['line_id']; $user_id = $cartDetail['user_id']; $dbh = new PDOConnection(); if (!check_cart_exists($dbh, $user_id)) { throw new Exception("Cannot find cart for user"); } //remove all cart_details for current user $query = "DELETE FROM cart_details WHERE user_id = :user_id AND line_id = :line_id"; $sth = $dbh->prepare($query); $sth->bindParam(':user_id', $user_id, PDO::PARAM_INT); $sth->bindParam(':line_id', $line_id, PDO::PARAM_INT); if (!$sth->execute()) { throw new Exception($sth->errorInfo()[2]); } //update line id for the rest of the cart $query = "UPDATE cart_details SET line_id = (line_id - 1) WHERE user_id = :user_id AND line_id > :line_id"; $sth = $dbh->prepare($query); $sth->bindParam(':user_id', $user_id, PDO::PARAM_INT); $sth->bindParam(':line_id', $line_id, PDO::PARAM_INT); if (!$sth->execute()) { throw new Exception($sth->errorInfo()[2]); } return true; }
function add_cart_detail($cartDetail) { if (!(isset($cartDetail['user_id']) && isset($cartDetail['lines']))) { throw new Exception('Must provide user_id and lines'); } if (empty($cartDetail['lines'])) { throw new Exception("'lines' is empty"); } $details = $cartDetail['lines']; $user_id = $cartDetail['user_id']; $dbh = new PDOConnection(); if (!check_cart_exists($dbh, $user_id)) { throw new Exception("Cannot find cart for user"); } //remove all cart_details for current user $query = "DELETE FROM cart_details WHERE user_id = :user_id"; $sth = $dbh->prepare($query); $sth->bindParam(':user_id', $user_id, PDO::PARAM_INT); if (!$sth->execute()) { throw new Exception($sth->errorInfo()[2]); } //set up parameters to bind $product_id = -1; $unit_id = -1; $price = -1; $quantity = -1; $line_id = 0; $query = "INSERT INTO cart_details(user_id, product_id, unit_id, price, quantity, line_id)\n VALUES(:user_id, :product_id, :unit_id, :price, :quantity, :line_id)"; $sth = $dbh->prepare($query); $sth->bindParam(':user_id', $user_id); $sth->bindParam(':product_id', $product_id); $sth->bindParam(':price', $price); $sth->bindParam(':quantity', $quantity); $sth->bindParam(':unit_id', $unit_id); $sth->bindParam(':line_id', $line_id); foreach ($details as $detail) { $detail = (array) $detail; if (!(isset($detail['product_id']) && isset($detail['unit_id']) && isset($detail['price']) && isset($detail['quantity']))) { throw new Exception("Must provide product_id, unit_id, price, quantity"); } $product_id = $detail['product_id']; $unit_id = $detail['unit_id']; $price = $detail['price']; $quantity = $detail['quantity']; $line_id = isset($detail['line_id']) ? $detail['line_id'] : $line_id; if (!$sth->execute()) { throw new Exception($sth->errorInfo()[2]); } ++$line_id; } return true; }
function update_cart_header($cartInfo) { return 'not implemented'; if (!isset($cartInfo['user_id'])) { throw new Exception("ERROR: user_id required"); } $dbh = new PDOConnection(); if (!check_cart_exists($dbh, $cartInfo['user_id'])) { throw new Exception("Could not find cart for user: "******"UPDATE cart_header SET "; $optionalQuery = ''; if (isset($cartInfo['delivery_date'])) { $optionalQuery .= 'delivery_date = :delivery_date '; } if (isset($cartInfo['delivery_method'])) { $optionalQuery .= 'delivery_method = :delivery_method '; } if (isset($cartInfo['comments'])) { $optionalQuery .= 'comments = :comments '; } if (isset($cartInfo['shipping_comments'])) { $optionalQuery .= 'shipping_comments = :shipping_comments '; } if ($optionalQuery === '') { throw new Exception('Nothing to update'); } $query .= $optionalQuery . " WHERE user_id = :user_id"; $sth = $dbh->prepare($query); $sth->bindParam(':user_id', $cartInfo['user_id'], PDO::PARAM_INT); if (isset($cartInfo['delivery_date'])) { $sth->bindParam(':delivery_date', $cartInfo['delivery_date']); } if (isset($cartInfo['delivery_method'])) { $sth->bindParam(':delivery_method', $cartInfo['delivery_method']); } if (isset($cartInfo['comments'])) { $sth->bindParam(':comments', $cartInfo['comments']); } if (isset($cartInfo['shipping_comments'])) { $sth->bindParam(':shipping_comments', $cartInfo['shipping_comments']); } if (!$sth->exeicute()) { throw new Exception($sth->errorInfo()[2]); } if (isset($cartInfo['lines'])) { //update cart lines } }