Example #1
0
 public function removeFromCart()
 {
     $cart = \Lib\Session::get('cart');
     $db = Register::get('db');
     $product_id = $this->http->get('product_id');
     $product_quantity = $this->http->get('product_quantity');
     if ($product_quantity < 1) {
         Location::To(URL . 'cart/show');
     }
     $product = new Product($product_id);
     if (!empty($cart)) {
         foreach ($cart as $p) {
             if ($p['product_id'] == $product_id) {
                 $orders = new Order($p['order_id']);
                 $quantity = $orders->getQuantity();
                 if ($quantity >= $product_quantity) {
                     $orders->setQuantity($quantity - $product_quantity);
                     $p_q = $product->getQuantity();
                     $product->setQuantity($p_q + $product_quantity);
                     $product->changeProductSold(-$product_quantity);
                     $db->query('START TRANSACTION');
                     if ($product->writeData(true) && $orders->writeData(true)) {
                         $db->query('COMMIT');
                     } else {
                         $db->query('ROLLBACK');
                     }
                     if ($orders->getQuantity() == 0) {
                         $orders->delete();
                     }
                 }
             }
         }
     }
     $this->ActNumberOfProducts();
     Location::To(URL . 'cart/show');
 }