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 8418 Views

Re: Change fields in an Address

31 March 2013 at 10:43am

Hello,

adding new field is not as easy as it should be and doesnt work for shipping/invoice address.

If you would like to add new field to SilvercartAddress, you must use decorator as written in tutorial and edit few templates:
- SilvercartAddAddressForm
- SilvercartAddAddressForm
- SilvercartCheckoutFormStep2Anonymous

Example of decorator (i want to add Company number [ICO] and remove PhoneAreaCode. Remember to set value for ICO, if already in DB):

<?php
   
   class MySilvercartAddressFormDecorator extends DataObjectDecorator {
   
      /**
       * This method is called before CustomHtmlForm requires the form fields. You
       * can manipulate the default form fields here.
       *
       * @param array &$formFields Form fields to manipulate
       *
       * @return bool
       *
       * @author Sebastian Diel <sdiel@pixeltricks.de>
       * @since 10.11.2011
       */
      public function updateFormFields(&$formFields) {
         $ICO = (isset($this->owner->address)) ? $this->owner->address->ICO : "";
         
         $formFields['ICO'] = array(
            'type' => 'TextField',
            'title' => _t('SilvercartAddress.ICO', 'Company number'),
            'value' => $ICO,
            'checkRequirements' => array(
               //'isFilledIn' => true
            )
         );
         $formFields['PhoneAreaCode'] = array(
            'type' => 'TextField',
            'title' => _t('SilvercartAddress.PHONEAREACODE', 'Phone area code'),
            'checkRequirements' => array(
               //'isFilledIn' => true
            )
         );
      }
   
   }

Second decorator is for storing the field in DB:

   <?php
   
   class MySilvercartAddressDecorator extends DataObjectDecorator {
   

      public function extraStatics() {
         return array(
            'db' => array(
               'ICO' => 'VarChar(10)'
            ),
         );
      }
   
   }

Then you should apply decorators to few classes (add in mysite/_config.php):

Object::add_extension('SilvercartAddress', 'MySilvercartAddressDecorator');
Object::add_extension('SilvercartAddAddressForm', 'MySilvercartAddressFormDecorator');
Object::add_extension('SilvercartEditAddressForm', 'MySilvercartAddressFormDecorator');
Object::add_extension('SilvercartRegisterRegularCustomerForm', 'MySilvercartAddressFormDecorator');

You will be in trouble if someone is anonymous customer (ordering without registration). You can add this field to the form, but it will be never stored because of SilvercartTools::extractAddressDataFrom() which allows only predefined set of address data to be stored.

Note: showing new fields in backend/emails/invoice means editing all templates :(
Probably i know, what is behind this, silvercart guys wants super customizable htlm form, which is fine, but makes too much work with simple changes.

I think, they should just use "formAreas". Each formArea has few fields and in template we loop just these areas, not fields. Adding new field to Area means adding the field to all templates using this area.

I just stopped my attempt to add new "Company number" field, i just rename "Addition" field and place it close to TaxIDNumber.

Maybe Im missing something. If so, let me drive correct way, please ;)

Best regrads

Pali

Re: Change fields in an Address

5 April 2013 at 3:11pm

Hi Pali,

at the moment there's no easy procedure to add fields to SilverCart addresses with "SilvercartTools::extractAddressDataFrom()" being the most problematic part.

We'll try to think about a better solution for that for the next versions.

Greetings,
Sascha

Re: Change fields in an Address

5 November 2013 at 10:23am Last edited: 5 November 2013 10:36am

Hi SC Team!

I'm stuck with customizing the address too. Please help!

What I really cannot do is getting rid of the required fields I do not need. I tried to update them with "updateFormFields" so that they can be removed from the templates. Since they are required fields, without setting them not to be required, they cannot be removed from the templates. So I tried this:

public function updateFormFields(&$formFields) {

   Debug::message("updateFormFields");

$formFields['StreetNumber'] = array(
'type' => 'TextField',
'title' => _t('Hausnummer', 'Street number')
);
   $formFields['PhoneAreaCode'] = array(
'type' => 'TextField',
'title' => _t('Telefon Vorwahl', 'Phone Area Code')
);
}

My code runs, since I can see the debug message, but that is all, nothing changes.

Interestingly, while I was fiddling around with the required setting of StreetNumber, it disappeared and it is no longer required. That is fine, but I do not know why, because If I remove the decorator, StreetNumber stays not required, so the code above clearly does nothing to change things, and I'm stuck with fields not needed :( And I also need to remove other fields as well, and those are still required.

Please help!

BTW: Is this issue already solved or not: "SilvercartTools::extractAddressDataFrom() not saving added extra fields"?

thanx is advance
Szabesz

Re: Change fields in an Address

5 November 2013 at 12:16pm

Hi Szabesz,

try this to disable the form fields requirements:

public function updateFormFields(&$formFields) {
$formFields['StreetNumber']['checkRequirements'] = array();
$formFields['PhoneAreaCode']['checkRequirements'] = array();
}

If you want to completely remove the form fields, try this:

public function updateFormFields(&$formFields) {
unset($formFields['StreetNumber']);
unset($formFields['PhoneAreaCode']);
}


Be careful with this. The submit success routine may expect this fields to be available, so this could break the handling of submission data.

Perhaps, the safest way to to hide by default required fields is to disable their requirement and transform them to hidden fields like this:

public function updateFormFields(&$formFields) {
$formFields['StreetNumber']['type'] = 'HiddenField';
$formFields['StreetNumber']['checkRequirements'] = array();
$formFields['PhoneAreaCode']['type'] = 'HiddenField';
$formFields['PhoneAreaCode']['checkRequirements'] = array();
}


Now, add the Fields to the beginning of your form template like this:

<form class="yform full" $FormAttributes >
$CustomHtmlFormMetadata

$CustomHtmlFormFieldByName(StreetNumber,CustomHtmlFormFieldHidden)
$CustomHtmlFormFieldByName(PhoneAreaCode,CustomHtmlFormFieldHidden)

...

CustomHtmlForm has a cache mechanism, perhaps this is the reason for your field to stay no longer required.
You can disable this cache for your development by adding the following call to your _config.php :

CustomHtmlForm::disableCache();

SilvercartTools::extractAddressDataFrom() does not handle your custom data.
To handle custom fields, use one of the following methods inside your form decorator:

  • public function onBeforeSubmitSuccess(&$data, &$form, &$formData);
  • public function onAfterSubmitSuccess(&$data, &$form, &$formData);
  • public function overwriteSubmitSuccess(&$data, &$form, &$formData);

Important: When using overwriteSubmitSuccess, you need to return something other than NULL.
For example:

public function overwriteSubmitSuccess(&$data, &$form, &$formData) {
// your alternative submission handling code here
// ...
// ...

return true;
}

I hope this helps!

Best Regards
Sebastian

Re: Change fields in an Address

5 November 2013 at 12:31pm

Hi Sebastian,

Thank you very much for the detailed instructions! I'm going to follow them and report back as soon as I have results.

Keep up the good work!
Szabesz

Re: Change fields in an Address

5 November 2013 at 10:49pm

Hi Sebastian,

I have just tested your instructions and the Hidden Fields do the trick! I just have to decorate the other forms and override the templates and even email templates to customize the address everywhere, and that's it. Just a few hours of work and some testing from now on :)

Thank you very much, I really appreciate the help of the SilverCart team!
Szabesz

Re: Change fields in an Address

7 November 2013 at 4:30pm Last edited: 10 November 2013 2:43pm

Hi Sebastian,

I'm making progress with implementing my decorators and templates to reflect the changes I need. But I just can't figure out how to solve the following:

With multistep forms, so - for example - with SilvercartCheckoutFormStep2Anonymous on the next step (SilvercartCheckoutFormStep5) the address data will be populated from session data, and the key player seems to be "SilvercartTools::extractAddressDataFrom()" which - as you pointed out - does not handle my custom field.

As far as I understand, none of these can help either:
onBeforeSubmitSuccess, function onAfterSubmitSuccess, overwriteSubmitSuccess
since my problem is how to display my custom field in "SilvercartCheckoutFormStep5.ss", where only those fields are going to be displayed which are processed by "extractAddressDataFrom()".

As a quick and dirty solution, I hacked the core by adding "$prefix.'_State' => 'State'" to the $dataFields array, but it would be nice if you could give me some advice on how to do it properly.

Thanx in advance
Szabesz

Re: Change fields in an Address

8 November 2013 at 9:30am Last edited: 8 November 2013 10:12am

Hi :)

The basic order handling should be processed in SilvercartCheckoutFormStepProcessOrder::process(). The method process() is called by the step form controller in CustomHtmlFormStepPage_Controller::init() and can be overwritten by decorating SilvercartCheckoutFormStepProcessOrder and adding the method extendedProcess().
There, you can provide a custom processing, used instead of the default routine.
Hint: Your extendedProcess() should return true or something else that won't be interpreted as false.

Best Regards
Sebastian