0

Here is my code:

<div class="search-menu">
  <div class="btn-group fc">
    <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
      <?php
        $currencies = explode(',', hana_get_opt('_cc'));
        foreach ($currencies as $currency) {
          if ($currency == get_option('woocommerce_currency')){
            echo $currency;
            break;
          }else{
            echo "Select Currency";
            break;
          }
        }
      ?>
      <span class="caret"></span>
    </a>
    <ul class="dropdown-menu currency_switcher">
      <?php
        foreach ($currencies as $currency) {
          if ($currency == get_option('woocommerce_currency'))
            echo '<li><a href="#" class="reset default" data-currencycode="' . $currency . '">' . $currency . '</a></li>';
          else
            echo '<li><a href="#" data-currencycode="' . $currency . '">' . $currency . '</a></li>';
        }
      ?>
    </ul>
  </div>
</div>

Which works great it returns a list of my currencies and updates the page accordingly, I have just on question if the user has yet to set a value I would like it to say either select currency or just apply the first option from my hana_get_opt('_cc') array as the default. Here is the html generated code:

<div class="btn-group fc">
                        <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">undefined<span class="caret"></span></a>
                        <ul class="dropdown-menu currency_switcher">
  <li><a href="#" class="reset default active" data-currencycode="SEK">SEK</a></li>
  <li><a href="#" data-currencycode="EUR">EUR</a></li>                                
                        </ul>
                    </div>

I am no php coder and much appreciate any help provided Chris

2 Answers 2

1
   if(get_option('woocommerce_currency')==' ')
    {
        $currencies = explode(',', hana_get_opt('_cc'));
          foreach ($currencies as $currency) 
                {

                 if $currency == get_option('woocommerce_currency'){
                     {
                     echo $currency;
                     break;
                       }
                              else{
                                        echo "Select Currency";
                                        break;
                                    }
                 }
    }

    else
    {   
      echo "Select Currency";
       $currencies = explode(',', hana_get_opt('_cc'));
     $currency=$currencies [0];
    }
Sign up to request clarification or add additional context in comments.

6 Comments

Fatal error: Can't use function return value in write context in on this line: if(isset(get_option('woocommerce_currency')))
tell me wch variable u r getting input from
do u have dropdown element? how is user entering the currency?
a drop down element on the front end the user chooses which currency they want to display the site in from a drop down. thank you for your patience
what is the drop down name??
|
0
+50

based on the assumption that get_option('woocommerce_currency') would return null if no currency is selected

<div class="search-menu">
<div class="btn-group fc">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
  <?php
    $currencies = explode(',', hana_get_opt('_cc'));
    $currentCurrency = get_option('woocommerce_currency')
    if ($currentCurrency === false) {
        echo "select currency";
    }
    else{
        echo $currentCurrency;
    }
  ?>
  <span class="caret"></span>
</a>
<ul class="dropdown-menu currency_switcher">
  <?php
    foreach ($currencies as $currency) {
      if ($currency == $currentCurrency)
        echo '<li><a href="#" class="reset default" data-currencycode="' . $currency . '">' . $currency . '</a></li>';
      else
        echo '<li><a href="#" data-currencycode="' . $currency . '">' . $currency . '</a></li>';
    }
  ?>
</ul>

replace the if clause if ( $currentCurrency != null ) with whatever applies, if get_option('woocommerce_currency') returns anything other as null if not yet set

6 Comments

actually like this it still doesn't echo select currency if no currency has been designated in the drop down
what does get_option('woocommerce_currency') return, if nothing has been set before?
according to wordpress documentation get_option returns false codex.wordpress.org/Function_Reference/get_option . I altered my answer. That should work
it still returns undefined as the first option in the drop down followed by the currencies.
undefined sounds to me like a javascript error. Could you add the HTML code generate between the ul with the class dropdown-menu. I think there is one element in the currencies array, that should not be there.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.