Есть несколько способов сделать это. Это один:
Шаг 1: Добавьте скрытый ввод, который включает URL-адрес текущей страницы
<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>
<input type="hidden" name="url" value=<?=$url;?>"
Шаг 2: Затем в вашем cart. php, вам нужно добавить перенаправление после добавления продукта, например
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
//code for adding product in cart
case "add":
if(!empty($_POST["quantity"])) {
$pid=$_GET["pid"];
$result=mysqli_query($con,"SELECT * FROM tblproduct WHERE id='$pid'");
while($productByCode=mysqli_fetch_array($result)){
$itemArray = array($productByCode["code"]=>array('name'=>$productByCode["name"], 'code'=>$productByCode["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode["price"], 'image'=>$productByCode["image"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode["code"],array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode["code"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
}
header("Location: ".$_POST['url']);
break;
}
}