2017 © Pedro Peláez
 

magento2-module example-address-field-note

Example module showing how to add custom fields to the checkout

image

yireo/example-address-field-note

Example module showing how to add custom fields to the checkout

  • Thursday, July 5, 2018
  • by yireo
  • Repository
  • 2 Watchers
  • 4 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Yireo ExampleAddressFieldNote

This module integrates a new fields (note) in various ways in the existing fieldsets of a shipment address. The note field follows the pattern of an Extension Attribute (programmatically added value)., (*1)

  • Setup procedure
  • Shipment Address step in the checkout
  • Address form under the Customer Account

Installation

composer require yireo-training/magento2-example-address-field-note:dev-master

Setup procedure

Through a file Setup/InstallData.php the field note is added to the database as an invisible field. The only purpose for this EAV attribute is to serve as backend to show the behaviour of Extension Attributes. Normally, an Extension Attribute would sync itself with some other table or perhaps even an external resource. This is only an example. If you need to a EAV attribute after all, forget about this approach and create a Custom Attribute instead. This procedure for Extension Attributes is to allow for complexer attributes to be stored outside of EAV., (*2)

Address form under the Customer Account

This is actually bad code: While the customer-entity can be cleanly extended using a form API, the address form is not easy to extend: Its fields are hard-coded in PHTML. Therefore, a plugin was created (etc/di.xml) to hack the new field comment (Block/Address/Edit/Field/Note.php) into the right place., (*3)

The block-class calls for the note value through the Extension Attribute code., (*4)

XML file extension_attributes.xml

To make the field note known as an Extension Attribute, it is added to the file extension_attributes.xml., (*5)

Shipment Address step in the checkout

The note field is added through the Extension Attribute method. There are 2 ways to add your own fields in the checkout: Either through XML layout code or through a PHP-based layout processor. The latter is choosen in this example, because it would allow for the XML layout update to be done automatically (depending on some kind of PHP logic). The layout processor is first added to etc/di.xml and then defined in Processor/NoteAddressField.php., (*6)

The Versions

05/07 2018

dev-master

9999999-dev https://github.com/yireo-training

Example module showing how to add custom fields to the checkout

  Sources   Download

OSL-3.0

The Requires

  • php >=7.0.0
  • magento/framework ^100.1.0|^101.0.0

 

The Development Requires

by Jisse Reitsma (Yireo)

composer-installer magento