To change the currency symbol to its respective 3-letter currency code

To make this solution applicable for any currency, you can create a generalized approach to change the currency symbol to its respective 3-letter currency code (like “USD” for the US Dollar, “SAR” for Saudi Riyal, etc.). Here’s how to achieve this:

### Custom Code to Display 3-Letter Currency Code for All Currencies
You can add the following code snippet to your theme’s `functions.php` file or use a plugin like **Code Snippets**:

#### Steps:
1. Go to **Appearance > Theme File Editor** or use **Code Snippets** plugin.
2. Open your active theme’s `functions.php` file.
3. Add the following code to convert any currency symbol into its 3-letter currency code:

“`php
// Change WooCommerce currency symbol to the currency code for all currencies
add_filter(‘woocommerce_currency_symbol’, ‘change_currency_symbol_to_code’, 10, 2);

function change_currency_symbol_to_code( $currency_symbol, $currency ) {
// Define an associative array of currency codes
$currency_codes = array(
‘USD’ => ‘USD’, // US Dollar
‘SAR’ => ‘SAR’, // Saudi Riyal
‘EUR’ => ‘EUR’, // Euro
‘GBP’ => ‘GBP’, // British Pound
‘INR’ => ‘INR’, // Indian Rupee
‘JPY’ => ‘JPY’, // Japanese Yen
‘CAD’ => ‘CAD’, // Canadian Dollar
// Add more currencies as needed
);

// Return the currency code if it exists in the array
if( array_key_exists( $currency, $currency_codes ) ) {
return $currency_codes[ $currency ];
}

// Otherwise, return the default currency symbol
return $currency_symbol;
}
“`

### Explanation:
– This code defines a list of currency codes for various currencies.
– It replaces the currency symbol with the corresponding 3-letter currency code for each currency in the array.
– You can add or remove currency codes as needed by editing the `$currency_codes` array.

### 4. **Clear Cache:**
After implementing this change, clear your website cache (if you’re using caching plugins) and browser cache.

Now, whenever you select any currency in WooCommerce, it will display the 3-letter currency code (like “USD”, “SAR”, etc.) instead of the usual symbol for that currency.

© 2023 Aravindhakumar G | All Rights Reserved
Privacy Policy