Files
004_comission/_resources/it114105/itp4513/Assignment/19-20/controllers/AddToCart.php
louiscklaw 6c60a73f30 update,
2025-01-31 19:15:17 +08:00

44 lines
1.3 KiB
PHP

<?php
class AddToCart extends Controller{
public function __construct(){
$this->processJSON();
}
private function processJSON(){
$json = file_get_contents('php://input');
$obj = json_decode($json, true);
if(isset($_SESSION['store']) && $_SESSION['store'] != $obj['storeId']){
$returnJSON = new stdClass();
$returnJSON->statue = false;
$returnJSON->message = "Store Changed";
echo json_encode($returnJSON);
}else{
$checkDup = false;
if(isset($_SESSION['store']) && isset($_SESSION['products'])){
foreach ($_SESSION['products'] as $key => $value) {
if($key == $obj["productId"]){
$_SESSION['products'][$key] += $obj["qty"];
$checkDup = true;
}
}
}
if(!$checkDup){
$_SESSION['store'] = $obj['storeId'];
if(isset($_SESSION['products'])){
$_SESSION['products'] += array($obj['productId']=> $obj['qty']);
}else{
$_SESSION['products'] = array($obj['productId']=> $obj['qty']);
}
}
$returnJSON = new stdClass();
$returnJSON ->statue = true;
$returnJSON->itemCount = count($_SESSION['products']);
$returnJSON->messsage = "Item saved";
echo json_encode($returnJSON);
}
}
}
?>