Search code examples
phpjquerywordpresswoocommercecurrency

Mark current currency in menu


I use Wordpress, WooCommerce and WooCommerce Currency Switcher. Instead of using the normal (ugly) dropdown I have created links in the top-bar to change currency. Here is the code:

jQuery(function () {
    jQuery('.woocs_curr_link').click(function () {
       window.location.href = location.protocol + '//' + location.host + 
       location.pathname + '?currency=' + jQuery(this).data('curr');
    });
 });

And here is the link

<a href="#" data-curr="EUR" class="woocs_curr_link">EUR</a>

This works great but the current currency is not marked out in the meny. It can be fethed with following code:

global $WOOCS;
echo $WOOCS->storage->get_val('woocs_current_currency');

How can I extend my code to mark out current currency in the top-bar menu?

Thanks!


Solution

  • I solved it like this, direct in the links with the help by fetching the current set currency and then add a class to mark out the right one.

    global $WOOCS;
    $curcur = $WOOCS->storage->get_val('woocs_current_currency');
    ?>
    
    <ul class="nav-menu nav-curentcy">
      <li <?php if ($curcur == 'EUR') echo " class=\"cur_cur\""; ?>>
        <a href="#" data-curr="EUR" class="woocs_curr_link">EUR</a>
      </li>
      <li <?php if ($curcur == 'USD') echo " class=\"cur_cur\""; ?>>
        <a href="#" data-curr="USD" class="woocs_curr_link">USD</a>
      </li>
      <li <?php if ($curcur == 'SEK') echo " class=\"cur_cur\""; ?>>
        <a href="#" data-curr="SEK" class="woocs_curr_link">SEK</a>
      </li>
    </ul>