I've integrated simplecartjs into my online store to manage product data. The data is stored in local storage and currently looks like this:
{"SCI-1":{"quantity":1,"id":"SCI-1","price":20,"name":"Matt Black Tape","size":"Empty"},"SCI-3":{"quantity":1,"id":"SCI-3","price":19,"name":"Mohawk Soundproofing Mat 48 x 39cm"},"SCI-5":{"quantity":2,"id":"SCI-5","price":8,"name":"Car Speaker Air Freshener","color":"Green Tea"},"SCI-7":{"quantity":1,"id":"SCI-7","price":50,"name":"Installation Package"},"SCI-9":{"quantity":1,"id":"SCI-9","price":30,"name":"Quartz Films","color":"Yellow","size":"50cm x 30cm"},"SCI-11":{"quantity":1,"id":"SCI-11","price":30,"name":"Quartz Films","color":"True Blue","size":"50cm x 30cm"}}
Now, I want to include the following line before it closes.
"SCI-12":{"quantity":1,"id":"SCI-12","price":5,"name":"Shipping Costs"}
However, the number of products may vary based on the user, so SCI-12 should adjust accordingly with the items.
EDIT It doesn't have to be SCI-1, any identifier will work as long as it comes after the items.
EDIT2 I'm attempting the following code... But it's not working as expected.
$("#matkahuolto").click(function(){
var value_object = '"Shipping Costs":{"quantity":1,"id":"Shipping Costs","price":5,"name":"Shipping Costs"}';
var json_object = JSON.parse(localStorage.your_json); // convert string to object
json_object["simpleCart_items"] = value_object; // add value
localStorage.your_json = JSON.stringify(json_object); // store it again.
});
$("#posti").click(function(){
var json_object = JSON.parse(localStorage.your_json); // convert string to object
json_object["simpleCart_items"] = value_object; // add value
localStorage.your_json = JSON.stringify(json_object); // store it again.
});
EDIT3 A snapshot of the local storage.