There is a basket of goods, it works using a session, as well as a php handler and a js file that sends Ajax requests to the handler.
So, let's say we have 10 items in the basket. Now I delete them one by one.
If this is done slowly, then everything will go well.
If you click on the crosses quickly, then not all products can be removed (they do not have time) or, if everything is deleted, then the quantity of goods in the basket and the total amount may not be zeroed out.
Why it happens?
Example Ajax request:
//Обновление виджета корзины в шапке function updateCart() { $.post('/wp-admin/admin-ajax.php', { action: 'updateCart' }, function (res) { $('.cart-btn').html(res); } ); } updateCart();
Sample PHP handler:
function updateCart(){ $data = json_decode(stripslashes($_SESSION['cart']), ARRAY_A); // Перебераю массив foreach ($data as $key => $value) { // Узнаю общее количество товара $quantity_product += $value; // Узнаю цену и умножаю на колличество $price_product += get_field('price', $key) * $value; } // Вывожу колличество товара и сумму if(!$data){ echo "Товаров 0 (0) тг."; } else { if($quantity_product == 1) { echo "$quantity_product товар ($price_product) тг."; } elseif($quantity_product > 1) { echo "Товаров: $quantity_product ($price_product) тг."; } } die(); }