Custom fields for orders, customers

You may want to store additional information for each customer. In a nutshell you need to update your DataModel, and any places where the data is entered. This can generally be done by creating extensions.

First read the SilverStripe documentation about decorators.

Customer

Customer fields are saved to both Members and Orders.

Add database field(s) to the model

In [mysite]/code/ExtendedCustomer.php

<?php
class ExtendedCustomer extends DataObjectDecorator{

    function extraStatics(){
        return array(
            'db' => array(
                'MyExtraField' => 'Varchar'
            )
        );
    }
    
}

To your _config.php file, add:

Object::add_extension('Member','ExtendedCustomer');
Object::add_extension('Order','ExtendedCustomer');

Update form(s)

To let your website visitors actually enter the data, you will need to modify various forms.

In [mysite]/code/ExtendedOrderForm.php

<?php
class ExtendedOrderForm extends Extension{

    function updateFields($fields){
        $fields->push(new TextField("MyExtraField","My Extra Field"));
    }

}

To your _config.php file, add:

Object::add_extension('OrderForm','ExtendedOrderForm');

Note: remember to flush the class manifest by adding ?flush=1 to your site url.

comments powered by Disqus