Create an array of all woocommerce orders and feed it into a looper

Hello there.
I’m tryning to create an array of all WC orders and display them via a looper.

A) Code for the functions.php


add_filter( ‘cs_looper_provider_all_orders_array’, function( $result, $args ) {

$query_args = array(
    'limit'    => -1, 
    'orderby'  => 'date',
    'order'    => 'DESC',
    'return'   => 'ids',
);

$order_ids = wc_get_orders( $query_args );
$orders = [];

foreach ( $order_ids as $order_id ) {
    $order = wc_get_order( $order_id );

    $orders[] = [
        'id'        => $order->get_id(),
        'status'    => $order->get_status(),
        'total'     => $order->get_total(),
        'currency'  => $order->get_currency(),
        'date'      => $order->get_date_created() ? $order->get_date_created()->date('Y-m-d H:i') : '',
        'customer'  => $order->get_formatted_billing_full_name(),
        'email'     => $order->get_billing_email(),
        'payment'   => $order->get_payment_method_title(),
    ];
}

return $orders;

}, 10, 2 );


B) Looper Provider is “query string” with the WP-Query “type:all_orders_array”


C) Looper Consumer is a column object with a text element:


Order #: {{dc:looper:field key=“id”}}
Customer: {{dc:looper:field key=“customer”}}
Email: {{dc:looper:field key=“email”}}
Total: {{dc:looper:field key=“total”}} {{dc:looper:field key=“currency”}}
Date: {{dc:looper:field key=“date”}}
Payment: {{dc:looper:field key=“payment”}}
Status: {{dc:looper:field key=“status”}}


Unfortunately there are no values visible. There should be at least 4 orders = 4 columns visible. There are only 3 with empty values there.
What am I missing?
Thx a heap for any hint.

EDIT: sorry for the formating of the code. The first and the last line should be in the “preformated text” area as well. Btw. I actually did not enclose the code into the preformated text tag. Your system did and left out the the first and the last line.

ok. I tried something different:

the function above should create a custom looper provider with the according keys. Correct?

So I changed the looper provider to custom and used as the hook “all_orders_array”.

so far so good. Unfortunately if I choose in the looper consumer the looper -> field option, no keys can be choose.

duh. And do I really need to bother your “one” service for this peanut? Sigh… The same logic works with a shortcode just fine.

function shortcode_wc_orders_array( $atts ) {
// Allow optional attributes (like limit)
$atts = shortcode_atts( array(
‘limit’ => -1, // -1 = no limit
), $atts, ‘orders_array’ );

// Build WooCommerce query
$query_args = array(
	'limit'    => intval( $atts['limit'] ),
	'orderby'  => 'date',
	'order'    => 'DESC',
	'return'   => 'ids',
);

$order_ids = wc_get_orders( $query_args );
$orders = [];

foreach ( $order_ids as $order_id ) {
	$order = wc_get_order( $order_id );

	$orders[] = [
		'id'       => $order->get_id(),
		'status'   => $order->get_status(),
		'total'    => $order->get_total(),
		'currency' => $order->get_currency(),
		'date'     => $order->get_date_created() ? $order->get_date_created()->date('Y-m-d H:i') : '',
		'customer' => $order->get_formatted_billing_full_name(),
		'email'    => $order->get_billing_email(),
		'payment'  => $order->get_payment_method_title(),
	];
}

// Return HTML output
if ( empty( $orders ) ) {
	return '<p>No orders found.</p>';
}

$output = '<ul class="wc-orders">';
foreach ( $orders as $order ) {
	$output .= '<li>';
	$output .= 'Order #' . esc_html( $order['id'] ) . ' | ';
	$output .= 'Total: ' . esc_html( $order['currency'] ) . ' ' . esc_html( $order['total'] ) . ' | ';
	$output .= 'Customer: ' . esc_html( $order['customer'] ) . ' | ';
	$output .= 'Email: ' . esc_html( $order['email'] ) . ' | ';
	$output .= 'Status: ' . esc_html( ucfirst( $order['status'] ) ) . ' | ';
	$output .= 'Date: ' . esc_html( $order['date'] ) . ' | ';
	$output .= 'Payment: ' . esc_html( $order['payment'] );
	$output .= '</li>';
}
$output .= '</ul>';

return $output;

}
add_shortcode( ‘orders_array’, ‘shortcode_wc_orders_array’ );

Hello @VRANKOVINA,

Thank you for the info.

Have you tried manually inputting the dynamic content in a text field? Since you’re already familiar with the fields, you can try writing it directly into the content – for example: {{ looper.field(‘key’: ‘id’) }}.

We also recommend enabling Twig and toggling debug mode so you can use something like {{ dump(looper.item) }} to inspect the available fields manually.

If you can provide the login details in a secure note, we’ll be happy to look into this further.

Best regards,

Hello Ismael

That is exactly what I tried to do in first place:

I placed this directly into a text field which was in a column (looper consumer). With no luck.
Unfortunately I had to restore a backup of the site without the “experiment” mentioned here. I need to set it up back again, so, pls have patience. I’ll come back as soon as its ready.

Fun fact: If I change the looper provider to a query builder and select for the posts option “orders”, no keys are visible either.

It’s correct to change the Looper Provider > Custom since you’re trying to use an external query or the function all_orders_array. Let us know once the test page is available so we can inspect it.

Hello Ismael
I figured it out! As soon as I have a second I’ll post the solution here. It were some minor changes in the php script needed. Thats it.

I’ll be back.

Cheers

Good to know! We’ll keep the thread open and wait for the solution. Thank you for keeping us updated.

Here we go:

with this php script you will fetch all desired data from woocommerce. add this to your functions.php of your CHILD theme:

add_filter( ‘cs_looper_custom_wc_orders_with_items’, function( $result, $params ) {

// Fetch all WooCommerce orders — adjust query args as needed
$orders = wc_get_orders( array(
    'limit' => -1,  // fetch all orders
    'orderby' => 'date',
    'order' => 'DESC',
));

$result = [];
foreach ( $orders as $order ) {

    // Get all purchased items for the order
    $items = [];
    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product(); // WC_Product object, might be null if product deleted

        $items[] = [
            'product_id'   => $product ? $product->get_id() : 0,
            'product_name' => $item->get_name(),
            'quantity'     => $item->get_quantity(),
            'total'        => wc_format_decimal( $item->get_total(), 2 ),
            'sku'          => $product ? $product->get_sku() : '',
            // Add more product/item details here if needed
        ];
    }

    $result[] = [
        'id'          => $order->get_id(),
        'date'        => $order->get_date_created()->date('Y-m-d H:i:s'),
        'status'      => $order->get_status(),
        'total'       => $order->get_total(),
        'customer'    => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
        'items'       => $items, // List of purchased items
    ];
}

return $result;

}, 10, 2 );

sorry for the CSS here. Something puts a part of the code into “preformatted” style. So, make sure, you copy all the code.

for the looper provider, preferably a row, choose “custom” and use in this case “wc_orders_with_items” as the hook.

for the looper consumer, preferably a text element, insert all the desired info you’ve fetched with the php script:

Order Nr.: {{dc:looper:field key=“id”}}
Date: {{dc:looper:field key=“date”}}
Customer : {{dc:looper:field key=“customer”}}
Total: € {{dc:looper:field key=“total”}}
Status: {{dc:looper:field key=“status”}}

And as the script fetches all the order’s items as well - which may be more than 1 - you can nest a looper provider inside the initial consumer column containing the order’s details.

I chose for the nested container a static list (but that is up to your preference) element. The static list element contains a nifty element called “figure”. This spares you a couple of minutes adding the numbering to the list.

The looper provider is the list item. The children are Figure and Content. In the Content container there is the text element carrying the items.

For the looper provider I chose “array” and the key was /is “items”.

The content of the text element is:

Name: {{dc:looper:field key=“product_name”}}
Quantity: {{dc:looper:field key=“quantity”}}
Total: € {{dc:looper:field key=“total”}}

Works great here so far. Unfortunately I cant give you a live example because its NDA.
For folks who are not so familiar with the abyss of woocommerce and want to fetch more order’s details with the script, perplexity AI is your friend!

AND: If you find a flaw or some BS here, pls pls pls correct me.

Hello @VRANKOVINA,

Thank you very much for sharing your detailed solution and instructions. Your PHP script for fetching all WooCommerce orders (with their items) and the clear explanation for integrating the data into the builder via a custom Looper provider are both excellent.

Thanks