businessdad icon

Aelia Currency Switcher - Show prices in multiple currencies

businessdad | PRO | 03/08/17 12:25:51 PM UTC | 0 ⭐ | 929 👁️ | Never ⏰ | []
PHP |

1.32 KB

|

None

|

0 👍

/

0 👎

/**
 * Example of how to show the prices of a product in different currencies on the
 * same page.
 *
 * Need help customising the code for your need? Hire us on Codeable: http://bit.ly/codeable_aelia
 *
 * NOTE
 * Replace "some_action_hook" with the appropriate hook, to display the prices
 * in the correct location.
 * 
 * @author Aelia <[email protected]>
 */
add_action('some_action_hook', function() {
  // Get the ID of current product
  $product_id = get_the_ID();
  
  // Safeguard. We can't proceed without a product ID
  if(empty($product_id)) {
    return;
  }
  
  // Keep a list of the currencies we would like to show on the page. We will
  // skip the active currency
  $currencies_to_show = array('EUR', 'USD', 'GBP');
  $active_currency = get_woocommerce_currency();
  
  foreach($currencies_to_show as $currency) {
    // Skip the active currency, as the price is already displayed at the top
    // of the page
    if($currency === $active_currency) {
      continue;
    }
    
    // Wrapping the price HTML in a <span> will make it easier to style it
    echo '<span class="price">';
    // Render the product price for the currency
    echo do_shortcode('[aelia_cs_product_price product_id="' . $product_id / '" formatted="1" currency="' . $currency . '"]');
    echo '</span>';
  }
}, 10, 1);

Comments