function add_to_cart($app, $db, $params) { if (isset($params['id'], $params['attributes'])) { $pid = filter_var($params['id'], FILTER_VALIDATE_INT, array('min_range' => 1)) ? $params['id'] : NULL; if (empty($params['attributes']) || $params['attributes'] === '0') { // product don't have attributes $attributes = NULL; } elseif ($params['attributes'] === '1') { // product needs attributes $app->flash('info', 'You need to choose the attributes of your product before adding it to your Cart.'); $category = \Data\ProductsRepository::get_category($db, $pid); $app->redirect($app->view()->url('/products/' . $category . '/' . $pid)); } else { // actual attributes string $attributes = $params['attributes']; } } if (isset($pid, $params['action']) && $params['action'] == 'add') { $stmt = \Data\CartRepository::add_to_cart($db, $_SESSION['user_id'], $pid, 1, $attributes); $app->flash('info', 'Your cart have been updated. A new product have been added.'); $app->redirect($app->view()->url('/shop/cart')); } elseif (isset($pid, $params['action']) && $params['action'] == 'remove') { $stmt = \Data\CartRepository::remove_from_cart($db, $_SESSION['user_id'], $pid, $attributes); $app->flash('info', 'Your cart have been updated. The selected product have been removed.'); $app->redirect($app->view()->url('/shop/cart')); } elseif (isset($pid, $params['action'], $params['qty']) && $params['action'] == 'move') { $qty = filter_var($params['qty'], FILTER_VALIDATE_INT, array('min_range' => 1)) ? $params['qty'] : 1; $stmt = \Data\CartRepository::remove_from_cart($db, $_SESSION['user_id'], $pid, $attributes); $stmt = \Data\WishListRepository::add_to_wish_list($db, $_SESSION['user_id'], $pid, $qty, $attributes); $app->flash('info', 'Your cart have been updated. The product selected have been moved to your Wish List.'); $app->redirect($app->view()->url('/shop/cart')); } elseif (isset($params['action']) && $params['action'] == 'clear') { $stmt = \Data\CartRepository::clear_cart($db, $_SESSION['user_id']); $app->flash('info', 'Your cart have been updated. Your cart is now empty.'); $app->redirect($app->view()->url('/shop/cart')); } else { // show cart $app->view()->set_template('layouts/basic.php'); $app->render('shop/cart.php', array('page_title' => 'Your Cart')); $app->stop(); } }
public static function update_cart($db, $uid, $pid, $qty, $attributes) { if ($qty > 0) { if ($attributes == NULL) { $query = 'UPDATE carts SET quantity=:qty, date_modified=NOW() WHERE user_id=:uid AND product_id=:pid AND attributes IS NULL;'; } else { $query = 'UPDATE carts SET quantity=:qty, date_modified=NOW() WHERE user_id=:uid AND product_id=:pid AND attributes=:attributes;'; } $stmt = $db->prepare($query); if ($attributes == NULL) { $stmt->execute(array('uid' => $uid, 'pid' => $pid, 'qty' => $qty)); } else { $stmt->execute(array('uid' => $uid, 'pid' => $pid, 'qty' => $qty, 'attributes' => $attributes)); } return $stmt; } elseif ($qty == 0) { return \Data\CartRepository::remove_from_cart($db, $uid, $pid, $attributes); } }