Advertisement
businessdad

Aelia Currency Switcher - Show prices in multiple currencies

Mar 8th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.32 KB | None | 0 0
  1. /**
  2.  * Example of how to show the prices of a product in different currencies on the
  3.  * same page.
  4.  *
  5.  * Need help customising the code for your need? Hire us on Codeable: http://bit.ly/codeable_aelia
  6.  *
  7.  * NOTE
  8.  * Replace "some_action_hook" with the appropriate hook, to display the prices
  9.  * in the correct location.
  10.  *
  11.  * @author Aelia <support@aelia.co>
  12.  */
  13. add_action('some_action_hook', function() {
  14.   // Get the ID of current product
  15.   $product_id = get_the_ID();
  16.  
  17.   // Safeguard. We can't proceed without a product ID
  18.   if(empty($product_id)) {
  19.     return;
  20.   }
  21.  
  22.   // Keep a list of the currencies we would like to show on the page. We will
  23.   // skip the active currency
  24.   $currencies_to_show = array('EUR', 'USD', 'GBP');
  25.   $active_currency = get_woocommerce_currency();
  26.  
  27.   foreach($currencies_to_show as $currency) {
  28.     // Skip the active currency, as the price is already displayed at the top
  29.     // of the page
  30.     if($currency === $active_currency) {
  31.       continue;
  32.     }
  33.    
  34.     // Wrapping the price HTML in a <span> will make it easier to style it
  35.     echo '<span class="price">';
  36.     // Render the product price for the currency
  37.     echo do_shortcode('[aelia_cs_product_price product_id="' . $product_id / '" formatted="1" currency="' . $currency . '"]');
  38.     echo '</span>';
  39.   }
  40. }, 10, 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement