Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[Solved] Adding Session Data to a UserDefinedForm


Go to End


6 Posts   1443 Views

Avatar
SnowBoarder82

Community Member, 57 Posts

22 February 2014 at 10:42am

Hi Willr / everyone.

I'm working on a project where I would like to be able to add session data to the submitted form (and email sent out) if the session data exists. For example, the contents of a shopping cart. What would be the best way to achieve this?

The project in question is on SS 2.4 using the applicable userforms module.

PS - the Userforms module is great, thank you for your work on this!

Cheers,

Avatar
Willr

Forum Moderator, 5523 Posts

22 February 2014 at 2:11pm

Glad you find the module useful. If you want to add session data, there would be several ways to go about doing it. The first question would be to you need this on all forms or just one particular form?

A quick overview of things you could try

A) Add a DataExtension to SubmittedForm which pushed the additional data to the record on onBeforeWrite()
B) Create a new EditableFormField subclass which doesn't create a form field on the front end, but can be used to save into the database columns.

Option A is probably easier, with the biggest challenge being you DataExtension's are added to every form so you would need to check for the page in question (say have a checkbox on the userform page for whether you want to attach the data).

Avatar
SnowBoarder82

Community Member, 57 Posts

24 February 2014 at 10:56am

Thanks for your reply Willr,

I would need this information on just one form. In the specific website case, I have a contact form (created using UserForms), and then there is also a checkout / order form (created via Jedateach's Shop module). I would like the shopping cart data (if it exists in the session) to be passed into the contact form.
Exploring Option A - would that potentially cause problems to the checkout / order form processing, or would adding a Dataextension to SubmittedForm only affect all forms created using UserForms.....

Thanks,

Avatar
SnowBoarder82

Community Member, 57 Posts

24 February 2014 at 4:12pm

Hi Willr,

I've been exploring around the userforms code, tyring to get a feel for it. Looking into Option B also i've created a new editableformfield called "EditableCartField", looking at EditableLiteralField as an example.
Not sure exactly how i can use this to save into the DB / form without creating the hidden field...
Trying to create the hidden cart field has posed a problem with attempting to call the Cart contents as an array or string within the hidden field declaration.... getting a bit lost, any advice or code snippet examples would be much appreciated.

Many thanks,

Avatar
Willr

Forum Moderator, 5523 Posts

24 February 2014 at 6:38pm

Well how did you need the contents of the cart stored? Will a text value do or have you got a complex object?

A simple instance of a custom form field for your case would look like

<?php

class EditableCartContentsField extends EditableFormField {

public function getFormField() {
return null;
}

public function getFieldValidationOptions() {
return false;
}

public function getValueFromData($data) {
return "This is a string to be saved into the database"
}
}

When a form with that field is saved, the getValueFromData() method will return the data to be included in the SubmittedFormField object. It can only be a string, but if you wanted to store a more complex object you can define your own subclass of SubmittedFormField and tell your object to use that instead.

public function getSubmittedFormField() {
return new CartSubmittedFormField();
}

Then your CartSubmittedFormField can store / format completely customise data.

Avatar
SnowBoarder82

Community Member, 57 Posts

25 February 2014 at 2:17pm

That's Great Thanks Willr,

A text value is fine for what i'm after.
Using the simple instance that you had provided I was able to edit the getValueFromData() method to return the cart items in each form submission.

For reference for anyone else, here's the specific code I used to get the cart as a string to return:

public function getValueFromData($data) {

$order = ShoppingCart::singleton()->current();

if($order){
$cartitems = "Artwork: ";
$items = $order->Items();
if($items->exists()){
$i = 1;
foreach($items as $item){
$cartitems .= $i.") ". $item->Product()->Title." -> ";
$cartitems .= $item->SubTitle()." |||| ";
$i++;
}
}
return $cartitems;
}
else return "No Artwork Added";
}

Many Thanks,