我使用Yii2框架。我将cookie用于购物车,当有人打开网站并在购物车中添加产品时,我使用cookie中的uniqueID将其添加到表中。问题是,当我第一次在浏览器中打开网站时,$ _ COOKIE不会在功能中显示,但是会生成它。当我刷新页面时,一切正常... 我在这里生成Cookie
if(isset($_COOKIE['uniqueID'])){
$curr_user = $_COOKIE['uniqueID'];
}else{
$expire = time() + 3600;// valid one year
setcookie('uniqueID', uniqid(), $expire);
$curr_user = $_COOKIE['uniqueID'];
}
在这里我将产品添加到购物车:
<button class="addToCart" type="button" data-toggle="tooltip" title="<?=Yii::$app->OutData->getLabel(56)?>" onclick="buyProductWithQty(<?=$promoProduct->id?>,1,'<?php echo $curr_user?>')"><i class="fa fa-shopping-cart"></i> <span class=""><?=Yii::$app->OutData->getLabel(55)?></span></button>
这是一个功能:
function buyProductWithQty(product_id, quantity, user) {
$.ajax({
url: '/admin/site/buyproductwithqty',
method: "POST",
data: {
product_id: product_id,
qty: quantity,
user:user
},
success: function (data) {
var data = JSON.parse(data);
if (data['reload'] == 1) {
addProductNotice2(data['msg'], 'success');
setTimeout(function(){ location.reload(); }, 2000);
} else {
//alert(data['msg']);
addProductNotice2(data['msg'], 'danger');
//$.notify(data['msg'], {globalPosition: 'bottom center'});
}
}
});
}
这是控制器中的功能:
public function actionBuyproductwithqty() {
if (isset($_POST['product_id']) and ! empty($_POST['product_id']) and isset($_POST['qty']) and ! empty($_POST['qty'])) {
$product_id = (int) $_POST['product_id'];
$quantity = (int) $_POST['qty'];
$flag = 0;
$productKey = 0;
$result['reload'] = 1;
$result['msg'] = 'Успешно добавен продукт в количката!';
$product = Product::findOne($product_id);
if ($product) {
$productPrice = $product->getPrice();
$session = Yii::$app->session;
if(Yii::$app->user->identity){
$add = new ProductCart();
$add->product_id = (int)$_POST['product_id'];
$add->user_id = (int)Yii::$app->user->identity->id;
$add->qty = (int)$_POST['qty'];
$add->save(false);
}else{
$add = new ProductCart();
$add->product_id = (int)$_POST['product_id'];
$add->user_id = $_POST['user'];
$add->qty = (int)$_POST['qty'];
$add->save(false);
}
if ($flag == 1) {
$flag2 = 0;
if (isset($_POST['specVal']) and ! empty($_POST['specVal'])) {
foreach ($session['cart'] as $key2 => $productArray) {
if (array_diff($_SESSION['cart'][$key2]['specifications'], $_POST['specVal']) === array_diff($_POST['specVal'], $_SESSION['cart'][$key2]['specifications']) and $_POST['product_id'] == $productArray['product_id']) {
$flag2 = 1;
$productKey2 = $key2;
break;
}
}
}
$vals = array();
$additionalPrice = 0.00;
if (isset($_POST['specVal']) and ! empty($_POST['specVal'])) {
foreach ($_POST['specVal'] as $spec_id => $spec_val_id) {
$vals[$spec_id] = $spec_val_id;
$specVal = SpecVals::findOne($spec_val_id);
if ($specVal) {
if ($specVal->pay_type == 'F') {
$additionalPrice += $specVal->amount;
} else if ($specVal->pay_type == 'P') {
$additionalPrice += ($specVal->amount * $productPrice) / 100;
}
}
}
}
$totalPrice = $quantity * ($productPrice + $additionalPrice);
$myarr[] = array(
'product_id' => $product_id,
'quantity' => $quantity,
'productPrice' => $productPrice,
'specificationPrice' => $additionalPrice,
'totalPrice' => $totalPrice,
'specifications' => $vals,
);
} else {
$vals = array();
$additionalPrice = 0.00;
if (isset($_POST['specVal']) and ! empty($_POST['specVal'])) {
foreach ($_POST['specVal'] as $spec_id => $spec_val_id) {
$vals[$spec_id] = $spec_val_id;
$specVal = SpecVals::findOne($spec_val_id);
if ($specVal) {
if ($specVal->pay_type == 'F') {
$additionalPrice += $specVal->amount;
} else if ($specVal->pay_type == 'P') {
$additionalPrice += ($specVal->amount * $productPrice) / 100;
}
}
}
}
$totalPrice = $quantity * ($productPrice + $additionalPrice);
$myarr[] = array(
'product_id' => $product_id,
'quantity' => $quantity,
'productPrice' => $productPrice,
'specificationPrice' => $additionalPrice,
'totalPrice' => $totalPrice,
'specifications' => $vals,
);
}
} else {
$result['reload'] = 0;
$result['msg'] = 'Проблем с намирането на продукта';
}
} else {
$result['reload'] = 0;
$result['msg'] = 'Възникна проблем';
}
echo json_encode($result);
}