Ali Samie Posted August 8, 2022 Share Posted August 8, 2022 (edited) 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 August 8, 2022 by stifler97 (see edit history) Link to comment Share on other sites More sharing options...
Ali Samie Posted August 9, 2022 Author Share Posted August 9, 2022 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 More sharing options...
Ali Samie Posted August 9, 2022 Author Share Posted August 9, 2022 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 More sharing options...
Ali Samie Posted August 9, 2022 Author Share Posted August 9, 2022 Finally I think I have to copy and paste this method: makeAddressForm() and make it a public one so I can use it anywhere Link to comment Share on other sites More sharing options...
Ali Samie Posted August 9, 2022 Author Share Posted August 9, 2022 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 More sharing options...
Ali Samie Posted August 9, 2022 Author Share Posted August 9, 2022 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(); 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now