Javascript 将Cookie分配给特定用户
我正在开发一个简单的Javascript 将Cookie分配给特定用户,javascript,jquery,wordpress,cookies,Javascript,Jquery,Wordpress,Cookies,我正在开发一个简单的Wordpress网站,它将使用Cookie跟踪用户订单。目前我拥有大部分的功能,但是我注意到了一个问题 当我以用户身份登录时,会创建一个cookie,我可以向其中添加项目。当我下订单并注销时,我以userB身份登录,我创建的cookie将显示,其中添加了userA项 我的问题是,如何才能为userA提供cookie,为userB提供cookie 下面是我检查cookie的代码: $(document).ready(function(){ //if cookie ex
Wordpress
网站,它将使用Cookie
跟踪用户订单。目前我拥有大部分的功能,但是我注意到了一个问题
当我以用户身份登录时,会创建一个cookie
,我可以向其中添加项目。当我下订单并注销时,我以userB身份登录,我创建的cookie
将显示,其中添加了userA项
我的问题是,如何才能为userA提供cookie
,为userB提供cookie
下面是我检查cookie
的代码:
$(document).ready(function(){
//if cookie exists, show the panel
if($.cookie('order_cookie') != undefined){
productArray = JSON.parse($.cookie('order_cookie'));
$(".order-alert").show();
$('#order_counter').html(productArray.length);
}
});
这是我在脚本中修改cookie
,因为用户在其中添加和删除项目:
//If the cookie exists get a reference to the array it contains (productArray)
if($.cookie('order_cookie') != undefined){
productArray = JSON.parse($.cookie('order_cookie'));
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
$('#products_field').val(encodeURIComponent($.cookie('order_cookie')));//Add to hidden field
console.log(encodeURIComponent($.cookie('order_cookie')));
}
//Reference to the order table
var ordertable = document.getElementById("ordertable");
//Loop through the Array and display in the table
for(var i = 0; i < productArray.length; i ++){
// console.log(productArray[i]);
console.log("Order Item " + i);
console.log("StockCode: " + productArray[i].stockCode);
console.log("Quantity: " + productArray[i].quantity);
var row = ordertable.insertRow(i + 1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
//Will need to Convert to JQuery - .html() method
cell1.innerHTML = productArray[i].stockCode;
cell2.innerHTML = productArray[i].quantity;
cell3.innerHTML = "<input type='button' value='-' class='removeBtn'/><input type='button' value='+' class='addBtn'/><input type='button' value='Delete' class='deleteBtn'/>"
}
//Delete Button - Removes item from the array and the table, updates the cookie
$(".deleteBtn").click(function(){
//Will need to Conver to JQuery - .Parent() method also "this"
var row = this.parentNode.parentNode;
var rowToDelete = row.rowIndex;
var elementToDelete = row.rowIndex-1;
//Remove from Array
productArray.splice(elementToDelete,1);
//Remove from Table
ordertable.deleteRow(rowToDelete);
//Update the Cookie with the information every time you delete
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
});
//Will remove 1 from the product quantity
$('.removeBtn').click(function(){ //Remove 1 from quantity
//Will need to Conver to JQuery - .Parent() method also "this""
var row = this.parentNode.parentNode;
var elementToUpdate = row.rowIndex - 1;
if( productArray[elementToUpdate].quantity <= 1){
ordertable.deleteRow(row.rowIndex);
productArray.splice(elementToUpdate,1);
}else{
productArray[elementToUpdate].quantity--;
ordertable.rows[row.rowIndex].cells[1].innerHTML = productArray[elementToUpdate].quantity;
}
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
});
//Will add 1 to the product quantity
$('.addBtn').click(function(){ //Add 1 to quantity
//Will need to Conver to JQuery - .Parent() method also "this"
var row = this.parentNode.parentNode;
var elementToUpdate = row.rowIndex - 1;
productArray[elementToUpdate].quantity++;
ordertable.rows[row.rowIndex].cells[1].innerHTML = productArray[elementToUpdate].quantity;
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
});
//如果cookie存在,则获取对其包含的数组(productArray)的引用
if($.cookie('order\u cookie')!=未定义){
productArray=JSON.parse($.cookie('order_cookie'));
$.cookie('order_cookie',JSON.stringify(productArray),{expires:1,path:'/'});
$(“#产品_字段”).val(encodeURIComponent($.cookie($.cookie('order_cookie'));//添加到隐藏字段
log(encodeURIComponent($.cookie('order_cookie'));
}
//对订单表的引用
var ordertable=document.getElementById(“ordertable”);
//循环遍历数组并显示在表中
对于(var i=0;i 如果(productArray[elementToUpdate].quantity在登录/注销时清除cookie,除非您有db设置来存储待定订单,否则我认为cookie不是正确的解决方案