$_SESSION['add-order'][$prefix . 'add-cart-qty'][] = $ajx_qty; } else { /* --- EXISTED --- */ if (is_numeric($key) && is_numeric($check_stock)) { $_SESSION['add-order'][$prefix . 'add-cart-type-id'][$key] = $ajx_type; $_SESSION['add-order'][$prefix . 'add-cart-stock-id'][$check_stock] = $ajx_stock; $_SESSION['add-order'][$prefix . 'add-cart-qty'][$check_stock] = $_SESSION['add-order'][$prefix . 'add-cart-qty'][$check_stock] + $ajx_qty; } else { $_SESSION['add-order'][$prefix . 'add-cart-type-id'][] = $ajx_type; $_SESSION['add-order'][$prefix . 'add-cart-stock-id'][] = $ajx_stock; $_SESSION['add-order'][$prefix . 'add-cart-qty'][] = $ajx_qty; } } $temp_stock = $_ajax->get_stock($ajx_type, $ajx_stock); $temp_stock = $temp_stock->stock_quantity - $ajx_qty; $_ajax->update_temp_stock($temp_stock, $ajx_type, $ajx_stock); $total_qty = 0; $sess_qty = $_SESSION['add-order'][$prefix . 'add-cart-qty']; foreach ($sess_qty as $sess_qty) { $total_qty += $sess_qty; } $total_cart = $total_qty; echo $total_cart; } /* --- DEFINED VARIABLE --- */ $_type_id = $_SESSION['add-order'][$prefix . 'add-cart-type-id']; $_stock_id = $_SESSION['add-order'][$prefix . 'add-cart-stock-id']; $_qty = $_SESSION['add-order'][$prefix . 'add-cart-qty']; $sub_total = 0; $sub_discount = 0; ?>
$curr_total = $_SESSION['bag']['total']; $curr_discount = $_SESSION['bag']['discount']; $curr_item = $curr_prod->type_price * $_SESSION['cart_qty'][$ajx_key]; $curr_item_disc = $promo_value * $_SESSION['cart_qty'][$ajx_key]; $new_temp_total = $curr_total - $curr_item; $new_temp_total_disc = $curr_discount - $curr_item_disc; $_SESSION['bag']['total'] = $new_temp_total; $_SESSION['bag']['discount'] = $new_temp_total_disc; $_ajax->update_temp_stock($temp_stock, $ajx_stock); $_ajax->update_temp_stock($cart_qty, $ajx_stock); $_SESSION['cart_qty'][$ajx_key] = $ajx_qty; $temp_qty = 0; $sess_qty = $_SESSION['cart_qty']; foreach($sess_qty as $qty){ $temp_qty += $qty; } $_SESSION['bag']['total'] = ($ajx_qty * $curr_prod->type_price) + $_SESSION['bag']['total']; $_SESSION['bag']['discount'] = ($ajx_qty * $promo_value) + $_SESSION['bag']['discount'];
} /* --- KEY --- */ $ajx_key = $_POST['key']; /* --- CALL FUNCTION --- */ $prefix = cleanurl($_global_general->website_title); $margin_stock = $_SESSION['add-order'][$prefix . 'add-cart-qty'][$ajx_key] - $ajx_qty; $product = $_ajax->get_product($ajx_type); $temp_stock = $_ajax->get_stock($ajx_type, $ajx_stock); $normal_stock = $temp_stock->stock_quantity + $_SESSION['add-order'][$prefix . 'add-cart-qty'][$ajx_key]; $dump_stock = $temp_stock->stock_quantity + $_SESSION['add-order'][$prefix . 'add-cart-qty'][$ajx_key] - $ajx_qty; if ($dump_stock < 0) { echo '<div class="alert alert-danger">'; echo 'Remaining stock: ' . $temp_stock->stock_quantity . 'pcs(s)'; echo '</div>'; } else { $_ajax->update_temp_stock($normal_stock, $ajx_type, $ajx_stock); /* --- DEFINE VARIABLE --- */ if (isset($_SESSION['add-order'][$prefix . 'add-cart-type-id'])) { $key = array_search($ajx_type, $_SESSION['add-order'][$prefix . 'add-cart-type-id']); $check_stock = array_search($ajx_stock, $_SESSION['add-order'][$prefix . 'add-cart-stock-id']); } /* --- CONTROL --- */ if (!isset($_SESSION['add-order'][$prefix . 'add-cart-type-id'])) { $_SESSION['add-order'][$prefix . 'add-cart-type-id'][] = $ajx_type; $_SESSION['add-order'][$prefix . 'add-cart-stock-id'][] = $ajx_stock; $_SESSION['add-order'][$prefix . 'add-cart-qty'][] = $ajx_qty; } else { /* --- EXISTED --- */ if (is_numeric($key) && is_numeric($check_stock)) { $_SESSION['add-order'][$prefix . 'add-cart-type-id'][$key] = $ajx_type; $_SESSION['add-order'][$prefix . 'add-cart-stock-id'][$check_stock] = $ajx_stock;
$curr_prod->type_price = $curr_prod->type_price - $promo_value; } else { $promo_value = 0; $curr_prod->type_price = $curr_prod->type_price; } $temp_stock = $curr_stock->stock_quantity + $_SESSION['cart_qty'][$ajx_key]; if ($ajx_qty <= $temp_stock) { $cart_qty = $temp_stock - $ajx_qty; $curr_total = $_SESSION['bag']['total']; $curr_discount = $_SESSION['bag']['discount']; $curr_item = $curr_prod->type_price * $_SESSION['cart_qty'][$ajx_key]; $curr_item_disc = $promo_value * $_SESSION['cart_qty'][$ajx_key]; $new_temp_total = $curr_total - $curr_item; $new_temp_total_disc = $curr_discount - $curr_item_disc; $_SESSION['bag']['total'] = $new_temp_total; $_SESSION['bag']['discount'] = $new_temp_total_disc; $_ajax->update_temp_stock($cart_qty, $ajx_type, $ajx_stock); $_SESSION['cart_qty'][$ajx_key] = $ajx_qty; $temp_qty = 0; $sess_qty = $_SESSION['cart_qty']; foreach ($sess_qty as $qty) { $temp_qty += $qty; } $_SESSION['bag']['total'] = $ajx_qty * $curr_prod->type_price + $_SESSION['bag']['total']; $_SESSION['bag']['discount'] = $ajx_qty * $promo_value + $_SESSION['bag']['discount']; $return = $temp_qty; } else { $return = "Currently quantity available is " . $curr_stock->stock_quantity . ", you tried to add " . $ajx_qty . "."; } echo $return; }