Trying to extend the Order model #1103
-
Hello, I am trying to extend the order model so visitors can give additional information (order_comment). I already added a column through migration. In the boot method I listened to order.beforeCreate
I am using the Mall Starter Theme and added the field in the checkout component
But the value is not being saved. Maybe post doesn't work? I am a newbie in extending plugins, so I would really appreciate help :) Thanks!! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Try attaching your event to the Order model's beforeSave event (each model in Laravel fires this): https://docs.octobercms.com/3.x/extend/system/models.html#extending-models Order::extend(function($model) {
$model->bindEvent('model.beforeSave', function() use ($model) {
\Log::info('Order comment:'. post('order_comment'));
// Maybe also check if this is the expected url using request()->url()
if (post('order_comment')) {
$order->order_comment = post('order_comment');
}
});
}); In this case, make sure the event listener only runs when you expect it (when the order is created in the frontend) and not everytime it is saved. |
Beta Was this translation helpful? Give feedback.
Hello! Yes I used it, but the missing form was a hint! The "normal" checkout component has no form, so the field was also not being submitted! So instead I am using now the quick-checkout and now it's in the post!
The beforeSave Event doesn't fire off in my case, the test log also doesn't report. And in beforeCreate, the changed attribute apparently doesn't save. I looked in the model code, and in beforeCreates, the model is still the cart model. But after the copy into the order model, afterCreate fires off - so I used that and now it's working!! I hope this is okay?
The next problem was, that the extension of the form fields didn't work like in the documentation of october. I came up wi…