Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Счетчик товаров в корзине

Recommended Posts

Прикручиваю счетчик товаров в корзине, который должен обновляться при клике на эту кнопку:

<button onclick="addToCart('<?php echo $product['product_id']; ?>');" class="btn btn-shopping-cart btn-outline-inverse">В корзину&nbsp;<i class="fa fa-shopping-cart"></i>


Все работает с таймером, а со слушателем не хочет, в чем может быть причина?:

// Спан выводит количество товаров
<span class="hidden-lg hidden-md hidden-sm" id="product-count" style="color:#FFFFFF; background-color: #282c34; border-radius: 50%; padding: 2px 5px; margin-left: -10px;">
<?php echo $product_count; ?>

function updateProductCount() {
    let xhr = new XMLHttpRequest();
    xhr.open('GET', window.location.href, true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
            let productCountElement = document.getElementById('product-count');
            productCountElement.innerHTML = xhr.responseText.match(/id="product-count"[^>]*>([\s\S]*?)<\/span>/)[1];

// С таймером все работает
// setInterval(updateProductCount, 5000);

// С этим кодом не работает
let updateButtons = document.querySelectorAll('.btn.btn-shopping-cart.btn-outline-inverse');
for (let i = 0; i < updateButtons.length; i++) {
    updateButtons[i].onclick = function() {



Opencart, PHP 5.4.16
Может быть это из-за старой версии php?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.