1

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 #}
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 #}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.