Jump to content

How to get list of fields in address form based on the current country of customer?


Recommended Posts

Hi. This is not a question. I have found a workaround already and just wanted to share with community.

Imagine in a custom module you want to get list of address form fields and do some process. This would work fine in your module:

$fields = AddressFormat::getOrderedAddressFields(
	$this->context->country->id,
	true,
	true
);

If you know a better way just let me know. Thanks

Edited by stifler97 (see edit history)
Link to comment
Share on other sites

Well this is not working fine. Just take a look at form-fields.tpl and you see that for attribute of name in each field they have used $field.name and for example you expect to see something like id_country as a filed in return of AddressFormat::getOrderedAddressFields() but you see this Country:name. This method gives the formatted address and not the form fields.

Link to comment
Share on other sites

There is some method for AddressController and OrderController with this name displayAjaxAddressForm() and you can call it this way:
$this->context->controller->displayAjaxAddressForm();

But this returns a null value instead of returning the address form and you can not use the form to retrieve fields.

Link to comment
Share on other sites

OK too bad for Prestashop that I can not easily use their code and actually reuse that. I tried this and works:

Just copied every method from the class of CustomerAddressForm.php

$addressForm = $this->getAddressForm();

$addressFormFields = $addressForm->getTemplateVariables()['formFields'];

And then these:

    protected function getAddressForm()
    {
        $addressForm = $this->makeAddressForm();

        if (Tools::getIsset('id_address') && ($id_address = (int) Tools::getValue('id_address'))) {
            $addressForm->loadAddressById($id_address);
        }

        if (Tools::getIsset('id_country')) {
            $addressForm->fillWith(['id_country' => Tools::getValue('id_country')]);
        }
        
        return $addressForm;
    }

    protected function makeAddressForm() : CustomerAddressForm
    {
        if (Configuration::get('PS_RESTRICT_DELIVERED_COUNTRIES')) {
            $availableCountries = Carrier::getDeliveredCountries($this->context->language->id, true, true);
        } else {
            $availableCountries = Country::getCountries($this->context->language->id, true);
        }

        $form = new CustomerAddressForm(
            $this->context->smarty,
            $this->context->language,
            $this->getTranslator(),
            $this->makeAddressPersister(),
            new CustomerAddressFormatter(
                $this->context->country,
                $this->getTranslator(),
                $availableCountries
            )
        );

        $form->setAction($this->getCurrentURL());

        return $form;
    }

    protected function makeAddressPersister()
    {
        return new CustomerAddressPersister(
            $this->context->customer,
            $this->context->cart,
            Tools::getToken(true, $this->context)
        );
    }

    protected function getCurrentURL()
    {
        return Tools::getCurrentUrlProtocolPrefix() . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    }

 

Link to comment
Share on other sites

This is the final Answer:
Make a class in your module like this:

<?php

class CustomerAddresssFormExtended extends CustomerAddressForm
{
    protected static function makeCustomerAddressForm()
    {
        if (Configuration::get('PS_RESTRICT_DELIVERED_COUNTRIES')) {
            $availableCountries = Carrier::getDeliveredCountries(Context::getContext()->language->id, true, true);
        } else {
            $availableCountries = Country::getCountries(Context::getContext()->language->id, true);
        }

        return new CustomerAddressForm(
            Context::getContext()->smarty,
            Context::getContext()->language,
            self::getTranslator(),
            self::makeAddressPersister(),
            new CustomerAddressFormatter(
                Context::getContext()->country,
                self::getTranslator(),
                $availableCountries
            )
        );
    }

    protected static function getTranslator()
    {
        return Context::getContext()->getTranslator();
    }

    protected static function makeAddressPersister()
    {
        return new CustomerAddressPersister(
            Context::getContext()->customer,
            Context::getContext()->cart,
            Tools::getToken(true, Context::getContext())
        );
    }

    public static function getFormFields()
    {
        return self::makeCustomerAddressForm()
            ->getTemplateVariables()['formFields'];
    }
}

Then use it like this every where:
 

require_once('classes/CustomerAddressFormExtended.php');

$formFields = CustomerAddresssFormExtended::getFormFields();

 

  • Like 2
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...