<p>Well. It's not pretty but it works. That improvements could be made on this is both certain and welcome.</p>

<pre><code>{# Check to see if we've for got prices set, Set them if we do... #}

{% set priceDKK = entry.price[0].DKK ? entry.price[0].DKK : false %}
{% set priceEUR = entry.price[0].EUR ? entry.price[0].EUR : false %}
{% set priceUSD = entry.price[0].USD ? entry.price[0].USD : false %}

{# Figure out which price and rate we have to calc with... #}
{# Create our var before the loop (because scope)... #}

{% set basePrice = "" %}

{# We work down the list looking for our first set price #}
{# We will set it as our base price #}
{# We are going to convert all prices to DKK first #}

{% for price in entry.price %}
  {% if priceDKK %}
    {% set basePrice = priceDKK / rateDKK %}
  {% elseif priceEUR %}
    {% set basePrice = priceEUR / rateEUR %}
  {% elseif priceUSD %}
    {% set basePrice = priceUSD / rateUSD %}
  {% endif %}
{% endfor %}

{# Calculate prices if they don't exist... #}
{# We do not calc prices that were manually set... #}

{% for price in entry.price %}
  {% if not priceDKK %}
    {% set priceDKK = basePrice * rateDKK | round(-3,'ceil') %}
  {% endif %}

  {% if not priceEUR %}
    {% set priceEUR = (basePrice * rateEUR) | round(-3,'ceil') %}
  {% endif %}

  {% if not priceUSD %}
    {% set priceUSD = (basePrice * rateUSD) | round(-3,'ceil') %}
  {% endif %}
{% endfor %}

{# end #}
</code></pre>

CC BY-SA 4.0
2020-03-31T07:44:20.667Z
2020-03-31T07:44:20.667Z
kerns
<p>I am using a table field in Craft (<code>price</code>) limited to one row only, used to manage price setting in different currencies on an entry. The three cols each contain number fields with the handles <code>dkk</code>, <code>euro</code>, and <code>usd</code> respectively. (<strong>note</strong> their column order here can be used to infer a preference later which is useful)</p>

<p>At the template level I am also setting a variable <code>pricepreferredCurrency</code> to specify the currency we want to use for conversions when required. This could be moved to the cp later I guess but it's barcoded at the template level for now.</p>

<pre><code>{% set preferredCurrency = euro %}
</code></pre>

<p>If at minimum one price is set I would like to display the price in all three currencies, but I am struggling to create template logic needed to (smartly) support the following functions:</p>

<ul>
<li><p>If any price is <em>explicitly</em> set, use that price and do not unset or
recalculate the price based on other prices or currencies. Just take the input at face value.</p></li>
<li><p>Otherwise, if a price is empty or unset but at least one other price is set, use the next available price that <em>is</em> set as the price to convert from (using <code>preferredCurrency</code> as the basis for that conversion)</p></li>
<li><p>If no prices in any currencies are set, exit and show no prices.</p></li>
</ul>

<p>I have already worked out the conversion between currencies, but I am having trouble looping through and making good decisions and logic to handle the different cases. If I can get to the cases I can handle the rest I think.</p>

<p>I feel like this is probably very simple and could even be abstracted I am sure to loop through an expanded set of currencies (without the need to get explicit) but that is beyond me.</p>

<p>I am also open to completely other ways of doing this in craft. Maybe it is better to move currencies into a category group.</p>

<p>I think ultimately I will make decisions about which price to show based on some geolocation, but now I am ready to calculate (where needed) and surface all three. Your help/advice as always much appreciated.</p>

<p>You can see I started to work through this and could immediately tell I was off on the wrong foot. This is really embarrassing to share but...</p>

<pre><code>{% for row in entry.price %}
{% set preferredCurrency = DKK %}

{% set priceDKK = row.dkk %}
{% set priceEURO = row.euro %}
{% set priceUSD = row.usd %}

{% if row.dkk is not empty %}
  {% set priceDKK = row.dkk %}
{% elseif priceEURO is not empty %}
  {% set priceEURO = row.euro %}
  {# abort - it is here I realized this is some dead-end foolery #}
</code></pre>

<templating><forloop><fieldtypes>
How to loop through a table and set variables based on the existence (or not) of data in those tables?
# How to loop through a table and set variables based on the existence (or not) of data in those tables?

(Asked by: username_0 on 2020-03-21)

I am using a table field in Craft (price) limited to one row only, used to manage price setting in different currencies on an entry. The three cols each contain number fields with the handles dkk, euro, and usd respectively. (note their column order here can be used to infer a preference later which is useful)
At the template level I am also setting a variable pricepreferredCurrency to specify the currency we want to use for conversions when required. This could be moved to the cp later I guess but it's barcoded at the template level for now.
{% set preferredCurrency = euro %}

If at minimum one price is set I would like to display the price in all three currencies, but I am struggling to create template logic needed to (smartly) support the following functions:

If any price is explicitly set, use that price and do not unset or
recalculate the price based on other prices or currencies. Just take the input at face value.
Otherwise, if a price is empty or unset but at least one other price is set, use the next available price that is set as the price to convert from (using preferredCurrency as the basis for that conversion)
If no prices in any currencies are set, exit and show no prices.

I have already worked out the conversion between currencies, but I am having trouble looping through and making good decisions and logic to handle the different cases. If I can get to the cases I can handle the rest I think.
I feel like this is probably very simple and could even be abstracted I am sure to loop through an expanded set of currencies (without the need to get explicit) but that is beyond me.
I am also open to completely other ways of doing this in craft. Maybe it is better to move currencies into a category group.
I think ultimately I will make decisions about which price to show based on some geolocation, but now I am ready to calculate (where needed) and surface all three. Your help/advice as always much appreciated.
You can see I started to work through this and could immediately tell I was off on the wrong foot. This is really embarrassing to share but...
{% for row in entry.price %}
{% set preferredCurrency = DKK %}

{% set priceDKK = row.dkk %}
{% set priceEURO = row.euro %}
{% set priceUSD = row.usd %}

{% if row.dkk is not empty %}
  {% set priceDKK = row.dkk %}
{% elseif priceEURO is not empty %}
  {% set priceEURO = row.euro %}
  {# abort - it is here I realized this is some dead-end foolery #}

**Tags:** templating, forloop, fieldtypes


---

## 1 answers

### Answer by username_0 on 2020-03-31. Score: 1

Well. It's not pretty but it works. That improvements could be made on this is both certain and welcome.
{# Check to see if we've for got prices set, Set them if we do... #}

{% set priceDKK = entry.price[0].DKK ? entry.price[0].DKK : false %}
{% set priceEUR = entry.price[0].EUR ? entry.price[0].EUR : false %}
{% set priceUSD = entry.price[0].USD ? entry.price[0].USD : false %}

{# Figure out which price and rate we have to calc with... #}
{# Create our var before the loop (because scope)... #}

{% set basePrice = "" %}

{# We work down the list looking for our first set price #}
{# We will set it as our base price #}
{# We are going to convert all prices to DKK first #}

{% for price in entry.price %}
  {% if priceDKK %}
    {% set basePrice = priceDKK / rateDKK %}
  {% elseif priceEUR %}
    {% set basePrice = priceEUR / rateEUR %}
  {% elseif priceUSD %}
    {% set basePrice = priceUSD / rateUSD %}
  {% endif %}
{% endfor %}

{# Calculate prices if they don't exist... #}
{# We do not calc prices that were manually set... #}

{% for price in entry.price %}
  {% if not priceDKK %}
    {% set priceDKK = basePrice * rateDKK | round(-3,'ceil') %}
  {% endif %}

  {% if not priceEUR %}
    {% set priceEUR = (basePrice * rateEUR) | round(-3,'ceil') %}
  {% endif %}

  {% if not priceUSD %}
    {% set priceUSD = (basePrice * rateUSD) | round(-3,'ceil') %}
  {% endif %}
{% endfor %}

{# end #}


---

