Jump to content

Front end - sort addresses in alphabetical order


ableier

Recommended Posts

  • 2 weeks later...

Hi Scully,

 

Yes, I believe this is the code you are referring to, but again I'm not sure what to change?:

 

class AddressesControllerCore extends FrontController

{
public $auth = true;
public $php_self = 'addresses';
public $authRedirection = 'addresses';
public $ssl = true;
 
/**
* Set default medias for this controller
*/
public function setMedia()
{
parent::setMedia();
$this->addCSS(_THEME_CSS_DIR_.'addresses.css');
$this->addJS(_THEME_JS_DIR_.'tools.js'); // retro compat themes 1.5
$this->addJS(_THEME_JS_DIR_.'addresses.js');
}
 
/**
* Initialize addresses controller
* @see FrontController::init()
*/
public function init()
{
parent::init();
 
if (!Validate::isLoadedObject($this->context->customer))
die(Tools::displayError('The customer could not be found.'));
}
 
/**
* Assign template vars related to page content
* @see FrontController::initContent()
*/
public function initContent()
{
parent::initContent();
 
$total = 0;
$multiple_addresses_formated = array();
$ordered_fields = array();
$addresses = $this->context->customer->getAddresses($this->context->language->id);
// @todo getAddresses() should send back objects
foreach ($addresses as $detail)
{
$address = new Address($detail['id_address']);
$multiple_addresses_formated[$total] = AddressFormat::getFormattedLayoutData($address);
unset($address);
++$total;
 
// Retro theme < 1.4.2
$ordered_fields = AddressFormat::getOrderedAddressFields($detail['id_country'], false, true);
}
 
// Retro theme 1.4.2
if ($key = array_search('Country:name', $ordered_fields))
$ordered_fields[$key] = 'country';
 
$addresses_style = array(
'company' => 'address_company',
'vat_number' => 'address_company',
'firstname' => 'address_name',
'lastname' => 'address_name',
'address1' => 'address_address1',
'address2' => 'address_address2',
'city' => 'address_city',
'country' => 'address_country',
'phone' => 'address_phone',
'phone_mobile' => 'address_phone_mobile',
'alias' => 'address_title',
);
 
$this->context->smarty->assign(array(
'addresses_style' => $addresses_style,
'multipleAddresses' => $multiple_addresses_formated,
'ordered_fields' => $ordered_fields,
'addresses' => $addresses, // retro compat themes 1.5ibility Theme < 1.4.1
));
 
$this->setTemplate(_PS_THEME_DIR_.'addresses.tpl');
}
}
Link to comment
Share on other sites

I'll help out. We do it together and you learn something. The file AddressesController.php is executed when displaying addresses.

To get the address, this controller uses this command:

 

$addresses = $this->context->customer->getAddresses($this->context->language->id);

 

Which means: take the customer class and execute the function getAdresses.

 

What I want you to show now is the code part of class Customer function getAdresses. Post it here. Once you have found it' the solution is very close.

Link to comment
Share on other sites

  • 3 weeks later...

Scully,

 

Sorry for the radio silence, and thank you for your help. 

 

I'm not sure if this is exactly what you wanted me to find.  I figure I need to change "Get address id" to get alias, somehow?!

 

/**

* Initialize address controller
* @see FrontController::init()
*/
public function init()
{
parent::init();
 
// Get address ID
$id_address = 0;
if ($this->ajax && Tools::isSubmit('type'))
{
if (Tools::getValue('type') == 'delivery' && isset($this->context->cart->id_address_delivery))
$id_address = (int)$this->context->cart->id_address_delivery;
elseif (Tools::getValue('type') == 'invoice' && isset($this->context->cart->id_address_invoice)
&& $this->context->cart->id_address_invoice != $this->context->cart->id_address_delivery)
$id_address = (int)$this->context->cart->id_address_invoice;
}
else
$id_address = (int)Tools::getValue('id_address', 0);
 
// Initialize address
if ($id_address)
{
$this->_address = new Address($id_address);
if (Validate::isLoadedObject($this->_address) && Customer::customerHasAddress($this->context->customer->id, $id_address))
{
if (Tools::isSubmit('delete'))
{
if ($this->_address->delete())
{
if ($this->context->cart->id_address_invoice == $this->_address->id)
unset($this->context->cart->id_address_invoice);
if ($this->context->cart->id_address_delivery == $this->_address->id)
{
unset($this->context->cart->id_address_delivery);
$this->context->cart->updateAddressId($this->_address->id, (int)Address::getFirstCustomerAddressId(Context::getContext()->customer->id));
}
Tools::redirect('index.php?controller=addresses');
}
$this->errors[] = Tools::displayError('This address cannot be deleted.');
}
Link to comment
Share on other sites

What you posted above has nothing to do with what I wanted you to do. Do you know what classes are? If not, you find them all in the classes directory of your shop installation.

Relating to my post #7.

 

$addresses = $this->context->customer->getAddresses($this->context->language->id);

 

The red colored part is the name of the class customer. Its file is named Customer.php. In this file, you will find the function asked for.

Link to comment
Share on other sites

  • 10 months later...

Am I close?

 /**
     * Return customer addresses.
     *
     * @param int $idLang Language ID
     *
     * @return array Addresses
     */
    public function getAddresses($idLang)
    {
        $group      = Context::getContext()->shop->getGroup();
        $shareOrder = isset($group->share_order) ? (bool)$group->share_order : false;
        $cacheId    = 'Customer::getAddresses'
            . '-' . (int)$this->id
            . '-' . (int)$idLang
            . '-' . ($shareOrder ? 1 : 0);
        if (!Cache::isStored($cacheId)) {
            $sql = 'SELECT DISTINCT a.*, cl.`name` AS country, s.name AS state, s.iso_code AS state_iso
                    FROM `'._DB_PREFIX_.'address` a
                    LEFT JOIN `'._DB_PREFIX_.'country` c ON (a.`id_country` = c.`id_country`)
                    LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON (c.`id_country` = cl.`id_country`)
                    LEFT JOIN `'._DB_PREFIX_.'state` s ON (s.`id_state` = a.`id_state`)
                    '.($shareOrder ? '' : Shop::addSqlAssociation('country', 'c')).'
                    WHERE `id_lang` = '.(int) $idLang.' AND `id_customer` = '.(int) $this->id.' AND a.`deleted` = 0';

            $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
            Cache::store($cacheId, $result);

            return $result;
        }

        return Cache::retrieve($cacheId);
    }

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...