Hello dear Team,
It seems that there is an Issue with Strings in functions in the functions.php in wordpress that you wpml string setup .. it gives back the correct $lang .. but the translation is always the default langauge..
Basically its about a function in functions.php:
with theese:
$output .= '<div class="cell heading-cell">' . __('Rabatt (Prozent)', 'discount-table-shortcode') . '</div>';
If have added the correct string tranbslation, but the langauge is never the one from the page ..
I use this as a shortcode on various pages..
How can I display wpml strings simply with the correct langage code?
This is the complete code:
// Shortcode function to display discount percentages and discounted prices for all quantity ranges for a specific product
function display_discount_info_shortcode($atts) {
// Set the language for WPML
if (function_exists('icl_get_languages')) {
$languages = icl_get_languages('skip_missing=0');
if (isset($languages[ICL_LANGUAGE_CODE]['default_locale'])) {
$lang = $languages[ICL_LANGUAGE_CODE]['default_locale'];
echo $lang;
switch_to_locale($lang);
}
}
// Extract shortcode attributes
$atts = shortcode_atts(array(
'id' => '', // Default product ID
), $atts, 'display_discount_info');
// Get the product ID from shortcode attribute
$product_id = $atts['id'];
// Get the product based on ID
$product = wc_get_product($product_id);
// Check if product exists
if ($product) {
// Initialize output variable
$output = '';
// Define quantity ranges and their corresponding discounts
$quantity_ranges = array(
array('min' => 1, 'max' => 5),
array('min' => 6, 'max' => 10),
array('min' => 11, 'max' => 24),
array('min' => 25, 'max' => PHP_INT_MAX) // PHP_INT_MAX represents an arbitrary large number
);
// Get the sale price of the product
$sale_price = $product->get_price();
// Start the "table" container
$output .= '<div class="discount-info-table">';
// Heading row for Quantity Range
$output .= '<div class="heading-row">';
$output .= '<div class="cell heading-cell">' . __('Menge', 'discount-table-shortcode') . '</div>';
// Loop through each quantity range to create cells
foreach ($quantity_ranges as $range) {
// Determine the maximum value for display
$max_display = ($range['max'] === PHP_INT_MAX) ? '∞' : $range['max'];
// Quantity range cell
$output .= '<div class="cell">' . $range['min'] . ' - ' . $max_display . '</div>';
}
$output .= '</div>'; // End heading row
// Heading row for Discount Percentage
$output .= '<div class="heading-row">';
$output .= '<div class="cell heading-cell">' . __('Rabatt (Prozent)', 'discount-table-shortcode') . '</div>';
// Loop through each quantity range to create cells
foreach ($quantity_ranges as $range) {
// Get the discount price for the current quantity range
$discount_price = apply_filters('advanced_woo_discount_rules_get_product_discount_price', $sale_price, $product, $range['min']);
// Calculate the discount percentage
$discount_percentage = ($sale_price - $discount_price) / $sale_price * 100;
// Discount percentage cell
$output .= '<div class="cell">' . $discount_percentage . '%</div>';
}
$output .= '</div>'; // End heading row
// End the "table" container
$output .= '</div>'; // End discount-info-table
// Restore the original language
if (function_exists('restore_previous_locale')) {
restore_previous_locale();
}
// Return the output
return $output;
} else {
// Restore the original language
if (function_exists('restore_previous_locale')) {
restore_previous_locale();
}
return __('Produkt mit der angegebenen ID nicht gefunden.', 'discount-table-shortcode');
}
}
// Register the shortcode
add_shortcode('display_discount_info', 'display_discount_info_shortcode');
|