为特定用户分配Cookie

时间:2014-06-11 15:24:29

标签: javascript jquery wordpress cookies

我正在开发一个简单的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: '/' });
});

1 个答案:

答案 0 :(得分:0)

在登录/注销时清除cookie,除非你有一个数据库设置来存储挂单,我不认为cookie是正确的解决方案。