Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricBusch/c563f8e28f2b4e5a24b92027df9acfa4 to your computer and use it in GitHub Desktop.
Save EricBusch/c563f8e28f2b4e5a24b92027df9acfa4 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.
<?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->get_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