HTML block in product tabs problem

Hi,

I have a problem with some HTML blocks and JS codes and need your help.
I am trying to show the quantity in different warehouses for each product in store.
I found a way to do that, with Chat GPT help :joy: but the problem is that the same HTML and JS combination works if I put HTML in the motivation block, but not if I put it in the HTML block for the product card. JS code gets all the needed data but looks like it can’t send data back to that HTML block that is in the product tab. You can see on the screenshot, that in the motivation block, there is data like 100,200,300…, and in the product tab, there is no data.
Maybe it’s not the best way and best-written codes, but that is what I can do with Chat GPT :rofl:

HTML:

<div id="sw_elm_availability" class="pib-title cm-combination">
    <i class="material-icons">store</i><em>RaspoloĹživost u radnjama</em>
  </div>
<div id="elm_availability" class="pib-content cm-popup-box" style="display: block;">
    <ul>
      <li>Magacin 1: <span id="magacin1-stock">Nema podataka</span> komada</li>
      <li>Magacin 2: <span id="magacin2-stock">Nema podataka</span> komada</li>
      <li>Magacin 3: <span id="magacin3-stock">Nema podataka</span> komada</li>
    </ul>
  </div>
<script src="/js/zaliha.js"></script>

JS:

document.addEventListener('DOMContentLoaded', function() {
    const productCode = document.querySelector('.ty-control-group__item').innerText;

    // Učitavanje svih CSV fajlova za magacine
    Promise.all([
        fetch('https://noks.co.rs/images/magacin1.csv').then(response => response.text()),
        fetch('https://noks.co.rs/images/magacin2.csv').then(response => response.text()),
        fetch('https://noks.co.rs/images/magacin3.csv').then(response => response.text())
    ])
    .then(([magacin1Data, magacin2Data, magacin3Data]) => {
        let magacin1Stock = findStockInCSV(magacin1Data, productCode);
        let magacin2Stock = findStockInCSV(magacin2Data, productCode);
        let magacin3Stock = findStockInCSV(magacin3Data, productCode);

        // AĹžuriranje HTML-a sa podacima o zalihama
        document.getElementById('magacin1-stock').textContent = magacin1Stock;
        document.getElementById('magacin2-stock').textContent = magacin2Stock;
        document.getElementById('magacin3-stock').textContent = magacin3Stock;
    })
    .catch(error => {
        console.error('Greška prilikom učitavanja CSV fajlova:', error);
    });
});

// Funkcija za pronalazak količine u CSV fajlu
function findStockInCSV(csvData, productCode) {
    let rows = csvData.split('\n');
    for (let i = 0; i < rows.length; i++) {
        let cols = rows[i].split(';');
        if (cols[0] === productCode) {
            return cols[1]; // Vraćamo količinu
        }
    }
    return 'Nema podataka'; // Ako product_code nije pronađen
}

I am open to suggestions if somebody has some different easier ideas for the same results…

Thanks in advance!

Hi!

If you’re using the Warehouses add-on and in the Settings > Appearance the Show number of available products checkbox is checked, quantity will be displayed:

Hi,

Thank you for your answer, but I am not using addon because it requires CS-Cart Ultimate.

1 Like

Any other ideas?

Thanks

Have you tried to go to Settings > Appearance - scroll down until you find the “Show number of available products”, check the box, save and clear cache?

Hi,

Thank you for your answer.
I tried with that option enabled, but still in the product tab, there is no data.

Any new ideas?

Thanks.