webdna / commerce-bulk-pricing

Other
2 stars 2 forks source link

How can I loop through the field on the front-end to get the qty and prices? #3

Closed terryupton closed 4 years ago

terryupton commented 5 years ago

I would like to show a table for the qty's and the prices for each bulk qty as per what is setup in the backend. How can I loop through this on the front-end?

I have tried a few approaches, but hitting errors: Key "qty" for array with keys "iso, 2, 4" does not exist.

{% for column in purchasable.setDiscounts %}
  {{ column.qty }}
{% endfor %}

Array to string conversion

{% for column in purchasable.setDiscounts %}
   {{ column }}
{% endfor %}
terryupton commented 5 years ago

This has got me most of the way there...

      {% for col in purchasable.setDiscounts.GBP %}
        {#        {{ col[0] }} - {{ col[1] }}#}
        {{ col }}
      {% endfor %}

      {% for key, value in purchasable.setDiscounts.GBP %}
        Key : {{ key }}
        Value : {{ value }}
      {% endfor %}
terryupton commented 5 years ago

Final update:

{% for purchasable in product.variants %}
  {{ purchasable.title }}<br />
  {{ purchasable.price|commerceCurrency(cart.currency) }}<br />

  {% for qty, price in purchasable.setDiscounts.GBP|slice(1) %}
    Qty : {{ qty }}
    Price : {{ price|number_format(2, '.', ',')|commerceCurrency(cart.currency) }}
    <br />
  {% endfor %}<br /></br />
{% endfor %}
samuelbirch commented 5 years ago

{% for qty, price in purchasable.bulkPricesField[cart.paymentCurrency] if key != 'iso' %} Qty: {{ qty }} Price: {{ price|commerceCurrency(cart.paymentCurrency) }} {% endfor %}