for loop through order quantities

  • 5 December 2023
  • 1 reply
  • 64 views

Badge
  • Contributor I
  • 2 replies

Hi,

I need help with the syntax for an email in a flow.

The quantities per specific BundleOptions for customers can be taken from the magento2 placed order event. For example in: {{ event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.0.option_qty|default:'' }} and {{ event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.1.option_qty|default:'' }}, notice how the only difference is the number change from zero to one. Someone can have up to 20 different BundleOptions. I need to loop through them and add them up to a total so I can say something like “You ordered a total of X quantity”. With some help of AI I came up with this but it gives me a syntax error, does someone know if want I want is possible and if so how?

{% with bundle_options=event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions %} {% with total_sum=0 %} {% for i in bundle_options|length|add:"-1" %} {% with qty=bundle_options.i.option_qty|default:0 %} {% with total_sum=total_sum|add:qty %} {% endwith %} {% endwith %} {% endfor %} {{ total_sum }} {% endwith %} {% endwith %}

 

I tried it without the add:"-1" too but that didn’t work either.

Thanks,

VTPP


1 reply

Badge

FYI, this works but being able to loop would be so much cleaner.
 

{% if event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions|length == 1 %}
<div>{{ event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.0.option_qty }}</div>
{% elif event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions|length == 2 %}
<div>{{ event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.0.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.1.option_qty }}</div>
{% elif event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions|length == 3 %}
<div>{{ event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.0.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.1.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.2.option_qty }}</div>
{% elif event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions|length == 4 %}
<div>{{ event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.0.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.1.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.2.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.3.option_qty }}</div>
{% elif event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions|length == 5 %}
<div>{{ event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.0.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.1.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.2.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.3.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.4.option_qty }}</div>
{% elif event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions|length == 6 %}
<div>{{ event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.0.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.1.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.2.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.3.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.4.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.5.option_qty }}</div>
{% elif event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions|length == 7 %}
<div>{{ event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.0.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.1.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.2.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.3.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.4.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.5.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.6.option_qty }}</div>
{% elif event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions|length == 8 %}
<div>{{ event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.0.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.1.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.2.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.3.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.4.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.5.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.6.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.7.option_qty }}</div>
{% elif event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions|length == 9 %}
<div>{{ event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.0.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.1.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.2.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.3.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.4.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.5.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.6.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.7.option_qty|add:event.Extra.Items.0.ProductOption.ExtensionAttributes.BundleOptions.8.option_qty }}</div>
{% endif %}
<div>&nbsp;</div>

 

Reply