Я разрабатываю простой сайт Wordpress
, который будет отслеживать заказ пользователей, используя Cookie
. На данный момент у меня есть основная часть функциональности, однако я заметил проблему.Назначить Cookie определенным пользователям
Когда я регистрируюсь как userA, создается cookie
, и я могу добавлять к нему элементы. Когда я размещаю заказ и выхожу из системы, я тогда вхожу в систему как userB, а созданный мной cookie
отображается с добавленными к нему элементами userA.
Мой вопрос: как я могу получить cookie
для пользователяA и cookie
для пользователяB ??
Ниже мой код, где я проверяю на 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: '/' });
});