Closed DarrylHardin closed 5 years ago
Okay, I finally figured it out for me. Feel free to use if you'd like. I think it would be nice to have this or something similar to it if there is a better way to go about it.
{# Get past orders #}
{% set pastOrders = craft.orders.customer(craft.commerce.customers).isCompleted(true).all() %}
{# set userRow array #}
{% set userRow = [[]] %}
{# loops through past orders and sets up to output correctly #}
{% for order in pastOrders %}
{% set userRow = userRow|merge([[order.customer.user.email, order.customer.user ]]) %}
{% endfor %}
{% spaceless %}
{% set options = {
header: ['Email', 'Name'],
rows: userRow
} %}
{{ craft.beam.csv(options) }}
{% endspaceless %}
I agree, as this is something that trips up people a lot. It's awkward to work with arrays and appending data in Twig.
There is plans to make this easier with Beam, but I don't have time to execute on them yet. Track #3 for updates.
Thanks for letting me know, though.
With 2.1 and the new append()
method, this should be much easier.
Please check it out, and let me know if anything could be improved even further.
@sjelfull Could you please help me out? I am trying to dynamically create a CSV and just cant get it to work / understand the docs.. here's my example below.. Thanks for your direction
{% set entries = craft.entries()
.section('bulkOrders')
.all() %}
{# Display the list #}
{% for entry in entries %}
{% do beam.append([entry.title, entry.firstName]) %}
{% endfor %}
{% set options = {
header: ['Title', 'First Name']
} %}
{% set beam = craft.beam.create(options) %}
{% do beam.csv() %}
Hi, looks like you are setting the beam variable after you are using it in the loop?
What happens if you move it before the loop? What kind of errors are you seeing?
Hi, looks like you are setting the beam variable after you are using it in the loop?
What happens if you move it before the loop? What kind of errors are you seeing?
Thanks, That fixed it! Doh!
Is it possible to loop through matrix blocks in the append method?
entry.title, entry.firstName, entry.block1.title, entry.block2.title and so on?
Sure, you can do that. It will just append a new row to the sheet.
It would be helpful if there was an example that showed how to input values dynamically rather than static.
This is what I have
However it returns array to string conversion error. If I change the
[[order.customer.user, footwear]]
to[order.customer.user, footwear]
it works, but puts everything on multiple columns and a single row.