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

34 lines
949 B
PHP

<?php
class RemoveInCart extends Controller{
public function __construct(){
$this->processJSON();
}
private function processJSON(){
$json = file_get_contents('php://input');
$obj = json_decode($json, true);
$returnJSON = new stdClass();
if(isset($obj["action"]) && $obj["action"] == "remove"){
unset($_SESSION["products"]);
unset($_SESSION["store"]);
$returnJSON->statue = true;
$returnJSON->messsage = "cart cleard";
}elseif(isset($_SESSION["products"][$obj["productId"]])){
unset($_SESSION["products"][$obj["productId"]]);
if($_SESSION["products"] == null){
unset($_SESSION["store"]);
}
$returnJSON->statue = true;
$returnJSON->itemCount = count($_SESSION['products']);
$returnJSON->messsage = "Item removed";
}else{
$returnJSON->statue = false;
$returnJSON->messsage = "Error";
}
echo json_encode($returnJSON);
}
}
?>