I greet you with respect, gentlemen
Let me explain, later I will put the code here in the meantime.
On the WordPress platform in WooCommerce I added 120+ products (gold and silver)
I made a java script with someone’s help based on an API that converts from EURO to RON. The script has the following location: public_html/wp-content/themes/name of the theme/js/custom-scripts.js
The code is over here:
// Scriptul JavaScript cu functia de conversie
// Functia pentru a face cereri catre API folosind AJAX
function fetchMetalPrices() {
// Foloseste AJAX pentru a face cererea catre API
var xhr = new XMLHttpRequest();
var apiKey = '24cbf4a48e25885d3b8ff42474c80747';
xhr.open('GET', 'https://api.metalpriceapi.com/api/v1/latest', true);
xhr.setRequestHeader('x-access-token', apiKey);
xhr.onload = function () {
if (xhr.status == 200) {
// Proceseaza datele primite de la API
var data = JSON.parse(xhr.responseText);
// Actualizeaza preturile pe pagina
updatePrices(data);
// Calculeaza si actualizeaza preturile in RON
updatePricesInRON(data);
} else {
console.error('Cererea catre API a esuat. Cod de eroare: ' + xhr.status);
}
};
xhr.send();
}
{
}
async function getExchangeRate() {
const apiKey = "69b2d358023e988b804b4e61aca6413d";
const apiUrl = https://open.er-api.com/v6/latest/EUR?apikey=${apiKey};
try {
const response = await fetch(apiUrl);
const data = await response.json();
debugger;
if (response.ok) {
const eurToRonRate = data.rates.RON;
//console.log(EUR to RON exchange rate: ${eurToRonRate});
return eurToRonRate;
} else {
console.error(Error: ${data.error});
}
} catch (error) {
console.error("Error fetching exchange rate:", error.message);
}
}
});
// Functia pentru a actualiza preturile pe pagina
function updatePrices() {
// Verificăm dacă data.gold și data.silver există înainte de a încerca să accesăm 'price'
let data;
getExchangeRate().then((exchangeRate) => {
data = exchangeRate.toFixed(3);
//parseFloat(exchangeRate.toFixed(3))
}
if (data && data.gold && data.gold.price && data.silver && data.silver.price && data.exchange_rates && data.exchange_rates.EUR) {
// Actualizeaza elementele HTML cu noile preturi in EURO si DOLARI
document.getElementById('goldPriceEUR').innerText = data.gold.price + ' EUR';
document.getElementById('silverPriceEUR').innerText = data.silver.price + ' EUR';
document.getElementById('goldPriceUSD').innerText = data.gold.price * data.exchange_rates.EUR + ' USD';
document.getElementById('silverPriceUSD').innerText = data.silver.price * data.exchange_rates.EUR + ' USD';
} else {
console.error('Datele primite de la API lipsesc sau sunt definite incorect.');
}
}
// Functia pentru a calcula si actualiza preturile in RON
function updatePricesInRON(data) {
# Conversie din EURO/DOLARI in RON folosind o rata de schimb fictiva (poate fi ajustata)
var exchangeRateRON = 4.9; // Exemplu de rata de schimb
var goldPriceRON = data.gold.price * exchangeRateRON;
var silverPriceRON = data.silver.price * exchangeRateRON;
// Actualizeaza elementele HTML cu noile preturi in RON
document.getElementById('goldPriceRON').innerText = goldPriceRON.toFixed(2) + ' RON';
document.getElementById('silverPriceRON').innerText = silverPriceRON.toFixed(2) + ' RON';
}
// Facem cereri periodice catre API pentru a mentine actualizate preturile
setInterval(fetchMetalPrices, 60000); // Facem cereri la fiecare 60 de secunde (poate fi ajustat)
Now what must be done.
From the database with the added products, depending on the weight of each one, a calculation should be made and the final price displayed for each product
For gold, it must be as follows:
Gold product depending on the weight + commercial surcharge imposed by the customer = final price
For silver it must be as follows:
The weight of the silver product + commercial surcharge imposed by the customer + VAT 19% = final price
with the help of a friend who wrote the java script for me, I don’t know what to do next, how to connect to the database for each individual product depending on the weight to make a calculation for me as I specified above.