Example #1
0
 /**
  * This function is used for merging the cart data with session
  * 
  * @return string
  */
 function mergeSessionWithCartDatabase()
 {
     if (isset($_SESSION['mycart'])) {
         $product_array = $_SESSION['mycart'];
         $default = new Core_CAddCart();
         $cartid = $default->getCartIdOfUser();
         if ($cartid == 0 || $cartid == '') {
             $sql = "insert into shopping_cart_table (user_id , cart_date) values ('" . $_SESSION['user_id'] . "','" . date('Y-m-d') . "')";
             $query = new Bin_Query();
             $query->updateQuery($sql);
         }
         $cartid = $default->getCartIdOfUser();
         $cartchecksql = "SELECT product_id FROM shopping_cart_products_table WHERE cart_id =" . $cartid;
         $cartchechqry = new Bin_Query();
         if ($cartchechqry->executeQuery($cartchecksql)) {
             foreach ($cartchechqry->records as $records) {
                 $cartproducts[] = $records['product_id'];
             }
         } else {
             $cartproducts = array();
         }
         foreach ($_SESSION['mycart'] as $key => $val) {
             $sql = 'select soh from product_inventory_table where product_id=' . $val['product_id'];
             $query = new Bin_Query();
             $query->executeQuery($sql);
             $soh = $query->records[0]['soh'];
             if ($val['qty'] <= $soh) {
                 $sql_shipping = 'select shipping_cost from products_table where product_id=' . $val['product_id'];
                 $query_shipping = new Bin_Query();
                 $query_shipping->executeQuery($sql_shipping);
                 $shipmentcost = $query_shipping->records[0]['shipping_cost'];
                 $shippingcost = $val['qty'][$i] * $shipmentcost;
                 $date = date('Y-m-d');
                 $default_cls = new Core_CAddCart();
                 $msrp = $default_cls->getMsrpByQuantity($val['product_id'], $val['qty']);
                 if ($msrp == '' || $msrp == 0) {
                     $msrp_sql = "SELECT msrp FROM products_table WHERE product_id=" . $val['product_id'];
                     $msrp_query = new Bin_Query();
                     $msrp_query->executeQuery($msrp_sql);
                     $msrp = $msrp_query->records[0][msrp];
                 }
                 $defobjj = new Core_CAddCart();
                 $groupdiscount = $defobjj->getUserGroupDiscount();
                 $msrp = $msrp - $msrp * ($groupdiscount / 100);
                 if (in_array($val['product_id'], $cartproducts)) {
                     $sql = "UPDATE shopping_cart_products_table SET product_qty=" . $val['qty'] . ",product_unit_price=" . $msrp . " where product_id=" . $val['product_id'] . " and cart_id=" . $cartid;
                 } else {
                     $sql = "INSERT INTO shopping_cart_products_table (cart_id,product_id,product_qty,date_added,product_unit_price,shipping_cost) VALUES (" . $cartid . "," . $val['product_id'] . "," . $val['qty'] . ",'" . $date . "'," . $msrp . "," . $shippingcost . ")";
                 }
                 $query = new Bin_Query();
                 $query->executeQuery($sql);
             }
         }
         $_SESSION['mycart'] = '';
         unset($_SESSION['mycart']);
     }
 }