Prestashop 1.7 Add address form fields to customer form in Backoffice

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

Here is my code

<?php

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

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

New contributor

Informatique Connect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật