0

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?

3
  • 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
    Mar 23 at 19:15
  • 1
    the question remains though: how and where do I set the custom field
    – kindoflike
    Mar 23 at 21:21
  • 1
    Understood, the question has been updated to clarify
    – cherrykoda
    Mar 23 at 23:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.