<p>I want to set a custom field on shopping cart when I first retrieve it.</p>
<p><a href="https://craftcms.com/docs/commerce/4.x/extend/events.html#modifycartinfo" rel="nofollow noreferrer">https://craftcms.com/docs/commerce/4.x/extend/events.html#modifycartinfo</a> sounds appropriate so I tried doing this:</p>
<pre><code>Event::on(
    BaseFrontEndController::class,
    BaseFrontEndController::EVENT_MODIFY_CART_INFO,
    function(ModifyCartInfoEvent $e) {
        $order = $e-&gt;cart;
        $order-&gt;setFieldValue('customField', SOME_VALUE);
        $e-&gt;cart = $order;
    }
);
</code></pre>
<p>the event handler is executed, yet <code>customField</code> is <code>null</code> in the API response.</p>
<p>doing the same thing in an <code>Adjuster</code> works. however that only runs on a cart <em>update</em> obviously.</p>
<hr />
<p>edit: ok, this explains why it didn't work that way <a href="https://github.com/craftcms/commerce/blob/develop/src/events/ModifyCartInfoEvent.php#L28" rel="nofollow noreferrer">https://github.com/craftcms/commerce/blob/develop/src/events/ModifyCartInfoEvent.php#L28</a></p>
<p>What's the right way to set the field on carts?</p>

CC BY-SA 4.0
2023-03-23T19:15:35.697Z
Hi there, it looks like you may have answered this yourself. Feel free to add the answer to your own question to receive credit and help the community. It is totally valid to answer your own questions here. Thanks!
cherrykoda
CC BY-SA 4.0
2023-03-23T21:21:05.117Z
the question remains though: how and where do I set the custom field
kindoflike
CC BY-SA 4.0
2023-03-23T23:09:53.190Z
Understood, the question has been updated to clarify
cherrykoda
<plugin-craftcommerce><events><custom-field><cart>
How to modify a custom field on cart, when the cart is created
# How to modify a custom field on cart, when the cart is created

(Asked by: username_0 on 2023-03-23)

I want to set a custom field on shopping cart when I first retrieve it.
https://craftcms.com/docs/commerce/4.x/extend/events.html#modifycartinfo sounds appropriate so I tried doing this:
Event::on(
    BaseFrontEndController::class,
    BaseFrontEndController::EVENT_MODIFY_CART_INFO,
    function(ModifyCartInfoEvent $e) {
        $order = $e->cart;
        $order->setFieldValue('customField', SOME_VALUE);
        $e->cart = $order;
    }
);

the event handler is executed, yet customField is null in the API response.
doing the same thing in an Adjuster works. however that only runs on a cart update obviously.

edit: ok, this explains why it didn't work that way https://github.com/craftcms/commerce/blob/develop/src/events/ModifyCartInfoEvent.php#L28
What's the right way to set the field on carts?

**Tags:** plugin-craftcommerce, events, custom-field, cart
> **Comments:**
> **username_1**: Hi there, it looks like you may have answered this yourself. Feel free to add the answer to your own question to receive credit and help the community. It is totally valid to answer your own questions here. Thanks!  
> **username_0**: the question remains though: how and where do I set the custom field  
> **username_1**: Understood, the question has been updated to clarify  

---

## 0 answers

