【发布时间】:2014-06-11 23:31:12
【问题描述】:
我正在开发一个简单的Wordpress 网站,它将使用Cookie 跟踪用户订单。我目前拥有大部分功能,但我发现了一个问题。
当我以 userA 身份登录时,会创建一个 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: '/' });
});
【问题讨论】:
标签: javascript jquery wordpress cookies