Jump to content

Add address form fields to customer form in Backoffice


Recommended Posts

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.');
        }
    }
}


 

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

Thx I tried to add what you said but doesn’t work either 

     

Quote

if (!$address->add()) {
            $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.');
        }
    }
}

 

Link to comment
Share on other sites

You should do this like that:

 /**
     * 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;
		$address->add(); //Here add address

        if (!$address->add()) {
            $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.');
        }
    }

Also check in Address class that in code I use correct method to add address it could be otehr method or this method could have some required parameters.

Link to comment
Share on other sites

Thanks for your time and help.

I try your code and even delete 

if (!$address->add()) { $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.'); }

Not be called twice but I doesn't work I'm just after if there's other option 

Link to comment
Share on other sites

I also tried this :

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;

class CustomerAddressMod extends Module
{
    public function __construct()
    {
        $this->name = 'customeraddressmod';
        $this->tab = 'administration';
        $this->version = '1.0.1';
        $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');
    }

    /**
     * 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 et sauvegarder l'adresse associée au client
        $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');

        // Validation des champs obligatoires
        if (empty($customer->firstname) || empty($customer->lastname) || empty($customer->email)) {
            $this->context->controller->errors[] = $this->l('Les champs Nom, Prénom, et Email sont obligatoires pour enregistrer l\'adresse.');
            return;
        }

        // Ajouter nom et prénom à l'adresse
        $address->firstname = $customer->firstname;
        $address->lastname = $customer->lastname;

        // Sauvegarder l'adresse
        if (!$address->add()) {
            $this->context->controller->errors[] = $this->l('Erreur lors de la sauvegarde de l\'adresse.');
        }
    }
}

 

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