{"id":1575,"date":"2017-08-11T21:25:30","date_gmt":"2017-08-11T19:25:30","guid":{"rendered":"https:\/\/www.h-hennes.fr\/blog\/?p=1575"},"modified":"2017-08-11T21:25:30","modified_gmt":"2017-08-11T19:25:30","slug":"magento-2-ajouter-des-nouveaux-attributs-aux-formulaires-de-creation-de-compte","status":"publish","type":"post","link":"https:\/\/www.h-hennes.fr\/blog\/2017\/08\/11\/magento-2-ajouter-des-nouveaux-attributs-aux-formulaires-de-creation-de-compte\/","title":{"rendered":"Magento 2 : Ajouter des nouveaux attributs aux formulaires de cr\u00e9ation de compte"},"content":{"rendered":"<p>Cet article est une mise \u00e0 jour d&rsquo;un article pr\u00e9c\u00e9dent sur Magento 1 : <a href=\"https:\/\/www.h-hennes.fr\/blog\/2015\/11\/19\/magento-ajouter-des-nouveaux-attributs-aux-formulaires-de-creation-de-compte\/\">Magento 1 : Ajouter des nouveaux attributs aux formulaires de cr\u00e9ation de compte <\/a><\/p>\n<p>Nous allons voir comment rajouter un attribut client aux formulaire de cr\u00e9ation de compte et d&rsquo;\u00e9dition du compte client.<br \/>\nCelui-ci sera nomm\u00e9 \u00ab\u00a0Sample\u00a0\u00bb<br \/>\n(La partie checkout a \u00e9t\u00e9 totalement r\u00e9\u00e9crite dans magento 2 et fera l&rsquo;objet d&rsquo;un article ult\u00e9rieur)<\/p>\n<p>Pour cela nous allons cr\u00e9er un module Hhennes_Customer qui sera situ\u00e9 dans le dossier app\/code\/Hhennes\/Customer.<br \/>\nLa structure de notre module sera la suivante :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1577\" src=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2017\/08\/hhennes_customer.jpg\" alt=\"Structure module magento 2\" width=\"305\" height=\"301\" srcset=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2017\/08\/hhennes_customer.jpg 305w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2017\/08\/hhennes_customer-300x296.jpg 300w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/p>\n<p>Pour commencer comme pour tout module magento 2, il faut d\u00e9clarer notre module en cr\u00e9ant le fichier module.xml avec le contenu suivant dans le dossier \/etc\/ de notre module :<\/p>\n<pre lang=\"xml\" escaped=\"true\">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\"&gt;\r\n &lt;module name=\"Hhennes_Customer\" setup_version=\"0.1.0\"&gt;\r\n &lt;sequence&gt;\r\n &lt;module name=\"Mage_Customer\"\/&gt; &lt;!-- D\u00e9pendance au module Mage Customer --&gt;\r\n &lt;\/sequence&gt;\r\n &lt;\/module&gt;\r\n&lt;\/config&gt;\r\n<\/pre>\n<p>Ansi que le fichier registration.php \u00e0 la racine avec le contenu suivant :<\/p>\n<pre lang=\"php\" escaped=\"true\">&lt;?php\r\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\r\n\\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\r\n'Hhennes_Customer',\r\n__DIR__\r\n);\r\n<\/pre>\n<p>Pour installer notre module il faut le fichier \u00ab\u00a0InstallData.php\u00a0\u00bb dans un dossier \u00ab\u00a0Setup\u00a0\u00bb avec le contenu suivant<\/p>\n<pre lang=\"php\" escaped=\"true\"><!--?php namespace Hhennes\\Customer\\Setup; use Magento\\Eav\\Setup\\EavSetup; use Magento\\Eav\\Setup\\EavSetupFactory; use Magento\\Eav\\Model\\Config; use Magento\\Framework\\Setup\\InstallDataInterface; use Magento\\Framework\\Setup\\ModuleDataSetupInterface; use Magento\\Framework\\Setup\\ModuleContextInterface; class InstallData implements InstallDataInterface { protected $eavSetupFactory; protected $eavConfig; \/** * Injection des d\u00e9pendances \u00e0 la cr\u00e9ation des * InstallData constructor * @param EavSetupFactory $eavSetupFactory * @param Config $eavConfig *\/ public function __construct( EavSetupFactory $eavSetupFactory , Config $eavConfig ) { $this-&gt;eavSetupFactory = $eavSetupFactory;\r\n        $this-&gt;eavConfig = $eavConfig;\r\n\r\n    }\r\n\r\n\t \/**\r\n     * @param ModuleDataSetupInterface $setup\r\n     * @param ModuleContextInterface $context\r\n     *\/\r\n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\r\n    {\r\n        $eavSetup = $this-&gt;eavSetupFactory-&gt;create(['setup' =&gt; $setup]);\r\n\t\t\r\n\t\t\/\/Cr\u00e9ation de l'attribut\r\n        $eavSetup-&gt;addAttribute(\r\n            \\Magento\\Customer\\Model\\Customer::ENTITY,\r\n            'sample',\r\n            [\r\n                'type' =&gt; 'text',\r\n                'label' =&gt; 'Sample attribute',\r\n                'input' =&gt; 'text',\r\n                'required'=&gt; false,\r\n                'default' =&gt; '',\r\n                'sort_order' =&gt; 100,\r\n                'system' =&gt; false,\r\n                'position' =&gt; 100,\r\n            ]\r\n        );\r\n\r\n\t\t\/\/Ajout de l'attribut aux formulaires clients\r\n        $autoLoginAttribute = $this-&gt;eavConfig-&gt;getAttribute(\\Magento\\Customer\\Model\\Customer::ENTITY,'sample');\r\n        $autoLoginAttribute-&gt;setData('used_in_forms',\r\n            ['adminhtml_customer','customer_account_create','customer_account_edit']\r\n        );\r\n        $autoLoginAttribute-&gt;save();\r\n    }\r\n}\r\n&lt;\/pre&gt;\r\n&lt;p&gt;La partie back-office du module est \u00e0 pr\u00e9sent compl\u00e8te.&lt;br ?-->\r\nIl faut g\u00e9rer l'affichage via la cr\u00e9ation de layouts et de templates et l'ensemble fonctionne quasiment comme dans Magento 1\r\nLes pages de cr\u00e9ation et d'\u00e9dition de compte comportent le code suivant :<\/pre>\n<pre lang=\"php\" escaped=\"true\">&lt;?php echo $block-&gt;getChildHtml('form_additional_info'); ?&gt;\r\n<\/pre>\n<p>qui permet de rajouter des \u00e9l\u00e9ments suppl\u00e9mentaires.<\/p>\n<p>Les controller concern\u00e9s sont donc les suivants : customer_account_create customer_account_edit, nous allons donc cr\u00e9er les fichiers de layouts associ\u00e9s dans le dossier view\/frontend\/layout\/<\/p>\n<p>customer_account_create.xml avec le contenu suivant :<\/p>\n<pre lang=\"xml\" escaped=\"true\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" layout=\"1column\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt;\r\n&lt;body&gt;\r\n&lt;referenceContainer name=\"form.additional.info\"&gt;\r\n&lt;block class=\"Magento\\Framework\\View\\Element\\Template\" name=\"form_additional_sample\" template=\"Hhennes_Customer::form\/sample_attribute.phtml\"\/&gt;\r\n&lt;\/referenceContainer&gt;\r\n&lt;\/body&gt;\r\n&lt;\/page&gt;\r\n<\/pre>\n<p>customer_account_edit.xml avec le contenu suivant :<\/p>\n<pre lang=\"xml\" escaped=\"true\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" layout=\"1column\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt;\r\n&lt;body&gt;\r\n&lt;referenceContainer name=\"form.additional.info\"&gt;\r\n&lt;block class=\"Magento\\Customer\\Block\\Form\\Edit\" name=\"form_additional_sample\" template=\"Hhennes_Customer::form\/sample_attribute_edit.phtml\"\/&gt;\r\n&lt;\/referenceContainer&gt;\r\n&lt;\/body&gt;\r\n&lt;\/page&gt;\r\n<\/pre>\n<p>Notez bien la diff\u00e9rence dans le premier cas le bloc est de type Magento\\Framework\\View\\Element\\Template alors que dans le second il est de type Magento\\Customer\\Block\\Form\\Edit<\/p>\n<p>Pour finir il faut cr\u00e9er les fichier phtml dans le dossier view\/frontend\/template\/form\/sample_attribute.phtml<\/p>\n<pre lang=\"php\" escaped=\"true\">&lt;?php\r\n&lt;div class=\"field\"&gt;\r\n&lt;label class=\"label\" for=\"sample\"&gt;&lt;span&gt;&lt;?php \/* @escapeNotVerified *\/ echo __('Sample Data') ?&gt;&lt;\/span&gt;&lt;\/label&gt;\r\n&lt;input type=\"text\" name=\"sample\" title=\"&lt;?php \/* @escapeNotVerified *\/ echo __('Sample Data') ?&gt;\" class=\"input-text\" autocomplete=\"off\" value=\"\"\/&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>et sample_attribute_edit.phtml<\/p>\n<pre lang=\"php\" escaped=\"true\">&lt;?php\r\n&lt;div class=\"field\"&gt;\r\n&lt;label class=\"label\" for=\"sample\"&gt;&lt;span&gt;&lt;?php \/* @escapeNotVerified *\/ echo __('Sample Data') ?&gt;&lt;\/span&gt;&lt;\/label&gt;\r\n&lt;input type=\"text\" name=\"sample\" title=\"&lt;?php \/* @escapeNotVerified *\/ echo __('Sample Data') ?&gt;\" class=\"input-text\" autocomplete=\"off\"\r\nvalue=\"&lt;?php echo $block-&gt;getCustomer()-&gt;getCustomAttribute('sample')-&gt;getValue() ?&gt;\"\/&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>Si tout va bien votre champ devrait \u00eatre visible sur les pages de cr\u00e9ation et d&rsquo;\u00e9dition de compte.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1578\" src=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2017\/08\/new-account.jpg\" alt=\"Nouveau champs account\" width=\"708\" height=\"795\" srcset=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2017\/08\/new-account.jpg 708w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2017\/08\/new-account-267x300.jpg 267w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/p>\n<p>Note : Attention \u00e0 date d&rsquo;aujourd&rsquo;hui dans le th\u00e8me luma, les champs additionnels sont affich\u00e9s \u00e0 la suite du mot de passe, et il est n\u00e9cessaire d&rsquo;afficher celui-ci pour les voir.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cet article est une mise \u00e0 jour d&rsquo;un article pr\u00e9c\u00e9dent sur Magento 1 : Magento 1 : Ajouter des nouveaux attributs aux formulaires de cr\u00e9ation de compte Nous allons voir comment rajouter un attribut client aux formulaire de cr\u00e9ation de compte et d&rsquo;\u00e9dition du compte client. Celui-ci sera nomm\u00e9 \u00ab\u00a0Sample\u00a0\u00bb (La partie checkout a \u00e9t\u00e9 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[246],"tags":[497,482],"class_list":["post-1575","post","type-post","status-publish","format-standard","hentry","category-magento-2","tag-attribut-client","tag-magento-2"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/1575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/comments?post=1575"}],"version-history":[{"count":2,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/1575\/revisions"}],"predecessor-version":[{"id":1579,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/1575\/revisions\/1579"}],"wp:attachment":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/media?parent=1575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/categories?post=1575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/tags?post=1575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}