我希望我不会问一个已经回答的问题,我已经在网站上广泛搜索了我的问题的答案,但无法找到解决方案。
我有以下问题。
我有一个购物车的表单,它应该应用优惠券,然后保存这些值,以便以后可以在不同的页面上使用。
但由于某些原因,这些会话变量未保存。
我的表单如下:
<?php
session_start();
$cart_custom = json_decode($_COOKIE['view_cart_custom_fab'],true);
//print_r($cart_custom);
include_once("inc/header.php");
//set_add_to_cart_cookie();
$fetch_user_info = mysqli_query($conn,"SELECT * FROM `st_customer` WHERE `email`= '{$_SESSION['USER_EMAIL']}'");
$fetch_info = mysqli_fetch_array($fetch_user_info);
$size_id = ($_REQUEST['id'] ? $_REQUEST['id'] : $_COOKIE['fabric_size_id']);
$fabric_id = $_COOKIE['fabric_id'];
$fabric_back = $_COOKIE['fabric_back'];
$shirts = $_COOKIE['shirts'];
$fabric_arms = $_COOKIE['fabric_arms'];
$fabric_pocket = $_COOKIE['fabric_pocket'];
$fabric_cuff = $_COOKIE['fabric_cuff'];
$fabric_boy_border = $_COOKIE['fabric_boy_border'];
$fabric = mysqli_query($conn,"SELECT * FROM `st-fabrics` where `id` = '$fabric_id'");
$fabric_record = mysqli_fetch_array($fabric);
$fabric_back_data = mysqli_query($conn,"SELECT * FROM `st-fabrics` where `id` = '$fabric_back'");
$fabric_back_record = mysqli_fetch_array($fabric_back_data);
$arms = mysqli_query($conn,"SELECT * FROM `st-fabrics` where `id` = '$fabric_arms'");
$arms_record = mysqli_fetch_array($arms);
$pocket = mysqli_query($conn,"SELECT * FROM `st-fabrics` where `id` = '$fabric_pocket'");
$pocket_record = mysqli_fetch_array($pocket);
$cuff = mysqli_query($conn,"SELECT * FROM `st-fabrics` where `id` = '$fabric_cuff'");
$cuff_record = mysqli_fetch_array($cuff);
$border = mysqli_query($conn,"SELECT * FROM `st-fabrics` where `id` = '$fabric_boy_border'");
$border_record = mysqli_fetch_array($border);
$select_size = mysqli_query($conn,"SELECT * FROM `st_sizes` where `size_id` = '$size_id'");
$user_select_size = mysqli_fetch_array($select_size);
$model_id = $user_select_size['size_model'];
$model = mysqli_query($conn,"SELECT * FROM `st_model` where `model_id` = '$model_id'");
$model_name = mysqli_fetch_array($model);
if($shirts=="half_shirt"){
$shirt = $user_select_size['half_sleeve'];
}
if($shirts=="full_tshirt"){
$shirt = $user_select_size['full_sleeve'];
}
$cm1 = $fabric_record['fabric_price']/10;
$cm2 = $arms_record['fabric_price']/10;
$cm3 = $fabric_back_record['fabric_price']/10;
$front_size = ($cm1*$user_select_size['front']*100);
$back_size = ($cm3*$user_select_size['size_back']*100);
$arms_size = ($cm2*$shirt*100);
$front_back_total = $user_select_size['front']+$user_select_size['size_back'];
$custom_total = ($front_size+$back_size+$arms_size);
if(isset($_POST['apply_coupon_btn'])){
$query_coupon = mysqli_query($conn,"SELECT * FROM `st_coupon` WHERE `coupon_code`='{$_POST['coupon_apply']}' AND `status`='1'");
$fetch_coupon = mysqli_fetch_array($query_coupon);
$message = "tmp2";
if(isset($fetch_coupon)){
$_SESSION['coupon_data'] = $fetch_coupon;
$check_user_id = mysqli_query($conn,"SELECT * FROM `st_order` as so JOIN `st_coupon` as sc on so.`coupon_id`=sc.`coupon_id` WHERE sc.`coupon_code`='{$_POST['coupon_apply']}' AND so.`user_id`={$_SESSION['user_id']}");
$message = "tmp3";
if(mysqli_num_rows($check_user_id)){
$message = "U hebt deze kortingscode reeds gebruikt";
}else{
if($fetch_coupon['coupon_type'] == "price"){
$_SESSION['price_coupon_price'] = $fetch_coupon['coupon_value'];
unset($_SESSION['price_coupon_percent']);
}elseif($fetch_coupon['coupon_type'] == "percent"){
$_SESSION['price_coupon_percent'] = $fetch_coupon['coupon_value'];
unset($_SESSION['price_coupon_price']);
}
}
}else{
$message = "Foute kortingscode";
}
}
if(isset($_POST['remove_coupon_btn'])){
unset($_SESSION['price_coupon_percent']);
unset($_SESSION['price_coupon_price']);
unset($_SESSION['coupon_data']);
unset($_SESSION['price_coupon_value']);
header("location: winkelwagen.php");
}
if(isset($_POST['update_cart']))
{
echo "update cart";
foreach($_POST['custom_qty_cart'] as $key=>$qtys){
$cart_custom[$key]['price']['qty'] = $qtys;
}
setcookie('view_cart_custom_fab', json_encode($cart_custom), time() + (86400 * 2), "/");
setcookie('custom_qty', $_POST['custom_qty'], time() + (86400 * 2), "/");
$custom_total = $custom_total*$_POST['custom_qty'];
setcookie('custom', $custom_total, time() + (86400 * 2), "/");
foreach($_POST["qty"] as $key => $qty) { //Print each item, quantity and price.
$id = $_POST["product_id"][$key];
$_SESSION["products"][$id]["fabric_qty"] = $qty;
}
header("location: winkelwagen.php");
}
if(isset($_POST['checkout_btn'])){
if($_SESSION['USER_EMAIL']){
header("location: afrekenen.php");
}else{
header("location: winkelwagen.php#login-modelbox");
}
}
?>
<div class="container">
<h4><?php echo $_SESSION['price_coupon_value']; ?></h4>
<h4><?php echo $_SESSION['coupon_data']; ?></h4>
<div class="main_content fabric-content margin_top">
<?php
?>
<?php if (!empty($_SESSION["products"]) || count($cart_custom) > 0) { ?>
<form method="POST" action="winkelwagen.php" style="width:60%; float:left; position:relative; left: 50%;">
<table class="margin_top" style="float: left; position: relative; left: -50%;">
<tr>
<th data-field="id" colspan="1" style="width: 15%;"></th>
<th data-field="id" colspan="1" style="width: 35%;">Naam</th>
<th data-field="id" colspan="1" style="width: 35%;">Aantal</th>
<th data-field="id" colspan="1" style="width: 15%;">Prijs</th>
</tr>
<?php if(count($cart_custom) > 0){ ?>
<?php
$total_price = 0;
foreach($cart_custom as $key=>$val){
$cookie_model = $cart_custom[$key]['model'];
$cookie_price = $cart_custom[$key]['price'];
$model_name = $cookie_model['model'];
if($model_name == "wranglan_model"){
$model_img = "image/t-shirt.png";
}elseif($model_name == "round_model"){
$model_img = "image/t-shirt.png";
}
$model_qty = str_replace(",",".",$cookie_price['qty']);
$model_price = $model_qty * $cookie_price['ttl_price'];
$total_price = $model_qty * $cookie_price['ttl_price'] + $total_price;
if(isset($_GET['remove_id'])){
unset($cart_custom[$_GET['remove_id']]);
setcookie('view_cart_custom_fab', json_encode($cart_custom), time() + (86400 * 2), "/");
header("location: winkelwagen.php");
}
?>
<tr>
<td colspan='1' style="width: 15%;">
<img src='<?php echo $model_img; ?>' alt='' class='img_table responsive_img' />
</td>
<td colspan='1' style="width: 35%;">
T-shirt op smaak
</td>
<td colspan='1' style="width: 35%;">
<input id='update_cart' name='update_cart' type='hidden' value='' />
<input class="qty" type="text" name="custom_qty_cart[<?php echo $key; ?>]" value="<?php echo $model_qty; ?>" style="width: 20%; text-align:right;"/>
<input name='Update' style="width: 18px; vertical-align:bottom" type='image' value='update_cart' src='/image/update.jpg' alt='Update' onclick = "return setHidden('update_cart');" />
<a href="?remove_id=<?php echo $key;?>" style="width: 18px; vertical-align:bottom"><img src="/image/recycle.jpg" style="width: 18px; vertical-align:bottom"></a>
</td>
<td colspan='1' style="width: 15%;">
<?php echo price_format($model_price); ?>
</td>
</tr>
<?php
}
?>
<?php } ?>
<?php if(!empty($_SESSION["products"])){?>
<?php
if (isset($_SESSION["products"]) && count($_SESSION["products"]) > 0) {
$total = 0;
$list_tax = '';
foreach ($_SESSION["products"] as $product) { //Print each item, quantity and price.
//print_r($_SESSION["products"]);
$product_name = $product["fabric_name"];
$product_id = $product["id"];
//$product_qty = ($product["fabric_qty"] > 1 ? $product["fabric_qty"]:1);
$product_qty = str_replace(",",".",$product["fabric_qty"]);
//$product_price = ($product_qty>1 ? $product["fabric_price"] * $product_qty : $product["fabric_price"]);
$product_price = ($product["fabric_price"] * $product_qty);
$product_code = $product["product_code"];
$product_color = $product["product_color"];
$product_size = $product["product_size"];
$item_price = $product_price;
$total_cost_price = $total_cost_price + $item_price;
//$item_price = sprintf("%01.2f",($product_price * $product_qty)); // price x qty = total item price
if(isset($_GET['removeid'])){
unset($_SESSION["products"][$_GET['removeid']]);
header("location: winkelwagen.php");
}
$cart_box .=
"<tr><td colspan='1' style='width: 15%'><img src='admin/images/fabric/" . $product['fabric_texture'] . "' alt='' class='img_table responsive_img' /></td>
<td colspan='1' style='width: 35%'>$product_name</td>
<td colspan='1' style='width: 35%'>
<input id='update_cart' name='update_cart' type='hidden' value='' />
<input type='text' name='qty[]' class='qty' min='1' max='100' value='".$product_qty."' style='width: 20%; text-align:right;'/>
<input name='Update' style='width: 18px; vertical-align:bottom' type='image' value='update_cart' src='/image/update.jpg' alt='Update' onclick = 'return setHidden('update_cart');' />
<a href='?removeid=$product_id' style='width: 18px; vertical-align:bottom'><img src='/image/recycle.jpg' style='width: 18px; vertical-align:bottom'></a>
<input type='hidden' name='product_id[]' value='".$product_id."' /></td>
<td colspan='1' style='width: 15%'>".price_format($item_price)."</td>";
$subtotal = ($product_price * $product_qty); //Multiply item quantity * price
$total = ($total + $subtotal); //Add up to total price
}
$grand_total = $total + $shipping_cost; //grand total
foreach ($taxes as $key => $value) { //list and calculate all taxes in array
$tax_amount = round($total * ($value / 100));
$tax_item[$key] = $tax_amount;
$grand_total = $grand_total + $tax_amount;
}
foreach ($tax_item as $key => $value) { //taxes List
$list_tax .= $key . ' ' . $currency . sprintf("%01.2f", $value) . '<br />';
}
$shipping_cost = ($shipping_cost) ? 'Shipping Cost : ' . $currency . sprintf("%01.2f", $shipping_cost) . '<br />' : '';
echo $cart_box;
}
?>
<?php } ?>
<?php
$fetch_shipping_price = mysqli_query($conn,"SELECT * FROM `st_custom_price` WHERE `meta_key`='shipping'");
$fetch_price = mysqli_fetch_array($fetch_shipping_price);
$shipping_price = $fetch_price['meta_price'];
$product_total = $total_price + $total_cost_price + ($price_detail['ttl_price']*$price_detail['qty']);
if($_SESSION['price_coupon_price']){
$total_amount = $total_price + $total_cost_price + ($price_detail['ttl_price']*$price_detail['qty']) - $_SESSION['price_coupon_price'];
$_SESSION['price_coupon_value'] = $_SESSION['price_coupon_price'];
}elseif($_SESSION['price_coupon_percent']){
$total_amount = $total_price + $total_cost_price + ($price_detail['ttl_price']*$price_detail['qty']);
$percent_total = ($_SESSION['price_coupon_percent'] / 100) * $total_amount;
$_SESSION['price_coupon_value'] = $percent_total;
$total_amount = $total_amount - $percent_total;
}else{
$total_amount = $total_price + $total_cost_price + ($price_detail['ttl_price']*$price_detail['qty']);
}
$ttl_ship = $total_amount + $shipping_price;
setcookie('total_cost', $ttl_ship, time() + (86400 * 2), "/");
setcookie('total_cost', $ttl_ship, time() + (86400 * 2), "/");
?>
<?php if(!empty($total_amount) || count($cart_custom) > 0){ ?>
<tr>
<th data-field="id" colspan="3" style="width: 85%;">Totaal</th>
<th data-field="id" colspan="1" style="width: 15%;"> <?php echo price_format($product_total); ?></th>
</tr>
<?php if($_SESSION['price_coupon_price'] || $_SESSION['price_coupon_percent']){
$discount_type = ($_SESSION['coupon_data']['coupon_type'] == "price" ? " €" : " percent ");
?>
<tr>
<th data-field="id" colspan="3" style="width: 85%;">
<input id='remove_coupon_btn' name='remove_coupon_btn' type='hidden' value='' />
Kortingscode <?php echo $_SESSION['coupon_data']['coupon_name']; ?><input name='remove_coupon_btn' style="width: 18px; vertical-align:bottom" type='image' value='remove_coupon_btn' src='/image/recycle.jpg' alt='Remove coupon' onclick = "return setHidden('remove_coupon_btn');" />
</th>
<th style="width: 15%;"><?php echo price_format($total_amount-$product_total); ?></th>
</tr>
<?php } ?>
<tr>
<th data-field="id" colspan="3" style="width: 85%;">Verzendingskosten</th>
<th data-field="id" colspan="1" style="width: 15%;"> <?php echo price_format($shipping_price); ?></th>
</tr>
<tr>
<th data-field="id" colspan="3" style="width: 85%;">Te betalen</th>
<th data-field="id" colspan="1" style="width: 15%;"> <?php echo price_format($ttl_ship); ?></th>
</tr>
</table>
<?php echo $message; ?>
<div class="more-products-pagination" style="float: left; position: relative; left: -50%;">
<div class="coupon_row" style="margin:5px 20px">
<?php if($_SESSION['price_coupon_price'] || $_SESSION['price_coupon_percent']){ ?>
<?php } else { ?>
<ul>
<li>Kortingscode</li>
<li><input type="text" name="coupon_apply" value=""><input type="submit" name="apply_coupon_btn" value="Toepassen" class="add_to_cart_btn" style="border: 1px solid #fff; display: inline-block;float: right;"></li>
</ul>
<?php } ?>
</div>
<div class="pagination_btn pagination_btn_right">
<input type="submit" class="add_to_cart_btn" value="Afrekenen" name="checkout_btn">
</div>
</div>
<?php } ?>
</form>
<?php }else {
echo "Geen producten in uw winkelwagen.";
}?>
</div>
</div>
<script language="javascript">
function setHidden(whichButton)
{
var element = document.getElementById(whichButton);
element.value = whichButton;
return true;
}
</script>
<?php
include_once("inc/footer.php");
?>
<style>
table {
border-collapse: collapse;
width: 100%;
background: rgba(253, 245, 208,0.6);
box-shadow: 0px 0px 2px #C1C1C1;
}
th, td {
text-align: left;
padding: 8px;
color:#444;
}
td{
}
th {
background-color: #BD3F6F;
color: white;
font-weight: 400;
font-size: 16px;
}
</style>
当我尝试在另一个页面上访问变量$ _SESSION [&#39; coupon_data&#39;]时,这不起作用,它是空的。
我在这段代码中设置变量:
if(isset($_POST['apply_coupon_btn'])){
$query_coupon = mysqli_query($conn,"SELECT * FROM `st_coupon` WHERE `coupon_code`='{$_POST['coupon_apply']}' AND `status`='1'");
$fetch_coupon = mysqli_fetch_array($query_coupon);
$message = "tmp2";
if(isset($fetch_coupon)){
$_SESSION['coupon_data'] = $fetch_coupon;
..
}
然后我在这里打电话给下一页:
if(isset($_POST['checkout_btn'])){
if($_SESSION['USER_EMAIL']){
header("location: afrekenen.php");
}else{
header("location: winkelwagen.php#login-modelbox");
}
}
即使我在同一页面中调用购物车项目的更新,会话变量由于某些我不理解的原因而消失:
if(isset($_POST['update_cart']))
{
echo "update cart";
foreach($_POST['custom_qty_cart'] as $key=>$qtys){
$cart_custom[$key]['price']['qty'] = $qtys;
}
setcookie('view_cart_custom_fab', json_encode($cart_custom), time() + (86400 * 2), "/");
setcookie('custom_qty', $_POST['custom_qty'], time() + (86400 * 2), "/");
$custom_total = $custom_total*$_POST['custom_qty'];
setcookie('custom', $custom_total, time() + (86400 * 2), "/");
foreach($_POST["qty"] as $key => $qty) { //Print each item, quantity and price.
$id = $_POST["product_id"][$key];
$_SESSION["products"][$id]["fabric_qty"] = $qty;
}
header("location: winkelwagen.php");
}
我完全感到困惑,希望有人可以帮助我。
答案 0 :(得分:0)
问题解决了,结果是在其中一个包含中创建了一个单独的会话,因此没有允许正确的全球化,对session_start进行了另一次调用。