<p>It seems that the following line was not needed.
<code>$order = Order::find()-&gt;id($order-&gt;id)-&gt;one();</code>
When I removed it everything works as expected.</p>

CC BY-SA 4.0
2024-04-23T12:20:43.593Z
2024-04-23T12:20:43.593Z
Matthias
<p>I am using Craft Commerce and want to edit the order after it has been completed by the customer. For this I am using the EVENT_AFTER_COMPLETE_ORDER event.</p>
<pre><code>Event::on(
            Order::class,
            Order::EVENT_AFTER_COMPLETE_ORDER,
            function (Event $event) {
                $order = $event-&gt;sender;
                $order = Order::find()-&gt;id($order-&gt;id)-&gt;one();

                if (!$order) {
                    throw new \Exception('Invalid order ID: ' . $this-&gt;id);
                }

                if ($order-&gt;shippingMethodHandle === 'DHL' &amp;&amp; !$order-&gt;parcelNumber) {
                    $order-&gt;setFieldValue(&quot;parcelNumber&quot;, 5005);

                    if (!Craft::$app-&gt;getElements()-&gt;saveElement($order)) {
                        Craft::error('Automatic creation of Mikropakket label has failed.', __METHOD__);
                    }
                }
            }
        );
</code></pre>
<p>I can correctly read the values in the $order variable but saving the parcelNumber does not seem to work. There are no errors and when I debug with Xdebug I can see that the saveElement method is being called.
When looking at the order the parcelNumber field is empty.</p>
<p>Does anyone have any experience with this?</p>

<plugin-development><plugin-craftcommerce><modules><custom-field>
Save field value After Complete Order
# Save field value After Complete Order

(Asked by: username_0 on 2024-04-17)

I am using Craft Commerce and want to edit the order after it has been completed by the customer. For this I am using the EVENT_AFTER_COMPLETE_ORDER event.
Event::on(
            Order::class,
            Order::EVENT_AFTER_COMPLETE_ORDER,
            function (Event $event) {
                $order = $event->sender;
                $order = Order::find()->id($order->id)->one();

                if (!$order) {
                    throw new \Exception('Invalid order ID: ' . $this->id);
                }

                if ($order->shippingMethodHandle === 'DHL' && !$order->parcelNumber) {
                    $order->setFieldValue("parcelNumber", 5005);

                    if (!Craft::$app->getElements()->saveElement($order)) {
                        Craft::error('Automatic creation of Mikropakket label has failed.', __METHOD__);
                    }
                }
            }
        );

I can correctly read the values in the $order variable but saving the parcelNumber does not seem to work. There are no errors and when I debug with Xdebug I can see that the saveElement method is being called.
When looking at the order the parcelNumber field is empty.
Does anyone have any experience with this?

**Tags:** plugin-development, plugin-craftcommerce, modules, custom-field


---

## 1 answers

### Answer by username_0 on 2024-04-23. Score: 1 | Accepted answer

It seems that the following line was not needed.
`$order = Order::find()->id($order->id)->one();`
When I removed it everything works as expected.


---

