SilverCart Forum

We moderate this Forum and we're here to help. Have you already run a forum search to check if your problem has already been solved?

You can help us helping you by providing detailed error messages, screenshots and logfile entries.

SebastianRamon

Page: 1 , 2 , 3 , 4
Topic Change fields in an Address 8042 Views

Change fields in an Address

9 November 2011 at 5:07pm

Hi

Can you please advise the best method to change the fields in the SilverCartAddress class (Changing existing fields, adding new fields) in a way that is maintainable between Silvercart updates. I would rather avoid directly changing the core Silvercart classes if possible.

Many thanks

Jason

Re: Change fields in an Address

9 November 2011 at 5:28pm

Hello Jason,

to add new fields to SilvercartAddress you should use the decorator pattern.

Implement the decorator like this:

<?php

class MySilvercartAddressDecorator extends DataObjectDecorator {

/**
* Additional statics for the decorated DataObject.
*
* @return array
*/
public function extraStatics() {
return array(
'db' => array(
'MyNewDbField_1' => 'VarChar(64)',
'MyNewDbField_2' => 'Int(16)',
),
);
}

}

Then you have to add the extension to SilvercartAddress. Do this in your _config.php (e.g. mysite/_config.php) by adding the following line:

Object::add_extension('SilvercartAddress', 'MySilvercartAddressDecorator');

After that you have to do a /dev/build/?flush=all on your installation. Then your new DB fields are generated.

You can find SilverStripes DataObjectDecorator documentation here for more information about this pattern.

I don't really understand what you want to change on existing fields. Could you explain?

Cheers,
Sebastian

Re: Change fields in an Address

9 November 2011 at 5:33pm

Thanks for the quick reply

One question with that approach, if I remove a field using the updateCMSFields method will this be reflected in the front end somehow?

Also are additions and removals of fields at this level reflected automatically through the system, for things like the checkout, order forms, pdf invoices etc.

Re: Change fields in an Address

9 November 2011 at 6:47pm

Hello Jason,

the updateCMSFields method won't remove a field out of the front end. To remove a field out of a form you have to use an own form template.
For example you can copy the file silvercart/templates/Layout/SilvercartAddAddressForm.ss into your project folder, remove the field and run a ?flush=all.

To remove or add a form field out of/into an existing form (PHP based) is a little bit harder. You have to write a new CustomHtmlForm (based on the one you want to extend) and register that form to the Page that shall display the form.
We will deploy an easier extendable version (Decorator based) of CustomHtmlForm within the next days.

Re: Change fields in an Address

10 November 2011 at 2:53pm

Hi Jason,

we just deployed a new version 1.3.1 of CustomHtmlForm which allows to easily add, remove or change fields of existing CustomHtmlForms. You can get it from bitbucket.

Try the documentation "How to extend a CustomHtmlForm" to get your new fields working.

Cheers,
Sebastian

Re: Change fields in an Address

10 November 2011 at 2:55pm

Thanks for that, I will give that a try and see how I get on.

Re: Change fields in an Address

14 November 2011 at 2:20pm

Just thought I would post back, that the new version of customhtmlforms works a treat.

Thanks
Jason

Re: Change fields in an Address

14 November 2011 at 3:33pm

Hi Jason,

thanks for you kind words. It's good to read positive feedback as well :-)

Cheers
Ramon