Jump to content

[Solved] Override AddressController.php - what am I doing wrong?


Rhapsody

Recommended Posts

I've previously modified core code to eliminate forced formatting that makes the US address format all CAPS and it works fine when I modify the following lines in AddressController.php

           if (!Tools::getValue('phone') AND !Tools::getValue('phone_mobile'))
               $this->errors[] = Tools::displayError('You must register at least one phone number');
           if (!$country = new Country((int)$address->id_country) OR !Validate::isLoadedObject($country))
               die(Tools::displayError());

           /* Comment out  US customer: normalize the address 
           if($address->id_country == Country::getByIso('US'))
           {
               include_once(_PS_TAASC_PATH_.'AddressStandardizationSolution.php');
               $normalize = new AddressStandardizationSolution;
               $address->address1 = $normalize->AddressLineStandardization($address->address1);
               $address->address2 = $normalize->AddressLineStandardization($address->address2);
           }
           End comment out US customer: normalize the address */

           $zip_code_format = $country->zip_code_format;
           if ($country->need_zip_code)



I tried inserting this as changed code in the override/controller directory so I don't have to modify the core code. Can somebody tell me what I need to do to make this work as an override rather than modifying the core code?

Link to comment
Share on other sites

Ok - I figured this out by reading some related posts and experimenting. Here is what I found.

1. The call in the override section to a parent process had to be moved to the end of my modified code

parent::preProcess();

used to be near the top, and is now moved near the bottom of the override.

2. I eliminated most all the code, except for all associated with the public function preProcess()
I kept most of that section and included my edits inside that code.

The resulting code is pasted below in two posts because of size limit

<?php
/*
* Created from 1.4.3 of AddressController.php
*
* Commented out  US customer: normalize the address (all caps)
* 
*  @author PrestaShop SA 
*  @copyright  2007-2011 PrestaShop SA
*  @version  Release: $Revision: 6594 $
*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/


class AddressController extends AddressControllerCore
{

   public function preProcess()
   {

       if ($back = Tools::getValue('back'))
           self::$smarty->assign('back', Tools::safeOutput($back));
       if ($mod = Tools::getValue('mod'))
           self::$smarty->assign('mod', Tools::safeOutput($mod));

       if (Tools::isSubmit('ajax') AND Tools::isSubmit('type'))
       {
           if (Tools::getValue('type') == 'delivery')
               $id_address = isset(self::$cart->id_address_delivery) ? (int)self::$cart->id_address_delivery : 0;
           elseif (Tools::getValue('type') == 'invoice')
               $id_address = (isset(self::$cart->id_address_invoice) AND self::$cart->id_address_invoice != self::$cart->id_address_delivery) ? (int)self::$cart->id_address_invoice : 0;
           else
               exit;
       }
       else
           $id_address = (int)Tools::getValue('id_address', 0);

       if ($id_address)
       {
           $this->_address = new Address((int)$id_address);
           if (Validate::isLoadedObject($this->_address) AND Customer::customerHasAddress((int)(self::$cookie->id_customer), (int)($id_address)))
           {
               if (Tools::isSubmit('delete'))
               {
                   if (self::$cart->id_address_invoice == $this->_address->id)
                       unset(self::$cart->id_address_invoice);
                   if (self::$cart->id_address_delivery == $this->_address->id)
                       unset(self::$cart->id_address_delivery);
                   if ($this->_address->delete())
                       Tools::redirect('addresses.php');
                   $this->errors[] = Tools::displayError('This address cannot be deleted.');
               }
               self::$smarty->assign(array('address' => $this->_address, 'id_address' => (int)$id_address));
           }
           elseif (Tools::isSubmit('ajax'))
               exit;
           else
               Tools::redirect('addresses.php');
       }
       if (Tools::isSubmit('submitAddress'))
       {
           $address = new Address();
           $this->errors = $address->validateControler();
           $address->id_customer = (int)(self::$cookie->id_customer);

           if (!Tools::getValue('phone') AND !Tools::getValue('phone_mobile'))
               $this->errors[] = Tools::displayError('You must register at least one phone number');
           if (!$country = new Country((int)$address->id_country) OR !Validate::isLoadedObject($country))
               die(Tools::displayError());

           /* Comment out  US customer: normalize the address 
           if($address->id_country == Country::getByIso('US'))
           {
               include_once(_PS_TAASC_PATH_.'AddressStandardizationSolution.php');
               $normalize = new AddressStandardizationSolution;
               $address->address1 = $normalize->AddressLineStandardization($address->address1);
               $address->address2 = $normalize->AddressLineStandardization($address->address2);
           }
           End  comment out US customer: normalize the address */

           $zip_code_format = $country->zip_code_format;
           if ($country->need_zip_code)
           {
               if (($postcode = Tools::getValue('postcode')) AND $zip_code_format)
               {
                   $zip_regexp = '/^'.$zip_code_format.'$/ui';
                   $zip_regexp = str_replace(' ', '( |)', $zip_regexp);
                   $zip_regexp = str_replace('-', '(-|)', $zip_regexp);
                   $zip_regexp = str_replace('N', '[0-9]', $zip_regexp);
                   $zip_regexp = str_replace('L', '[a-zA-Z]', $zip_regexp);
                   $zip_regexp = str_replace('C', $country->iso_code, $zip_regexp);
                   if (!preg_match($zip_regexp, $postcode))
                       $this->errors[] = ''.Tools::displayError('Zip/ Postal code').' '.Tools::displayError('is invalid.').'
'.Tools::displayError('Must be typed as follows:').' '.str_replace('C', $country->iso_code, str_replace('N', '0', str_replace('L', 'A', $zip_code_format)));
               }
               elseif ($zip_code_format)
                   $this->errors[] = ''.Tools::displayError('Zip/ Postal code').' '.Tools::displayError('is required.');
               elseif ($postcode AND !preg_match('/^[0-9a-zA-Z -]{4,9}$/ui', $postcode))
                       $this->errors[] = ''.Tools::displayError('Zip/ Postal code').' '.Tools::displayError('is invalid.').'
'.Tools::displayError('Must be typed as follows:').' '.str_replace('C', $country->iso_code, str_replace('N', '0', str_replace('L', 'A', $zip_code_format)));
           }

Link to comment
Share on other sites

2nd part of pasted code

if ($country->isNeedDni() AND (!Tools::getValue('dni') OR !Validate::isDniLite(Tools::getValue('dni'))))
               $this->errors[] = Tools::displayError('Identification number is incorrect or has already been used.');
           elseif (!$country->isNeedDni())
               $address->dni = NULL;
           if (Configuration::get('PS_TOKEN_ENABLE') == 1 AND
               strcmp(Tools::getToken(false), Tools::getValue('token')) AND
               self::$cookie->isLogged(true) === true)
               $this->errors[] = Tools::displayError('Invalid token');

           if ((int)($country->contains_states) AND !(int)($address->id_state))
               $this->errors[] = Tools::displayError('This country requires a state selection.');

           if (!sizeof($this->errors))
           {
               if (isset($id_address))
               {
                   $country = new Country((int)($address->id_country));
                   if (Validate::isLoadedObject($country) AND !$country->contains_states)
                       $address->id_state = 0;
                   $address_old = new Address((int)$id_address);
                   if (Validate::isLoadedObject($address_old) AND Customer::customerHasAddress((int)self::$cookie->id_customer, (int)$address_old->id))
                   {
                       if ($address_old->isUsed())
                       {
                           $address_old->delete();
                           if (!Tools::isSubmit('ajax'))
                           {
                               $to_update = false;
                               if (self::$cart->id_address_invoice == $address_old->id)
                               {
                                   $to_update = true;
                                   self::$cart->id_address_invoice = 0;
                               }
                               if (self::$cart->id_address_delivery == $address_old->id)
                               {
                                   $to_update = true;
                                   self::$cart->id_address_delivery = 0;
                               }
                               if ($to_update)
                                   self::$cart->update();
                           }
                       }
                       else
                       {
                           $address->id = (int)($address_old->id);
                           $address->date_add = $address_old->date_add;
                       }
                   }
               }
               elseif (self::$cookie->is_guest)
                   Tools::redirect('addresses.php');

               if ($result = $address->save())
               {
                   /* In order to select this new address : order-address.tpl */
                   if ((bool)(Tools::getValue('select_address', false)) == true OR (Tools::isSubmit('ajax') AND Tools::getValue('type') == 'invoice'))
                   {
                       /* This new adress is for invoice_adress, select it */
                       self::$cart->id_address_invoice = (int)($address->id);
                       self::$cart->update();
                   }
                   if (Tools::isSubmit('ajax'))
                   {
                       $return = array(
                           'hasError' => !empty($this->errors), 
                           'errors' => $this->errors,
                           'id_address_delivery' => self::$cart->id_address_delivery,
                           'id_address_invoice' => self::$cart->id_address_invoice
                       );
                       die(Tools::jsonEncode($return));
                   }
                   Tools::redirect($back ? ($mod ? $back.'&back;='.$mod : $back) : 'addresses.php');
               }
               $this->errors[] = Tools::displayError('An error occurred while updating your address.');
           }
       }
       elseif (!$id_address)
       {
           $customer = new Customer((int)(self::$cookie->id_customer));
           if (Validate::isLoadedObject($customer))
           {
               $_POST['firstname'] = $customer->firstname;
               $_POST['lastname'] = $customer->lastname;
           }
       }
       if (Tools::isSubmit('ajax') AND sizeof($this->errors))
       {
           $return = array(
               'hasError' => !empty($this->errors), 
               'errors' => $this->errors
           );
           die(Tools::jsonEncode($return));
       }
       parent::preProcess();

   }

}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...