Background of the issue:
I have a lot of custom code in my function.php file for my site hidden link. The code includes custom fields for products, JavaScript for dynamic pricing, and additional product information display. I am trying to translate these custom fields using WPML.
// Sekcja 1: Funkcja wyświetlająca niestandardowe pola produktu na stronie produktu
function display_custom_product_fields() {
global $post;
$fields = [
'koszt_dostawy' => 'Koszt dostawy',
'instalacja_elektryczna' => 'Instalacja Elektryczna',
'instalacja_hydrauliczna' => 'Instalacja Hydrauliczna',
'plytowanie_gk' => 'Płytowanie GK',
'fundamenty_punktowe' => 'Fundamenty punktowe',
'plyta_fundamentowa' => 'Płyta fundamentowa',
'ogrzewanie_podlogowe' => 'Ogrzewanie podłogowe',
'pakiet_pod_klucz' => 'Pakiet pod klucz',
'taras_betonowy' => 'Taras betonowy',
'taras_drewniany' => 'Taras drewniany',
'izolacja_otwarta_dyfuzja' => 'Zastosowanie izolacji z otwartą dyfuzją',
'personalizacja_projektu' => 'Personalizacja projektu',
'wiata_samochodowa' => 'Wiata samochodowa',
'elewacja_drewniana' => 'Elewacja drewniana - Thermofuru',
'scianka_kolankowa' => 'Ścianka kolankowa+N 70cm',
'lazienka_na_pietrze' => 'Łazienka na piętrze',
'pakiet_podbitka_plus' => 'Pakiet podbitka plus',
'pakiet_dekoracji_elewacyjnych' => 'Pakiet dekoracji elewacyjnych',
'zmiana_pokrycia_dachu' => 'Zmiana pokrycia dachu'
];
$fields_filled = false;
foreach ($fields as $field => $label) {
$value = get_post_meta($post->ID, $field, true);
if (!empty($value)) {
$fields_filled = true;
break;
}
}
if (!$fields_filled) {
return; // Jeśli żadne pole nie jest wypełnione, nie wyświetlaj pola
}
// Dodanie znaczników dla pól niestandardowych produktu
echo '';
echo 'Skonfiguruj swój dom:';
foreach ($fields as $field => $label) {
$value = get_post_meta($post->ID, $field, true);
if (empty($value)) {
continue; // Pomiń puste wartości
}
if ($field == 'koszt_dostawy') {
$dostawa_za_km_value = get_post_meta($post->ID, 'dostawa_za_km', true);
echo ' ' . $label . ' ' . $value . ' zł + '.$dostawa_za_km_value.' zł/km';
echo 'Odległość od zakładu [km]';
} else if ($field == 'taras_betonowy' || $field == 'taras_drewniany' || $field == 'elewacja_drewniana') {
echo ' ' . $label . ' ' . $value . ' zł/m2';
echo 'Powierzchnia [m2]';
} else if ($field == 'lazienka_na_pietrze') {
echo ' ' . $label . '';
echo '';
echo 'ID, 'toaleta', true).'"> Toaleta';
echo 'ID, 'lazienka_z_wanna', true).'"> Łazienka z wanną lub prysznicem';
echo '';
} else {
echo ' ' . $label . ' ' . $value . ' zł';
}
}
echo '';
}
add_action('woocommerce_single_product_summary', 'display_custom_product_fields', 25);
// Sekcja 2: Dodanie skryptu JavaScript, aby pola niestandardowe działały na stronie produktu
function add_custom_script() {
global $post;
?>
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('koszt_dostawy').addEventListener('change', function() {
var kmField = document.getElementById('dostawa_km_field');
kmField.style.display = this.checked ? 'block' : 'none';
});
document.getElementById('taras_betonowy').addEventListener('change', function() {
var meterInput = document.getElementById('taras_betonowy_meters_field');
meterInput.style.display = this.checked ? 'block' : 'none';
});
document.getElementById('taras_drewniany').addEventListener('change', function() {
var meterInput = document.getElementById('taras_drewniany_meters_field');
meterInput.style.display = this.checked ? 'block' : 'none';
});
document.getElementById('elewacja_drewniana').addEventListener('change', function() {
var meterInput = document.getElementById('elewacja_drewniana_meters_field');
meterInput.style.display = this.checked ? 'block' : 'none';
});
document.getElementById('lazienka_na_pietrze').addEventListener('change', function() {
var options = document.getElementById('lazienka_na_pietrze_options');
options.style.display = this.checked ? 'block' : 'none';
});
});
jQuery(document).ready(function($) {
function update_product_price() {
var regular_price = parseFloat("");
var sale_price = parseFloat("");
var base_price = !isNaN(sale_price) && sale_price > 0 ? sale_price : regular_price;
var total_price = base_price;
$('.custom-product-fields input[type=checkbox]:checked').each(function() {
var extra_cost = parseFloat($(this).val());
if (!isNaN(extra_cost)) {
total_price += extra_cost;
}
});
var km_cost = 0;
if ($('#koszt_dostawy').is(':checked')) {
var km = parseFloat($('#dostawa_km').val());
if (!isNaN(km)) {
var per_km_cost = parseFloat("");
if (!isNaN(per_km_cost)) {
km_cost = km * per_km_cost;
total_price += km_cost;
}
}
}
if ($('#taras_betonowy').is(':checked')) {
var beton_m2 = parseFloat($('#taras_betonowy_meters').val());
var beton_cost = parseFloat($('#taras_betonowy').val());
if (!isNaN(beton_m2) && !isNaN(beton_cost)) {
total_price += beton_m2 * beton_cost;
}
}
if ($('#taras_drewniany').is(':checked')) {
var drewniany_m2 = parseFloat($('#taras_drewniany_meters').val());
var drewniany_cost = parseFloat($('#taras_drewniany').val());
if (!isNaN(drewniany_m2) && !isNaN(drewniany_cost)) {
total_price += drewniany_m2 * drewniany_cost;
}
}
if ($('#elewacja_drewniana').is(':checked')) {
var elewacja_m2 = parseFloat($('#elewacja_drewniana_meters').val());
var elewacja_cost = parseFloat($('#elewacja_drewniana').val());
if (!isNaN(elewacja_m2) && !isNaN(elewacja_cost)) {
total_price += elewacja_m2 * elewacja_cost;
}
}
// Aktualizacja ceny promocyjnej i regularnej
if (!isNaN(sale_price) && sale_price > 0) {
$('.price del .woocommerce-Price-amount').text(regular_price.toFixed(2) + ' zł');
$('.price ins .woocommerce-Price-amount').text(total_price.toFixed(2) + ' zł');
} else {
$('.price .woocommerce-Price-amount').first().text(total_price.toFixed(2) + ' zł');
}
}
$('.custom-product-fields input[type=checkbox], .custom-product-fields input[type=number]').on('change keyup', update_product_price);
update_product_price();
});
function updatePrice() {
var select = document.getElementById("customer_type");
var selectedValue = select.value;
// Pobierz oryginalną cenę z elementu
var originalPriceElement = document.querySelector('.woocommerce-Price-amount');
var originalPrice = parseFloat(originalPriceElement.innerText.replace(/[^d.]/g, ''));
var newPrice = originalPrice;
if (selectedValue == "23") {
newPrice = originalPrice * 1.15;
}
// Aktualizuj cenę na stronie
originalPriceElement.innerText = newPrice.toFixed(2) + " zł";
}
'fa-arrows-alt',
'powierzchnia_zabudowy' => 'fa-arrows-alt',
'pokoje' => 'fa-bed'
];
return isset($icons[$field]) ? $icons[$field] : 'fa-info-circle';
}
Symptoms:
I can't find any of the custom fields in WPML to translate.
Questions:
How can I make my custom fields from function.php translatable in WPML?
Is there a specific way to register custom fields so that WPML can recognize them?
Do I need to modify my existing code to make the custom fields translatable?
|