D
D
Dmitry Samoilov2020-07-31 13:57:01
JavaScript
Dmitry Samoilov, 2020-07-31 13:57:01

How to empty the shopping cart after ordering?

Good day, I'm almost done with the site and I'm stuck on the question of emptying the cart, I want to delete all the items that are in the cart when a person makes an order

I have a code to remove an item from the cart

//удаляем товар из корзины
function delGoods() {
    
    let id = $(this).attr('data-id');
    delete cart[id];
    saveCart();
    showCart();
}


function saveCart() {
    //сохраняю корзину в localStorage
    localStorage.setItem('cart', JSON.stringify(cart)); //корзину в строку
}


The basket block where the selected services are displayed I tried some options, but for some reason nothing works, I would like to know how this is done at all
<div class="main-cart"></div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alexalexes, 2020-07-31
@alexalexes

Look at the showCart function. Under what conditions does it draw an empty cart, put this state in cart.

function delGoods()
{
    cart = {}; // ... я поставил пустой объект, к примеру.
    saveCart();
    showCart();
}

A
alekssamos, 2020-07-31
@alekssamos

Write an empty JSON {} or [] there, in localStorage.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question