Hi everyone I really need your help I'm getting mad xD.
I try only in the backoffice when I create a new customer to add adress form field like that when for example I create an order in the backoffice if I create a new customer I can set up all fields in one whereas create the customer first then go to address and add the address etc so I would the form save everything in one submit form.
I'm trying to create a module but I'm stuck in fact I can see all the fields I want in create new customer but when I fill all the address fields and I save the customer is created no error message but the address is not saved. Here is the code if someone can help me to save the address fields in the same time. Thanks a lot
<?php if (!defined('_PS_VERSION_')) { exit; } use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\CountryType; use Customer; use Address; use Validate; use Tools; class CustomerAddressMod extends Module { public function __construct() { $this->name = 'customeraddressmod'; $this->tab = 'administration'; $this->version = '1.0.3'; $this->author = 'Your Name'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Module Adresse Client'); $this->description = $this->l('Ajoute les champs d\'adresse au formulaire de création de client.'); } public function install() { return parent::install() && $this->registerHook('actionCustomerFormBuilderModifier') && $this->registerHook('actionAfterCreateCustomerFormHandler') && $this->registerHook('actionObjectAddressAddBefore') && $this->registerHook('actionObjectAddressAddAfter'); } /** * Ajouter des champs d'adresse dans le formulaire de création de client */ public function hookActionCustomerFormBuilderModifier($params) { $formBuilder = $params['form_builder']; // Ajout des champs d'adresse, alias en premier $formBuilder->add('alias', TextType::class, [ 'label' => $this->l('Alias de l\'adresse'), 'required' => true, ]); $formBuilder->add('address1', TextType::class, [ 'label' => $this->l('Adresse'), 'required' => true, ]); $formBuilder->add('address2', TextType::class, [ 'label' => $this->l('Complément d\'adresse'), 'required' => false, ]); $formBuilder->add('postcode', TextType::class, [ 'label' => $this->l('Code postal'), 'required' => true, ]); $formBuilder->add('city', TextType::class, [ 'label' => $this->l('Ville'), 'required' => true, ]); $formBuilder->add('id_country', CountryType::class, [ 'label' => $this->l('Pays'), 'required' => true, ]); $formBuilder->add('phone', TextType::class, [ 'label' => $this->l('Téléphone'), 'required' => false, ]); $formBuilder->add('phone_mobile', TextType::class, [ 'label' => $this->l('Téléphone mobile'), 'required' => false, ]); } /** * Sauvegarder les informations d'adresse après la création du client */ public function hookActionAfterCreateCustomerFormHandler($params) { $customer = $params['customer']; // Créer une adresse associée à ce client uniquement s'il n'y a pas d'erreurs dans la création du client if (Validate::isLoadedObject($customer)) { $this->createAddress($customer); } } /** * Créer une adresse pour le client existant */ private function createAddress($customer) { $address = new Address(); $address->id_customer = $customer->id; $address->alias = Tools::getValue('alias'); $address->address1 = Tools::getValue('address1'); $address->address2 = Tools::getValue('address2'); $address->postcode = Tools::getValue('postcode'); $address->city = Tools::getValue('city'); $address->id_country = Tools::getValue('id_country'); $address->phone = Tools::getValue('phone'); $address->phone_mobile = Tools::getValue('phone_mobile'); $address->firstname = $customer->firstname; $address->lastname = $customer->lastname; if (!$address->save()) { $this->context->controller->errors[] = $this->l('Erreur lors de la sauvegarde de l\'adresse.'); } else { $this->context->controller->success[] = $this->l('Adresse ajoutée avec succès.'); } } /** * Hook avant l'ajout de l'adresse */ public function hookActionObjectAddressAddBefore($params) { $address = $params['object']; if (!Validate::isLoadedObject($address)) { $this->context->controller->errors[] = $this->l('L\'adresse n\'est pas valide.'); } } /** * Hook après l'ajout de l'adresse */ public function hookActionObjectAddressAddAfter($params) { $address = $params['object']; if (Validate::isLoadedObject($address)) { $this->context->controller->success[] = $this->l('Adresse ajoutée avec succès.'); } } }