WHMCS模板产品价格货币自动切换问题求助(代码调试)
Hey there! Let's troubleshoot why your currency-based price switching code isn't working and get it up and running smoothly.
First, Fix the Condition Syntax
Your current code uses a single equals sign = in the {if} statement, which is for assignment in most template engines (like Smarty, which your syntax resembles). To compare values correctly, you need to use a double equals sign ==:
<h4 class="h5">Starting At<strong> {if $currency == 2} // currency id = 2// Rs 1.19 {else} $ 1.19 {/if} <small>/m</small></strong></h4>
That's the most likely culprit here—right now your code is trying to set $currency to 2 instead of checking if it matches 2.
Make It More Robust (Avoid Hardcoding Prices)
Hardcoding prices directly in the template isn't ideal if you ever need to update them or add more currencies. Instead, pass structured price data from your backend that maps currency IDs to their respective values and symbols:
Backend Example (e.g., PHP with Smarty)
// Fetch prices from your database or config file $product_prices = [ 1 => 1.19, // USD 2 => 1.19 // INR ]; $currency_symbols = [ 1 => '$', 2 => 'Rs' ]; // Pass variables to the template $smarty->assign('currency', $_SESSION['active_currency_id']); $smarty->assign('product_prices', $product_prices); $smarty->assign('currency_symbols', $currency_symbols);
Updated Template Code
<h4 class="h5">Starting At<strong> {$currency_symbols[$currency]} {$product_prices[$currency]} <small>/m</small></strong></h4>
This setup lets you easily add new currencies later without editing the template itself.
If You Need Frontend Dynamic Switching
If users switch currencies without reloading the page, you'll need JavaScript to update the price in real-time:
HTML
<select id="currency-switcher"> <option value="1">USD</option> <option value="2">INR</option> </select> <h4 class="h5">Starting At<strong> <span id="price-value">$ 1.19</span> <small>/m</small></strong></h4>
JavaScript
const currencySwitcher = document.getElementById('currency-switcher'); const priceDisplay = document.getElementById('price-value'); // Define price data (could also fetch this from an API) const priceData = { 1: { symbol: '$', amount: 1.19 }, 2: { symbol: 'Rs', amount: 1.19 } }; currencySwitcher.addEventListener('change', () => { const selectedCurrency = currencySwitcher.value; const price = priceData[selectedCurrency]; priceDisplay.textContent = `${price.symbol} ${price.amount}`; });
Double-Check Variable Availability
Finally, confirm that the $currency variable is actually being passed to your template from the backend. If it's missing or has an unexpected value, the condition will never trigger correctly. You can debug this by adding {$currency} somewhere in your template to see what value it holds.
内容的提问来源于stack exchange,提问作者susanto

