public function addToCart() { $product_id = $this->http->get('product_id'); $product_name = $this->http->get('product_name'); $product_quantity = $this->http->get('product_quantity'); $product_price = $this->http->get('product_price'); $product = new Product($product_id); if (!empty($product)) { $db = Register::get('db'); $quantity = $product->getQuantity(); if ($quantity > 0 && $quantity >= $product_quantity && $product_quantity >= 0) { $product->setQuantity($quantity - $product_quantity); $product->changeProductSold($product_quantity); $cart = \Lib\Session::get('cart'); if (!empty($cart)) { foreach ($cart as $p) { if ($p['product_id'] == $product_id) { $order = new Order($p['order_id']); if ($order->getQuantity()) { $quantity = $order->getQuantity(); $order->setQuantity($quantity + $product_quantity); $db->query('START TRANSACTION'); if ($product->writeData(true) && $order->writeData(true)) { $order_id = $db->getLastInsertedId(); $db->query('COMMIT'); \Lib\Session::set('cart', array('order_id' => $order_id, 'product_id' => $product_id), 2); } else { $db->query('ROLLBACK'); } $this->ActNumberOfProducts(); Location::To(URL . 'cart/show'); } } } } $order = new Order(); $order->setDate(); $order->setActive(false); $order->setProductId($product_id); $order->setProductName($product_name); $order->setQuantity($product_quantity); $order->setPrice($product->getPrice()); $order->setStatus(1); $db->query('START TRANSACTION'); if ($product->writeData(true) && $order->writeData()) { $order_id = $db->getLastInsertedId(); $db->query('COMMIT'); \Lib\Session::set('cart', array('order_id' => $order_id, 'product_id' => $product_id), 2); } else { $db->query('ROLLBACK'); } $this->ActNumberOfProducts(); \Lib\Location::To(URL . 'cart/show'); } else { \Lib\Location::To(URL . 'cart/show'); } } }