javascript: data submit from button randomly not working

I am setting up an e-commerce webshop, and would like to have the buttons either show “Add to cart” in red or “Added to cart” in green. I have a working shop without this feature, and a working single button that changes color and text (via SVG and a lot of CSS). When I combine the two, it sometimes works, and sometimes doesn’t, and I don’t find the reason for that.

HTML:

<div class="toolbar">
            <h1 class="brand">Shop</h1>
            <p>
                <a href="cart.php">
                    Cart items:
                    <span id="count">0</span>, Price:
                    <span id="sum">0</span>
                </a>
            </p>
        </div>
    <div class="products">
                <div class="product">
                    <h3>Product one</h3>
                    <p>Price: 2000</p>
       <button data-id="1" data-price="2000" data-title="Product one" class="add-to-cart-button">
            <svg class="add-to-cart-box box-1" width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="24" height="24" rx="2" fill="#ffffff"/></svg>
            <svg class="add-to-cart-box box-2" width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="24" height="24" rx="2" fill="#ffffff"/></svg>
            <svg class="cart-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="9" cy="21" r="1"></circle><circle cx="20" cy="21" r="1"></circle><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path></svg>
            <svg class="tick" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path fill="#ffffff" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zM9.29 16.29L5.7 12.7c-.39-.39-.39-1.02 0-1.41.39-.39 1.02-.39 1.41 0L10 14.17l6.88-6.88c.39-.39 1.02-.39 1.41 0 .39.39.39 1.02 0 1.41l-7.59 7.59c-.38.39-1.02.39-1.41 0z"/></svg>
            <span class="add-to-cart">Add to cart</span>
            <span class="added-to-cart">Added to cart</span>
        </button>
                </div>
            </div>

There are several such “Product” entries (currently Product one … Product four).

Javascript:

let count = 0;
let sum = 0;
let cart = {};

if (localStorage.getItem("count")) {
    count = parseInt(localStorage.getItem("count"));
}

if (localStorage.getItem("sum")) {
    sum = parseInt(localStorage.getItem("sum"));
}

if (localStorage.getItem("cart")) {
    cart = JSON.parse(localStorage.getItem("cart"));
}

updateCart();

let btns = document.querySelectorAll(".products button");

for (let i = 0; i < btns.length; i++) {
    let btn = btns[i];
    btn.addEventListener("click", add);
}

function add(event) {
    let price = Number(event.target.dataset.price);
    let title = event.target.dataset.title;
    let id = event.target.dataset.id;

if (id in cart) {
    cart[id].qty++;
} else {
    let cartItem = {
        title: title,
        price: price,
        qty: 1
    };
    cart[id] = cartItem
}

    count++;
    sum += price;

    console.log(cart);

    localStorage.setItem("cart", JSON.stringify(cart));
    updateCart();
}

function updateCart() {
    document.getElementById("sum").textContent = sum;
    document.getElementById("count").textContent = count;
    localStorage.setItem("sum", sum);
    localStorage.setItem("count", count);
}

addToCartButton = document.querySelectorAll(".add-to-cart-button");

document.querySelectorAll('.add-to-cart-button').forEach(function(addToCartButton) {
    addToCartButton.addEventListener('click', function() {
        addToCartButton.classList.add('added');
    });
});

I currently have four entries, and when I click the corresponding buttons, some get stored in localStorage correctly (= price, title, qty), and with some, only the click is stored in localStorage (qty). When I delete localStorage and repeat the procedure, different items get stored. I couldn’t find any pattern in the behavior.

It seems that once an item is not stored correctly, clicking the corresponding button will only increase the number of qty entries that are “unassigned”.

How can I make this stable?