<p>Got this error and not sure how can I fix this.</p>
<pre><code>// Function to get orders for a specific vendor
public function getOrdersForVendor($vendorId)
{
    $orders = $this-&gt;getAllOrdersFromWooCommerce();

    $vendorOrders = [];
    foreach ($orders as $order) {
        foreach ($order['line_items'] as $item) {
            foreach ($item['meta_data'] as $meta) {
                if ($meta['key'] === '_vendor_id' &amp;&amp; $meta['value'] == $vendorId) {
                    $vendorOrders[] = $order;
                    break 2; // Exit both inner foreach loops
                }
            }
        }
    }

    // Format the filtered orders
    $formattedOrders = [];
    foreach ($vendorOrders as $order) {
        $formattedDate = date('Y-m-d g:i A', strtotime($order['date_created']));

        $formattedOrders[] = [
            'customer_name' =&gt; $order['billing']['first_name'] . ' ' . $order['billing']['last_name'],
            'product' =&gt; array_map(function ($item) {
                return $item['name'];
            }, $order['line_items']),
            'order_placed' =&gt; $formattedDate,
            'price' =&gt; $order['total'],
            'weight_quantity' =&gt; array_map(function ($item) {
                return $item['quantity'];
            }, $order['line_items']),
            'address' =&gt; $order['billing']['address_1'] . ', ' . $order['billing']['city'] . ', ' . $order['billing']['postcode'] . ', ' . $order['billing']['country'],
            'order_number' =&gt; $order['id'],
            'customer_notes' =&gt; $order['customer_note'],
        ];
    }

    return $formattedOrders;
}
 
</code></pre>
<p>Here is the twig codes involved</p>
<pre><code>                &lt;div class=&quot;table-container&quot;&gt;

                    {# {% set vendorId = 66 %}
                    {% set orders = craft.app.orderService.getOrdersForVendor(vendorId)  %} #}

                    {% set currentUser = craft.app.user.identity %}
                    {% set vendorId = currentUser.vendorId ?? null %} {# Replace `vendorId` with the correct user attribute #}

                    {% if vendorId %}
                        {% set orders = craft.app.orderService.getOrdersForVendor(vendorId) %}
                    {% else %}
                        {# Handle the case where vendorId is not set, e.g., show an error message or empty state #}
                        {% set orders = [] %}
                        &lt;p&gt;No vendor ID found for the current user.&lt;/p&gt;
                    {% endif %}

                    &lt;table class=&quot;table&quot;&gt;
                        &lt;thead class=&quot;column-header&quot;&gt;
                            &lt;tr&gt;
                                &lt;th scope=&quot;col&quot; class=&quot;first-head&quot;&gt;&lt;input type=&quot;checkbox&quot; id=&quot;headerCheckbox&quot;/&gt;&lt;/th&gt;
                                &lt;th scope=&quot;col&quot;&gt;Customer Name&lt;/th&gt;
                                &lt;th scope=&quot;col&quot;&gt;Product&lt;/th&gt;
                                &lt;th scope=&quot;col&quot;&gt;Order Placed&lt;/th&gt;
                                &lt;th scope=&quot;col&quot;&gt;Price&lt;/th&gt;
                                &lt;th scope=&quot;col&quot;&gt;Weight/Quantity&lt;/th&gt;
                                &lt;th scope=&quot;col&quot;&gt;Address&lt;/th&gt;
                                &lt;th scope=&quot;col&quot;&gt;Order Number&lt;/th&gt;
                                &lt;th scope=&quot;col&quot;&gt;Customer Notes&lt;/th&gt;
                            &lt;/tr&gt;
                        &lt;/thead&gt;
                        &lt;tbody id=&quot;unallocatedOrders&quot;&gt;
                            {% for order in orders %}
                                &lt;tr class=&quot;clickable-row&quot;&gt;
                                    &lt;th scope=&quot;row&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;delete-checkbox&quot;/&gt;&lt;/th&gt;
                                    &lt;td class=&quot;name&quot;&gt;{{ order.customer_name }}&lt;/td&gt;
                                    &lt;td class=&quot;product&quot;&gt;
                                        &lt;span class=&quot;product-short&quot;&gt;{{ order.product | join(', ') | slice(0, 30) }}
                                            {% if order.product | join(', ') | length &gt; 30 %}...
                                            {% endif %}
                                        &lt;/span&gt;
                                        &lt;span class=&quot;product-full&quot;&gt;{{ order.product | join(', ') }}&lt;/span&gt;
                                    &lt;/td&gt;
                                    &lt;td class=&quot;order-date&quot;&gt;{{ order.order_placed }}&lt;/td&gt;
                                    &lt;td class=&quot;price&quot;&gt;${{ order.price }}&lt;/td&gt;
                                    &lt;td class=&quot;weight&quot;&gt;{{ order.weight_quantity | join(', ') }}&lt;/td&gt;
                                    &lt;td class=&quot;address&quot;&gt;{{ order.address }}&lt;/td&gt;
                                    &lt;td class=&quot;order-number&quot;&gt;{{ order.order_number }}&lt;/td&gt;
                                    &lt;td class=&quot;notes&quot;&gt;
                                        &lt;span class=&quot;notes-short&quot;&gt;{{ order.customer_notes | slice(0, 30) }}
                                            {% if order.customer_notes | length &gt; 30 %}...
                                            {% endif %}
                                        &lt;/span&gt;
                                        &lt;span class=&quot;notes-full&quot;&gt;{{ order.customer_notes }}&lt;/span&gt;
                                    &lt;/td&gt;
                                &lt;/tr&gt;
                            {% endfor %}
                        &lt;/tbody&gt;
                    &lt;/table&gt;
                &lt;/div&gt;
</code></pre>
<p>Stack Trace</p>
<pre><code>2024-07-03 10:08:12 [web.INFO] [nystudio107\codeeditor\CodeEditor::bootstrap] CodeEditor module bootstrapped {&quot;memory&quot;:4181480} 
2024-07-03 10:08:12 [web.INFO] [mediabeastnz\abandonedcart\AbandonedCart::init] Abandoned Cart for Craft Commerce plugin loaded {&quot;memory&quot;:4477704} 
2024-07-03 10:08:12 [web.INFO] [wbrowar\adminbar\AdminBar::init] Admin Bar plugin loaded {&quot;memory&quot;:4488456} 
2024-07-03 10:08:12 [web.INFO] [simplygoodwork\blockusage\BlockUsage::init] Block Usage plugin loaded {&quot;memory&quot;:4500584} 
2024-07-03 10:08:12 [web.INFO] [nystudio107\codefield\CodeField::init] Code Field plugin loaded {&quot;memory&quot;:4656344} 
2024-07-03 10:08:12 [web.INFO] [yii\web\Session::open] Session started {&quot;memory&quot;:4673760} 
2024-07-03 10:08:12 [web.INFO] [bymayo\commercewidgets\CommerceWidgets::init] Commerce Widgets plugin loaded {&quot;memory&quot;:4726984} 
2024-07-03 10:08:12 [web.INFO] [mediabeastnz\backinstock\BackInStock::init] Back In Stock plugin loaded {&quot;memory&quot;:4770920} 
2024-07-03 10:08:12 [web.INFO] [async-queue] New PushEvent for Purge Unfinalized Assets job - (handled) {&quot;memory&quot;:4951016} 
2024-07-03 10:08:12 [web.INFO] [wbrowar\guide\Guide::init] Guide plugin loaded {&quot;memory&quot;:5720392} 
2024-07-03 10:08:12 [web.INFO] [chasegiunta\jason\Jason::init] Jason plugin loaded {&quot;memory&quot;:5737208} 
2024-07-03 10:08:12 [web.INFO] [dodecastudio\lightbox\Lightbox::init] Lightbox plugin loaded {&quot;memory&quot;:5758640} 
2024-07-03 10:08:12 [web.INFO] [superbig\mjml\MJML::init] MJML plugin loaded {&quot;memory&quot;:5767432} 
2024-07-03 10:08:12 [web.INFO] [nystudio107\seomatic\Seomatic::init] SEOmatic plugin loaded {&quot;memory&quot;:6126024} 
2024-07-03 10:08:12 [web.INFO] [custommodule] WooCommerce Orders Response: Array
</code></pre>

CC BY-SA 4.0
2024-07-02T07:50:12.923Z
Please include the error log and full stack trace in your question. Also, what have you tried to resolve this? What debugging steps have you taken? Where are you stuck?
MoritzLost
CC BY-SA 4.0
2024-07-02T19:34:34.993Z
Which Craft version is this?
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2024-07-03T01:55:13.707Z
@MatsMikkelRummelhoff this is for Craft Version 4
Nate
CC BY-SA 4.0
2024-07-03T01:57:26.720Z
@MoritzLost I am typically stuck with that error as I was trying to call out orders based on who is logged in on CraftCMS from woocommerce Rest API, it is supposed to be dynamic rendering of rows and tables on a specific page.
Nate
CC BY-SA 4.0
2024-07-03T02:00:18.397Z
@MoritzLost I added the twig file from the question, added the code involved.
Nate
CC BY-SA 4.0
2024-07-03T09:32:23.247Z
@Nate That's only info level logs, not the actual error or stack trace. Maybe post a screenshot of the exception view you're seeing, that should also show the stack trace and line the error is coming from.
MoritzLost
<controller>
Serialization of 'Closure' is not allowed in Craft CMS
# Serialization of 'Closure' is not allowed in Craft CMS

(Asked by: username_0 on 2024-07-02)

Got this error and not sure how can I fix this.
// Function to get orders for a specific vendor
public function getOrdersForVendor($vendorId)
{
    $orders = $this->getAllOrdersFromWooCommerce();

    $vendorOrders = [];
    foreach ($orders as $order) {
        foreach ($order['line_items'] as $item) {
            foreach ($item['meta_data'] as $meta) {
                if ($meta['key'] === '_vendor_id' && $meta['value'] == $vendorId) {
                    $vendorOrders[] = $order;
                    break 2; // Exit both inner foreach loops
                }
            }
        }
    }

    // Format the filtered orders
    $formattedOrders = [];
    foreach ($vendorOrders as $order) {
        $formattedDate = date('Y-m-d g:i A', strtotime($order['date_created']));

        $formattedOrders[] = [
            'customer_name' => $order['billing']['first_name'] . ' ' . $order['billing']['last_name'],
            'product' => array_map(function ($item) {
                return $item['name'];
            }, $order['line_items']),
            'order_placed' => $formattedDate,
            'price' => $order['total'],
            'weight_quantity' => array_map(function ($item) {
                return $item['quantity'];
            }, $order['line_items']),
            'address' => $order['billing']['address_1'] . ', ' . $order['billing']['city'] . ', ' . $order['billing']['postcode'] . ', ' . $order['billing']['country'],
            'order_number' => $order['id'],
            'customer_notes' => $order['customer_note'],
        ];
    }

    return $formattedOrders;
}
 

Here is the twig codes involved
                

                    {# {% set vendorId = 66 %}
                    {% set orders = craft.app.orderService.getOrdersForVendor(vendorId)  %} #}

                    {% set currentUser = craft.app.user.identity %}
                    {% set vendorId = currentUser.vendorId ?? null %} {# Replace `vendorId` with the correct user attribute #}

                    {% if vendorId %}
                        {% set orders = craft.app.orderService.getOrdersForVendor(vendorId) %}
                    {% else %}
                        {# Handle the case where vendorId is not set, e.g., show an error message or empty state #}
                        {% set orders = [] %}
                        No vendor ID found for the current user.
                    {% endif %}

                    
                        
                            
                                
                                Customer Name
                                Product
                                Order Placed
                                Price
                                Weight/Quantity
                                Address
                                Order Number
                                Customer Notes
                            
                        
                        
                            {% for order in orders %}
                                
                                    
                                    {{ order.customer_name }}
                                    
                                        {{ order.product | join(', ') | slice(0, 30) }}
                                            {% if order.product | join(', ') | length > 30 %}...
                                            {% endif %}
                                        
                                        {{ order.product | join(', ') }}
                                    
                                    {{ order.order_placed }}
                                    ${{ order.price }}
                                    {{ order.weight_quantity | join(', ') }}
                                    {{ order.address }}
                                    {{ order.order_number }}
                                    
                                        {{ order.customer_notes | slice(0, 30) }}
                                            {% if order.customer_notes | length > 30 %}...
                                            {% endif %}
                                        
                                        {{ order.customer_notes }}
                                    
                                
                            {% endfor %}
                        
                    
                

Stack Trace
2024-07-03 10:08:12 [web.INFO] [nystudio107\codeeditor\CodeEditor::bootstrap] CodeEditor module bootstrapped {"memory":4181480} 
2024-07-03 10:08:12 [web.INFO] [mediabeastnz\abandonedcart\AbandonedCart::init] Abandoned Cart for Craft Commerce plugin loaded {"memory":4477704} 
2024-07-03 10:08:12 [web.INFO] [wbrowar\adminbar\AdminBar::init] Admin Bar plugin loaded {"memory":4488456} 
2024-07-03 10:08:12 [web.INFO] [simplygoodwork\blockusage\BlockUsage::init] Block Usage plugin loaded {"memory":4500584} 
2024-07-03 10:08:12 [web.INFO] [nystudio107\codefield\CodeField::init] Code Field plugin loaded {"memory":4656344} 
2024-07-03 10:08:12 [web.INFO] [yii\web\Session::open] Session started {"memory":4673760} 
2024-07-03 10:08:12 [web.INFO] [bymayo\commercewidgets\CommerceWidgets::init] Commerce Widgets plugin loaded {"memory":4726984} 
2024-07-03 10:08:12 [web.INFO] [mediabeastnz\backinstock\BackInStock::init] Back In Stock plugin loaded {"memory":4770920} 
2024-07-03 10:08:12 [web.INFO] [async-queue] New PushEvent for Purge Unfinalized Assets job - (handled) {"memory":4951016} 
2024-07-03 10:08:12 [web.INFO] [wbrowar\guide\Guide::init] Guide plugin loaded {"memory":5720392} 
2024-07-03 10:08:12 [web.INFO] [chasegiunta\jason\Jason::init] Jason plugin loaded {"memory":5737208} 
2024-07-03 10:08:12 [web.INFO] [dodecastudio\lightbox\Lightbox::init] Lightbox plugin loaded {"memory":5758640} 
2024-07-03 10:08:12 [web.INFO] [superbig\mjml\MJML::init] MJML plugin loaded {"memory":5767432} 
2024-07-03 10:08:12 [web.INFO] [nystudio107\seomatic\Seomatic::init] SEOmatic plugin loaded {"memory":6126024} 
2024-07-03 10:08:12 [web.INFO] [custommodule] WooCommerce Orders Response: Array

**Tags:** controller
> **Comments:**
> **username_1**: Please include the error log and full stack trace in your question. Also, what have you tried to resolve this? What debugging steps have you taken? Where are you stuck?  
> **username_2**: Which Craft version is this?  
> **username_0**: @MatsMikkelRummelhoff this is for Craft Version 4  
> **username_0**: @MoritzLost I am typically stuck with that error as I was trying to call out orders based on who is logged in on CraftCMS from woocommerce Rest API, it is supposed to be dynamic rendering of rows and tables on a specific page.  
> **username_0**: @MoritzLost I added the twig file from the question, added the code involved.  
> **username_1**: @Nate That's only info level logs, not the actual error or stack trace. Maybe post a screenshot of the exception view you're seeing, that should also show the stack trace and line the error is coming from.  

---

## 0 answers

