Last active
November 6, 2022 11:06
-
-
Save EricBusch/adc1294699e944815b14ed98c81eaf60 to your computer and use it in GitHub Desktop.
If you have set the default currency code in your WooCommerce settings but are displaying some products which use a different currency, here's how to replace the default currency symbol with the currency provided in the original data feed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Change the currency symbol for the WooCommerce product to match the currency | |
* of the product supplied by the Datafeedr API. | |
* | |
* @see dfrapi_currency_code_to_sign() | |
* @global WC_Product $product | |
* | |
* @param string $currency_symbol Current currency symbol. | |
* @param string $currency Base WooCommerce currency code. | |
* | |
* @return string Updated currency symbol. | |
*/ | |
add_filter( 'woocommerce_currency_symbol', 'mycode_change_currency_symbol', 10, 2 ); | |
function mycode_change_currency_symbol( $currency_symbol, $currency ) { | |
global $product; | |
if ( ! is_object( $product ) || ! isset( $product ) ) { | |
return $currency_symbol; | |
} | |
$fields = get_post_meta( $product->id, '_dfrps_product', true ); | |
if ( empty( $fields ) ) { | |
return $currency_symbol; | |
} | |
if ( ! isset( $fields['currency'] ) ) { | |
return $currency_symbol; | |
} | |
$currency_symbol = dfrapi_currency_code_to_sign( $fields['currency'] ); | |
return $currency_symbol; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment