Transfer data from calculator
-
Hello. Some questions
- can we transfer calculated data to form field via shortcode or any another way ?
- Can we make a log of calculated data from guest and users to get some statistics of usage ?
Thank you.
-
Hello, thank you for writing to us. This is not provided in the plugin itself. But you can write a separate JS code that will listen for events in the plugin forms and send the data entered by the user to the database.
Example of code:document.addEventListener('DOMContentLoaded', function() {
// Function to initialize the debouncer once the converter is rendered
function initializeDebouncer() {
const inputField = document.querySelector('.cr-exchange-rates .amount input');
const resultField = document.querySelector('.cr-exchange-rates .result');
let debounceTimer;
if (!inputField) return; // If the input field is not yet available, exit
inputField.addEventListener('input', function() {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
const amount = inputField.value;
fetch('/path-to-your-server-endpoint', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
amount: amount,
fromCurrency: document.querySelector('.cr-exchange-rates select').value,
toCurrency: document.querySelector('.cr-exchange-rates select:last-child').value
}),
})
.then(response => response.json())
.then(data => {
console.log('Conversion logged:', data);
})
.catch(error => {
console.error('Error:', error);
});
}, 500); // 500 ms delay for the debounce
});
}
// Use a MutationObserver to detect when the converter is added to the DOM
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
if (document.querySelector('.cr-exchange-rates')) {
// If the currency converter is found, initialize the debouncer
initializeDebouncer();
observer.disconnect(); // Stop observing once initialized
}
}
}
});
// Start observing the body for added nodes (i.e., when the converter is injected into the DOM)
observer.observe(document.body, {
childList: true,
subtree: true
});
});@falselight , Thank you for this example.
Can you provide more info for this string :
fetch('/path-to-your-server-endpointI want to transfer converted (calculated) data as product price (USD) to checkout page. Is it possible to use /checkout as path for that ?
Also can we log converted data and show some statistics via shortcode?
- /path-to-your-server-endpoint – your api in wordpress, or 3rd-party server, for handler request, and put to db.
- Sure, Your conversion logs, which are stored in a database you can display anywhere on your WordPress page directly using PHP code or through shortcodes, via a third-party plugin.
global $wpdb;
// Example by PHP
$logs = $wpdb->get_results(
"SELECT * FROM {$wpdb->prefix}logs ORDER BY log_date DESC"
);I want to transfer converted (calculated) data as product price (USD) to checkout page. Is it possible to use /checkout as path for that ?
Sure, we have a brand-new plugin for this. Please try: https://ww.wp.xz.cn/plugins/fx-currency-converter/ This plugin convert price vis shortcode.
-
This reply was modified 1 year, 9 months ago by
falselight.
Thank you for the subject.
The topic ‘Transfer data from calculator’ is closed to new replies.